may 13, 1998cs102-02lecture 7-2 the end of graphics in java cs 102-02 lecture 7-2 a picture's...

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

Post on 22-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 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

May 13, 1998 CS102-02 Lecture 7-2

The End of Graphics in Java

CS 102-02

Lecture 7-2

A picture's worth a thousand words

Page 2: 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

May 13, 1998 CS102-02 Lecture 7-2

Agenda

• Arcs Revisited

• Polygons

• Messing with the Screen

• Paint modes

• Java2D

Page 3: 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

May 13, 1998 CS102-02 Lecture 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

Page 4: 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

May 13, 1998 CS102-02 Lecture 7-2

The Arc and The Rectangle

(x, y)heightwidth

- 60°

120° arcAngle

startAngle

Page 5: 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

May 13, 1998 CS102-02 Lecture 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

Page 6: 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

May 13, 1998 CS102-02 Lecture 7-2

Draw Your Own Polygon

• Two versions of drawPolygondrawPolygon(int xPoints[], int yPoints[], int points)

drawPolygon(Polygon p)

• Polygons are always closed (the book is wrong)

• points <= array length– A logic error?

Page 7: 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

May 13, 1998 CS102-02 Lecture 7-2

Inside a Polygon

• Java draws and connects points in the order they're listed

• Sometimes you get interior points

Page 8: 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

May 13, 1998 CS102-02 Lecture 7-2

Building Polygon Objects

• Polygon classpublic Polygon(int xpoints[], int ypoints[], int npoints)

– Constructs and initializes a polygon from the specified parameters

Page 9: 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

May 13, 1998 CS102-02 Lecture 7-2

Fill a Polygon

• Filled polygons are just like regular polygons, but painted in the foreground colorfillPolygon(int xPoints[], int yPoints[], int points)

fillPolygon(Polygon p)

Page 10: 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

May 13, 1998 CS102-02 Lecture 7-2

Feeling Disconnected? Draw a Polyline

• Polylines are just like polygons, except– If first and last point are different, they

aren't automatically connecteddrawPolyline(int xPoints[], int yPoints[], int nPoints)

Page 11: 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

May 13, 1998 CS102-02 Lecture 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

Page 12: 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

May 13, 1998 CS102-02 Lecture 7-2

Start With This...

30, 10

150

150

Page 13: 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

May 13, 1998 CS102-02 Lecture 7-2

Use copyArea()

copyArea(30, 10, 150, 150,

200, 60)

Rectangle to copy from

Move over dX, dY

Page 14: 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

May 13, 1998 CS102-02 Lecture 7-2

And Copy To This

30, 10

150

150

30+200, 10+60

Page 15: 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

May 13, 1998 CS102-02 Lecture 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)

Page 16: 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

May 13, 1998 CS102-02 Lecture 7-2

Paint and XOR Mode

Page 17: 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

May 13, 1998 CS102-02 Lecture 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

Page 18: 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

May 13, 1998 CS102-02 Lecture 7-2

The Code for the Picturepublic 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);}

Page 19: 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

May 13, 1998 CS102-02 Lecture 7-2

Overlapping Over and Over

g.setColor(Color.red);// Now you see itg.fillOval(220, 80, 200, 100);// ... now you don'tg.fillOval(220, 80, 200, 100);// ... now you see it againg.fillOval(220, 80, 200, 100);

Book's explanation is a little simplistic

Page 20: 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

May 13, 1998 CS102-02 Lecture 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

Page 21: 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

May 13, 1998 CS102-02 Lecture 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