15.3. Check Boxes

 
[Page 455 ( continued )]

Chapter Summary

  • Each component has its own coordinate system with the origin ( 0, 0 ) at the upper-left corner of the window. The x- coordinate increases to the right, and the y -coordinate increases downward.

  • The Graphics class is an abstract class for displaying figures and images on the screen on different platforms. The Graphics class is implemented on the native platform in the JVM. When you use the paintComponent(g) method to paint on a GUI component, this g is an instance of a concrete subclass of the abstract Graphics class for the specific platform. The Graphics class encapsulates the platform details and enables you to draw things uniformly without concern for the specific platform.

  • Invoking super.paintComponent(g) is necessary to ensure that the viewing area is cleared before a new drawing is displayed. The user can request the component to be redisplayed by invoking the repaint() method defined in the Component class. Invoking repaint() causes paintComponent to be invoked by the JVM. The user should never invoke paintComponent directly. For this reason, the protected visibility is sufficient for paintComponent .

  • Normally you use JPanel as a canvas. To draw on a JPanel , you create a new class that extends JPanel and overrides the paintComponent method to tell the panel how to draw things.

  • You can set fonts for the components or subjects you draw, and use font metrics to measure font size . Fonts and font metrics are encapsulated in the classes Font and FontMetrics . FontMetrics can be used to compute the exact length and width of a string, which is helpful for measuring the size of a string in order to display it in the right position.


    [Page 456]
  • The Component class has the setBackground , setForeground , and setFont methods. These methods are used to set colors and fonts for the entire component. Suppose you want to draw several messages in a panel with different colors and fonts; you have to use the setColor and setFont methods in the Graphics class to set the color and font for the current drawing.

  • To display an image, first create an image icon. You can then use ImageIcon 's getImage() method to get an Image object for the image and draw the image using the drawImage method in the java.awt.Graphics class.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net