1 / 17

Painting (Chapter 12)

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.

iwalani
Download Presentation

Painting (Chapter 12)

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. Painting(Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth

  2. Preview • The paint() method and the graphics context. • the GUI thread and the repaint() method. • Spontaneous painting. • Painting to images.

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

  4. Graphics Context(java.awt.Graphics) • Represents the “thing” you are painting to: • Component • Image • Printer • Most Useful For: • Applet • Canvas • Frame • Panel

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

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

  7. Selecting a Font, Continued • Style: • Font.PLAIN • Font.BOLD • Font.ITALIC • Font.BOLD + Font.ITALIC • Size: In points (e.g. 24 point)

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

  9. Primitive Painting Methods(pp. 331-338) • drawLine() • drawRect() and fillRect() • drawOval() and fillOval() • drawArc() and fillArc() • drawPolygon() and fillPolygon() • drawPolyline() • drawString() • drawImage()

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

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

  12. 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.”

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

  14. Images • “Images are off-screen representations of rectangular pixel patterns.” • Operations: • Create • Modify • Draw to screen or other images

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

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

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

More Related