Presentation is loading. Please wait.

Presentation is loading. Please wait.

Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.

Similar presentations


Presentation on theme: "Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc."— Presentation transcript:

1 Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.

2 Overview Graphics Project 2

3 Graphics Abstract class No instantiation Graphics g = someComponent.getGraphics()

4 Graphics API Graphics (1.1) Available fonts Lines drawn with a single-pixel width Shapes painted solid colors only

5 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

6 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

7 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).

8 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

9 Optional: modify drawing parameters g2d.setPaint(fillColorOrPattern); g2d.setStroke(penThicknessOrPattern); g2d.setComposite(someAlphaComposite); g2d.setFont(someFont); g2d.rotate(...); g2d.scale(...); g2d.setTransform(someAffineTransform);

10 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); ….

11 Graphics 2D: Cast public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; …stuff here… }

12 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

13 Other shapes… Arc2D Ellipse2D GeneralPath, Line2D Rectangle2D, & RoundRectangle2D

14 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

15 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);

16 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);

17 Resulting shape

18 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);

19 Creating…

20 Some Examples… Graphics Assortment of shapes Graphics 2D Added functionality Fonts Using fonts


Download ppt "Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc."

Similar presentations


Ads by Google