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
 

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

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