Drawing Lines, Rectangles and Ovals

This section presents Graphics methods for drawing lines, rectangles and ovals. The methods and their parameters are summarized in Fig. 12.17. For each drawing method that requires a width and height parameter, the width and height must be nonnegative values. Otherwise, the shape will not display.

Figure 12.17. Graphics methods that draw lines, rectangles and ovals.

(This item is displayed on pages 612 - 613 in the print version)

Method

Description

public void drawLine( int x1, int y1, int x2, int y2 )

 

Draws a line between the point (x1, y1) and the point (x2, y2).

public void drawRect( int x, int y, int width, int height)

 

Draws a rectangle of the specified width and height. The top-left corner of the rectangle has the coordinates (x, y). Only the outline of the rectangle is drawn using the Graphics object's colorthe body of the rectangle is not filled with this color.

public void fillRect( int x, int y, int width, int height )

 

Draws a filled rectangle with the specified width and height. The top-left corner of the rectangle has the coordinate (x, y). The rectangle is filled with the Graphics object's color.

public void clearRect( int x, int y, int width, int height )

 

Draws a filled rectangle with the specified width and height in the current background color. The top-left corner of the rectangle has the coordinate (x, y). This method is useful if the programmer wants to remove a portion of an image.

public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )

 

Draws a rectangle with rounded corners in the current color with the specified width and height. The arcWidth and arcHeight determine the rounding of the corners (see Fig. 12.20). Only the outline of the shape is drawn.

public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )

 

Draws a filled rectangle with rounded corners in the current color with the specified width and height. The arcWidth and arcHeight determine the rounding of the corners (see Fig. 12.20).

public void draw3DRect( int x, int y, int width, int height, boolean b )

 

Draws a three-dimensional rectangle in the current color with the specified width and height. The top-left corner of the rectangle has the coordinates (x, y). The rectangle appears raised when b is true and lowered when b is false. Only the outline of the shape is drawn.

public void fill3DRect( int x, int y, int width, int height, boolean b )

 

Draws a filled three-dimensional rectangle in the current color with the specified width and height. The top-left corner of the rectangle has the coordinates (x, y). The rectangle appears raised when b is true and lowered when b is false.

public void drawOval( int x, int y, int width, int height )

 

Draws an oval in the current color with the specified width and height. The bounding rectangle's top-left corner is at the coordinates (v, y). The oval touches all four sides of the bounding rectangle at the center of each side (see Fig. 12.21). Only the outline of the shape is drawn.

public void fillOval( int x, int y, int width, int height )

 

Draws a filled oval in the current color with the specified width and height. The bounding rectangle's top-left corner is at the coordinates (x, y). The oval touches all four sides of the bounding rectangle at the center of each side (see Fig. 12.21).

The application of Fig. 12.18Fig. 12.19 demonstrates drawing a variety of lines, rectangles, three-dimensional rectangles, rounded rectangles and ovals.

Figure 12.18. Drawing lines, rectangles and ovals.

(This item is displayed on page 614 in the print version)

1 // Fig. 12.18: LinesRectsOvalsJPanel.java 2 // Drawing lines, rectangles and ovals. 3 import java.awt.Color; 4 import java.awt.Graphics; 5 import javax.swing.JPanel; 6 7 public class LinesRectsOvalsJPanel extends JPanel 8 { 9 // display various lines, rectangles and ovals 10 public void paintComponent( Graphics g ) 11 { 12 super.paintComponent( g ); // call superclass's paint method 13 14 this.setBackground( Color.WHITE ); 15 16 g.setColor( Color.RED ); 17 g.drawLine( 5, 30, 380, 30 ); 18 19 g.setColor( Color.BLUE ); 20 g.drawRect( 5, 40, 90, 55 ); 21 g.fillRect( 100, 40, 90, 55 ); 22 23 g.setColor( Color.CYAN ); 24 g.fillRoundRect( 195, 40, 90, 55, 50, 50 ); 25 g.drawRoundRect( 290, 40, 90, 55, 20, 20 ); 26 27 g.setColor( Color.YELLOW ); 28 g.draw3DRect( 5, 100, 90, 55, true ); 29 g.fill3DRect( 100, 100, 90, 55, false ); 30 31 g.setColor( Color.MAGENTA ); 32 g.drawOval( 195, 100, 90, 55 ); 33 g.fillOval( 290, 100, 90, 55 ); 34 } // end method paintComponent 35 } // end class LinesRectsOvalsJPanel

Figure 12.19. Creating JFrame to display lines, rectangles and ovals.

(This item is displayed on page 615 in the print version)

1 // Fig. 12.19: LinesRectsOvals.java 2 // Drawing lines, rectangles and ovals. 3 import java.awt.Color; 4 import javax.swing.JFrame; 5 6 public class LinesRectsOvals 7 { 8 // execute application 9 public static void main( String args[] ) 10 { 11 // create frame for LinesRectsOvalsJPanel 12 JFrame frame = 13 new JFrame( "Drawing lines, rectangles and ovals" ); 14 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 15 16 LinesRectsOvalsJPanel linesRectsOvalsJPanel = 17 new LinesRectsOvalsJPanel(); 18 linesRectsOvalsJPanel.setBackground( Color.WHITE ); 19 frame.add( linesRectsOvalsJPanel ); // add panel to frame 20 frame.setSize( 400, 210 ); // set frame size 21 frame.setVisible( true ); // display frame 22 } // end main 23 } // end class LinesRectsOvals  

In Fig. 12.18, line 17 draws a red line, line 20 draws an empty blue rectangle and line 21 draws a filled blue rectangle. Methods fillRoundRect (line 24) and drawRoundRect (line 25) draw rectangles with rounded corners. Their first two arguments specify the coordinates of the upper-left corner of the bounding rectanglethe area in which the rounded rectangle will be drawn. Note that the upper-left corner coordinates are not the edge of the rounded rectangle, but the coordinates where the edge would be if the rectangle had square corners. The third and fourth arguments specify the width and height of the rectangle. The last two arguments determine the horizontal and vertical diameters of the arc (i.e., the arc width and arc height) used to represent the corners.

Figure 12.20 labels the arc width, arc height, width and height of a rounded rectangle. Using the same value for the arc width and arc height produces a quarter circle at each corner. When the arc width, arc height, width and height have the same values, the result is a circle. If the values for width and height are the same and the values of arcWidth and arcHeight are 0, the result is a square.

Figure 12.20. Arc width and arc height for rounded rectangles.

(This item is displayed on page 615 in the print version)

Methods draw3DRect (line 28) and fill3DRect (line 29) take the same arguments. The first two arguments specify the top-left corner of the rectangle. The next two arguments specify the width and height of the rectangle, respectively. The last argument determines whether the rectangle is raised (true) or lowered (false). The three-dimensional effect of draw3DRect appears as two edges of the rectangle in the original color and two edges in a slightly darker color. The three-dimensional effect of fill3DRect appears as two edges of the rectangle in the original drawing color and the fill and other two edges in a slightly darker color. Raised rectangles have the original drawing color edges at the top and left of the rectangle. Lowered rectangles have the original drawing color edges at the bottom and right of the rectangle. The three-dimensional effect is difficult to see in some colors.

Methods drawOval and fillOval (lines 3233) take the same four arguments. The first two arguments specify the top-left coordinate of the bounding rectangle that contains the oval. The last two arguments specify the width and height of the bounding rectangle, respectively. Figure 12.21 shows an oval bounded by a rectangle. Note that the oval touches the center of all four sides of the bounding rectangle. (The bounding rectangle is not displayed on the screen.)

Figure 12.21. Oval bounded by a rectangle.

Категории