1 / 21

The End of Graphics in Java

Delve into advanced 2D graphics and imaging in Java with a focus on arcs, polygons, painting modes, Java2D, and more. Learn to create, fill, and manipulate shapes using graphics methods and explore the capabilities of Java2D for various applications.

roux
Download Presentation

The End of Graphics in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The End of Graphics in Java A picture's worth a thousand words CS 102-02 Lecture 7-2

  2. Agenda • Arcs Revisited • Polygons • Messing with the Screen • Paint modes • Java2D

  3. 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. The Arc and The Rectangle (x, y) height width 120° arcAngle startAngle - 60°

  5. "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. 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. Inside a Polygon • Java draws and connects points in the order they're listed • Sometimes you get interior points

  8. Building Polygon Objects • Polygon class public Polygon(int xpoints[], int ypoints[], int npoints) • Constructs and initializes a polygon from the specified parameters

  9. 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. 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. 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. Start With This... 30, 10 150 150

  13. Use copyArea() Rectangle to copy from copyArea(30, 10, 150, 150, 200, 60) Move over dX, dY

  14. And Copy To This 30, 10 30+200, 10+60 150 150

  15. 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. Paint and XOR Mode

  17. 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. 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. Overlapping Over and Over Book's explanation is a little simplistic 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);

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

More Related