13.17. Programming Exercises

 
[Page 426 ( continued )]

13.3. The Graphics Class

You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class, as shown in Figure 13.3.

Figure 13.3. The Graphics class contains the methods for drawing strings and shapes .
(This item is displayed on page 427 in the print version)

The Graphics class is an abstract class that provides a device-independent graphics interface for displaying figures and images on the screen on different platforms. Whenever a component (e.g., a button, a label, a panel) is displayed, the JVM automatically creates a Graphics object for the component on the native platform. This object can be obtained using the getGraphics() method. For example, the Graphics object for a label jlblBanner can be obtained using


[Page 427]
 Graphics graphics = jlblBanner.getGraphics(); 

Think of a GUI component as a piece of paper and the Graphics object as a pencil or paintbrush. You can apply the methods in the Graphics class to draw things on a GUI component.

To fully understand the Graphics class, we first present an intuitive, but not practical example in Listing 13.1. The program creates a label in line 5. When the main method is executed, the constructor TestGetGraphics is executed (line 13) and the label is added to the frame (line 8). At this time, the Graphics object for the label has not been created because the label has not been displayed. Therefore, jlblBanner.getGraphics() returns null (line 9). After the frame is displayed in line 18, all the components in the frame are also displayed. The Graphics object for the label is obtained in line 21. A line is drawn from (0, 0) to (50, 50) on the label, as shown in Figure 13.4.


[Page 428]
Figure 13.4. A line is drawn on a label, and the label is placed inside the frame.


Listing 13.1. TestGetGraphics.java
 1   import   javax.swing.*;  2   import   java.awt.Graphics;  3  4   public class   TestGetGraphics   extends   JFrame {  5   private   JLabel jlblBanner =   new   JLabel(   "Banner"   );  6  7   public   TestGetGraphics() {  8      add(jlblBanner);  9      System.out.println(  jlblBanner.getGraphics()  ); 10    } 11 12   public static void   main(String[] args) { 13  TestGetGraphics frame =   new   TestGetGraphics();  14      frame.setTitle(   "TestGetGraphics"   ); 15      frame.setLocationRelativeTo(   null   );  // Center the frame  16      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17      frame.setSize(   200   ,   100   ); 18      frame.setVisible(true); 19      JOptionPane.showMessageDialog(null, 20   "Delay on purpose\nClick OK to dismiss the dialog"   ); 21  Graphics graphics = frame.jlblBanner.getGraphics();  22  graphics.drawLine(     ,     ,   50   ,   50   );  23    } 24  } 

When you run this program, the frame is displayed (line 18), then a message dialog box is displayed (line 19). What is the purpose of having a message dialog box in this program? The purpose is to delay the execution of lines 21 and 22. Without the delay, you might not see the line (line 22). The reason will be discussed in the next section.

When you create a label with a text such as new JLabel("Banner") , you see the text on the label when the label is displayed. The text is actually painted on the label using the drawString method in the Graphics class internally.

Every GUI component has a Graphics object that can be obtained using getGraphics() after the component is displayed. You may rewrite this example using a button, a text field, or a text area rather a label. Note that jlblBanner.getGraphics() returns null in line 9, because jlblBanner has not been displayed yet. It is displayed when the frame is set visible in line 18.

In line 9, jlblBanner.getGraphics() is used to return a Graphics object. In line 21, why do you have to use frame.jlblBanner.getGraphics() in the main method? This is because jlblBanner is an instance variable, and it cannot be referenced directly in a static method.

 


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