Drawing Arcs

An arc is drawn as a portion of an oval. Arc angles are measured in degrees. Arcs sweep (i.e., move along a curve) from a starting angle by the number of degrees specified by their arc angle. The starting angle indicates in degrees where the arc begins. The arc angle specifies the total number of degrees through which the arc sweeps. Figure 12.22 illustrates two arcs. The left set of axes shows an arc sweeping from zero degrees to approximately 110 degrees. Arcs that sweep in a counterclockwise direction are measured in positive degrees. The set of axes on the right shows an arc sweeping from zero degrees to approximately 110 degrees. Arcs that sweep in a clockwise direction are measured in negative degrees. Note the dashed boxes around the arcs in Fig. 12.22. When drawing an arc, we specify a bounding rectangle for an oval. The arc will sweep along part of the oval. Graphics methods drawArc and fillArc for drawing arcs are summarized in Fig. 12.23.

Figure 12.22. Positive and negative arc angles.

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

Figure 12.23. Graphics methods for drawing arcs.

Method

Description

public void drawArc( int x, int y, int width, int height, int startAngle, int arcAngle )

 

Draws an arc relative to the bounding rectangle's top-left x and y coordinates with the specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees.

public void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle )

 

Draws a filled arc (i.e., a sector) relative to the bounding rectangle's top-left x and y coordinates with the specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees.

The application of Fig. 12.24Fig. 12.25 demonstrates the arc methods of Fig. 12.23. The application draws six arcs (three unfilled and three filled). To illustrate the bounding rectangle that helps determine where the arc appears, the first three arcs are displayed inside a red rectangle that has the same x, y, width and height arguments as the arcs.

Figure 12.24. Arcs displayed with drawArc and fillArc.

(This item is displayed on pages 617 - 618 in the print version)

1 // Fig. 12.24: ArcsJPanel.java 2 // Drawing arcs. 3 import java.awt.Color; 4 import java.awt.Graphics; 5 import javax.swing.JPanel; 6 7 public class ArcsJPanel extends JPanel 8 { 9 // draw rectangles and arcs 10 public void paintComponent( Graphics g ) 11 { 12 super.paintComponent( g ); // call superclass's paintComponent 13 14 // start at 0 and sweep 360 degrees 15 g.setColor( Color.RED ); 16 g.drawRect( 15, 35, 80, 80 ); 17 g.setColor( Color.BLACK ); 18 g.drawArc( 15, 35, 80, 80, 0, 360 ); 19 20 // start at 0 and sweep 110 degrees 21 g.setColor( Color.RED ); 22 g.drawRect( 100, 35, 80, 80 ); 23 g.setColor( Color.BLACK ); 24 g.drawArc( 100, 35, 80, 80, 0, 110 ); 25 26 // start at 0 and sweep -270 degrees 27 g.setColor( Color.RED ); 28 g.drawRect( 185, 35, 80, 80 ); 29 g.setColor( Color.BLACK ); 30 g.drawArc( 185, 35, 80, 80, 0, -270 ); 31 32 // start at 0 and sweep 360 degrees 33 g.fillArc( 15, 120, 80, 40, 0, 360 ); 34 35 // start at 270 and sweep -90 degrees 36 g.fillArc( 100, 120, 80, 40, 270, -90 ); 37 38 // start at 0 and sweep -270 degrees 39 g.fillArc( 185, 120, 80, 40, 0, - 270 ); 40 } // end method paintComponent 41 } // end class ArcsJPanel

Figure 12.25. Creating JFrame to display arcs.

(This item is displayed on pages 618 - 619 in the print version)

1 // Fig. 12.25: DrawArcs.java 2 // Drawing arcs. 3 import javax.swing.JFrame; 4 5 public class DrawArcs 6 { 7 // execute application 8 public static void main( String args[] ) 9 { 10 // create frame for ArcsJPanel 11 JFrame frame = new JFrame( "Drawing Arcs" ); 12 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 13 14 ArcsJPanel arcsJPanel = new ArcsJPanel(); // create ArcsJPanel 15 frame.add( arcsJPanel ); // add arcsJPanel to frame 16 frame.setSize( 300, 210 ); // set frame size 17 frame.setVisible( true ); // display frame 18 } // end main 19 } // end class DrawArcs  

Категории