Presentation is loading. Please wait.

Presentation is loading. Please wait.

May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS 102-02 Lecture 7-2 A picture's worth a thousand words.

Similar presentations


Presentation on theme: "May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS 102-02 Lecture 7-2 A picture's worth a thousand words."— Presentation transcript:

1 May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS 102-02 Lecture 7-2 A picture's worth a thousand words

2 May 13, 1998CS102-02Lecture 7-2 Agenda Arcs Revisited Polygons Messing with the Screen Paint modes Java2D

3 May 13, 1998CS102-02Lecture 7-2 Arc de Triomphe drawArc takes six arguments: –The rectangle: int x, int y, int width, int height Center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments –The angle at which to begin drawing the arc: int startAngle –The size of the arc to draw in degrees: int arcAngle

4 May 13, 1998CS102-02Lecture 7-2 The Arc and The Rectangle (x, y) heightheight width - 60° 120° arcAngle startAngle

5 May 13, 1998CS102-02Lecture 7-2 "Polly want a cracker?" "No, polygon!" Polygon is just a set of vertices –Array of points {(x1, y1), (x2, y2)... (xn, yn)} Polygonal methods draw lines and closed figures –Specify either a polygon object, or –A set of points

6 May 13, 1998CS102-02Lecture 7-2 Draw Your Own Polygon Two versions of drawPolygon drawPolygon(int xPoints[], int yPoints[], int points) drawPolygon(Polygon p) Polygons are always closed (the book is wrong) points <= array length –A logic error?

7 May 13, 1998CS102-02Lecture 7-2 Inside a Polygon Java draws and connects points in the order they're listed Sometimes you get interior points

8 May 13, 1998CS102-02Lecture 7-2 Building Polygon Objects Polygon class public Polygon(int xpoints[], int ypoints[], int npoints) –Constructs and initializes a polygon from the specified parameters

9 May 13, 1998CS102-02Lecture 7-2 Fill a Polygon Filled polygons are just like regular polygons, but painted in the foreground color fillPolygon(int xPoints[], int yPoints[], int points) fillPolygon(Polygon p)

10 May 13, 1998CS102-02Lecture 7-2 Feeling Disconnected? Draw a Polyline Polylines are just like polygons, except –If first and last point are different, they aren't automatically connected drawPolyline(int xPoints[], int yPoints[], int nPoints)

11 May 13, 1998CS102-02Lecture 7-2 Messin' with the Screen Copy one portion of a screen to another with copyArea() copyArea() needs to know two things –Area you want to copy (a rectangle) –How far to move the copy ( dX and dY ) relative to the origin of the rectangular area

12 May 13, 1998CS102-02Lecture 7-2 Start With This... 30, 10 150

13 May 13, 1998CS102-02Lecture 7-2 Use copyArea() copyArea(30, 10, 150, 150, 200, 60) Rectangle to copy from Move over dX, dY

14 May 13, 1998CS102-02Lecture 7-2 And Copy To This 30, 10 150 30+200, 10+60

15 May 13, 1998CS102-02Lecture 7-2 Painting a la Mode Pick a paint mode: overwrite or XOR Default as though setPaintMode() were called –When drawing, replace any color with current foreground color Can also setXORMode(Color xorColor)

16 May 13, 1998CS102-02Lecture 7-2 Paint and XOR Mode

17 May 13, 1998CS102-02Lecture 7-2 How XOR Mode Works Drawing in XORMode with XORColor –Pixels in the current color are changed to the specified color –And vice versa. Colors other than those two colors are changed in an unpredictable but reversible manner –If the same figure is drawn twice, then all pixels are restored to their original values

18 May 13, 1998CS102-02Lecture 7-2 The Code for the Picture public void paint(Graphics g) { // Draw in PaintMode g.setColor(Color.blue); g.fillPolygon(xPoints, yPoints, 4); g.setColor(Color.red); g.fillOval(20, 80, 200, 100); // Switch to XORMode with yellow g.setXORMode(Color.yellow); g.setColor(Color.blue); g.fillPolygon(xRightPoints, yPoints, 4); g.setColor(Color.red); g.fillOval(220, 80, 200, 100); }

19 May 13, 1998CS102-02Lecture 7-2 Overlapping Over and Over g.setColor(Color.red); // Now you see it g.fillOval(220, 80, 200, 100); //... now you don't g.fillOval(220, 80, 200, 100); //... now you see it again g.fillOval(220, 80, 200, 100); Book's explanation is a little simplistic

20 May 13, 1998CS102-02Lecture 7-2 Java2D Set of classes for advanced 2D graphics and imaging –Good for CAD/CAM –Games Included in JDK 1.2 (currently in beta 3) Built in to java.awt.* –Inheritance in action

21 May 13, 1998CS102-02Lecture 7-2 What's in Java2D? Line art, text, and images in a single comprehensive model Support for image compositing and alpha channel images Classes to provide accurate color space definition and conversion Rich set of display-oriented imaging operators


Download ppt "May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS 102-02 Lecture 7-2 A picture's worth a thousand words."

Similar presentations


Ads by Google