170 likes | 332 Views
Painting (Chapter 12). Java Certification Study Group January 25, 1999 Mark Roth. Preview. The paint() method and the graphics context. the GUI thread and the repaint() method. Spontaneous painting. Painting to images. The paint() method and the Graphics Context.
E N D
Painting(Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth
Preview • The paint() method and the graphics context. • the GUI thread and the repaint() method. • Spontaneous painting. • Painting to images.
The paint() method and the Graphics Context • public void paint( Graphics g ) • Defined in java.awt.Component • You provide the method, AWT “knows” when to call it: “This method is called when the contents of the component should be painted in response to the component first being shown or damage needing repair. The clip rectangle in the Graphics parameter will be set to the area which needs to be painted.” - JDK 1.1 Javadoc
Graphics Context(java.awt.Graphics) • Represents the “thing” you are painting to: • Component • Image • Printer • Most Useful For: • Applet • Canvas • Frame • Panel
Selecting a Color • Call g.setColor( color ) • Color can be one of the constants such as Color.blue, Color.magenta, … (These are final static Color objects) • You can also create your own color using new Color( r, g, b ) • Color change will be applied to all subsequent painting operations.
Selecting a Font • First, create the Font object: new Font( String fontname, int style, int size ) • Font Name: • Standard fonts: • “Serif” • “SansSerif” • “Monospaced” • Can be retrieved: • String fontnames[] = Toolkit.getDefaultToolkit().getFontList()
Selecting a Font, Continued • Style: • Font.PLAIN • Font.BOLD • Font.ITALIC • Font.BOLD + Font.ITALIC • Size: In points (e.g. 24 point)
Drawing and Filling • Coordinate system • X increases as you go right • Y increases as you go down • Upper-left Corner is (0,0) • Defines primitive painting methods
Primitive Painting Methods(pp. 331-338) • drawLine() • drawRect() and fillRect() • drawOval() and fillOval() • drawArc() and fillArc() • drawPolygon() and fillPolygon() • drawPolyline() • drawString() • drawImage()
Clipping • Imposes a restriction on the region that a graphics context can modify. • Call g.setClip( x, y, width, height ) • See Figure 12.15 and 12.16
Painting a Contained Component • You can subclass Canvas and provide your own implementation of Paint to create your own components. (See pp. 342-343) • paint( Graphics g ) will be called at appropriate times. • Java will automatically paint background of component with backgroundColor and call setColor( foregroundColor ) before calling paint.
Spontaneous Painting • GUI Thread Calls Paint Under 4 Circumstances: • “After exposure • After de-iconification • Shortly after init() returns (applets only) • When a browser returns to a previously displayed page containing an applet, provided the applet is at least partially visible.”
Spontaneous Painting • Rule of Thumb: Do all drawing operations in paint() • Call paint() to paint the contents of the window. • Call update() to erase the background and then paint the contents of the window. • Call repaint() to schedule a call to update (avoids duplicate calls to update).
Images • “Images are off-screen representations of rectangular pixel patterns.” • Operations: • Create • Modify • Draw to screen or other images
Creating Images • Create an Empty Image: • Image im1 = component.createImage( 400, 250 ) • Create from .gif or .jpeg (in Applet or from Toolkit): • getImage( URL fileURL ) • getImage( URL dirURL, String path )
Drawing to Images • Call im.getGraphics() and paint to that context. • Call g.drawImage( im, x, y, this ) from the paint() method of a Component to draw the image to the screen. • Very useful for flicker-free double-buffering.
References • All Material in this Presentation is heavily based on:Roberts, Simon and Heller, Philip, Java 1.1 Certification Study Guide, 1997: SYBEX™. ISBN: 0-7821-2069-5 • Selected portions from JDK 1.1.6 JavaDoc HTML pages.