Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
Overview Graphics Project 2
Graphics Abstract class No instantiation Graphics g = someComponent.getGraphics()
Graphics API Graphics (1.1) Available fonts Lines drawn with a single-pixel width Shapes painted solid colors only
Java 1.1 Graphics public void paint(Graphics g) { // Set pen parameters g.setColor(someColor); g.setFont(someLimitedFont); // Draw a shape g.drawRect(...); // outline g.drawPolygon(...); // outline g.drawOval(...); // outline... } Example: Graphics 1.1
Graphics extended (1.2) Graphics2D Colors and patterns: gradient fills, fill patterns tiled images transparency Local fonts Pen thicknesses, dashing patterns, & segment connection styles Coordinate transformations Rotations & shearing
Java 2D: Common Methods public void drawString(String s, flt x, flt y) drawString(s, 2.0f, 3.0f) public void rotate(double theta) rotation of theta radians public void rotate(dbl theta, dbl x, dbl y) theta radians, point of rotation (x, y).
Continued public void scale(double xscale, yscale) Values greater than 1.0 expand the axis Values less than 1.0 shrink the axis public void setPaint(Paint paint) Solid Color, a GradientPaint, & TexturePaint public void setStroke(Stroke pen) Line thickness around object
Optional: modify drawing parameters g2d.setPaint(fillColorOrPattern); g2d.setStroke(penThicknessOrPattern); g2d.setComposite(someAlphaComposite); g2d.setFont(someFont); g2d.rotate(...); g2d.scale(...); g2d.setTransform(someAffineTransform);
Drawing a shape Call paintComponent method of superclass maintains the component look and feel clears off-screen pixmap (Swing implements double buffering) public void paint(Graphics){ super.paint(g); ….
Graphics 2D: Cast public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; …stuff here… }
Drawing Shape Objects public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; // Assume x, y, and diameter are instance variables. Ellipse2D.Double circle = new Ellipse2D.double(x, y, diameter, diameter); g2d.fill(circle);... } There is double and float…idea being performance
Other shapes… Arc2D Ellipse2D GeneralPath, Line2D Rectangle2D, & RoundRectangle2D
Complex Constructors public Arc2D.Double( x, y, width, height, startAngle, deltaAngle, closure) A lot of built in functionality and methods ARC2D.OPEN ARC2D.PIE ARC2D.CHORD
Transforming shapes Using AffineTransforms AffineTransform at = new AffineTransform(); at.translate(150, 200); at.rotate(-Math.PI/3); at.scale(2, 0.5); g.transform(at); Ellipse2D.Float circle = new Ellipse2D.Float(-50, -50, 100, 100); g.draw(circle);
Transforming shapes AffineTransform at = new AffineTransform(); at.translate(150, 200); at.rotate(-Math.PI/3); at.scale(2, 0.5); Ellipse2D.Float circle = new Ellipse2D.Float(-50, -50, 100, 100); Shape shape = at.createTransformedShape(circle); g.draw(shape);
Resulting shape
General paths: Custom Shapes GeneralPath path = new GeneralPath(); path.moveTo(50, 50); path.lineTo(150, 150); path.quadTo(200, 200, 250, 150); path.curveTo(250, 250, 150, 250, 150, 200); path.closePath(); g.draw(path);
Creating…
Some Examples… Graphics Assortment of shapes Graphics 2D Added functionality Fonts Using fonts