Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Baking a Pie Graph

. The height of the rectangle
. The point on the ellipse where the arc should begin (in degrees from
0 to 359)
. The size of the arc (also in degrees)
. The type of arc it is


The arc’s starting point and size range from 0 to 359 degrees in a counter-
clockwise direction, beginning with 0 degrees at the 3 o’clock position, as
shown in Figure 23.1.


The type of arc is specifiedusing class variables: PIEfor pie graph slices,
CLOSEDif the endpoints are connected with a straight line, and OPENif the
endpoints should not be connected.


The following statement draws an open arc at (100,50) that is 120 degrees
long, begins at the 30-degree mark, and has a width of 65 and a height of 75:


Arc2D.Float smile = new Arc2D.Float(100F, 50F, 65F, 75F,
30F, 120F, Arc2D.Float.OPEN);


Baking a Pie Graph


Todraw this hour to a close, you create PiePanel, a GUI component that
displays a pie graph. This component is a subclass of JPanel, a simple
Swing container that’s useful as a place to draw something.


One way to begin creating a class is to define the way objects of the class
are created. Programs that use the PiePanelclass must undertake the fol-
lowing steps:


. Create a PiePanelobject by using the constructor method
PiePanel(int). The integer specified as an argument is the number
of slices the pie graph contains.
. Call the object’s addSlice(Color, float)method to give a slice the
designated color and value.


The value of each slice in PiePanelis the quantity represented by that
slice.


For example, Table 23.1 displays data about the status of student loan
repayments in the United States for the first 38 years of the program,
according to the Office of Postsecondary Education.


90

180

90

180 0

270

How arcs are defined in degrees.
Free download pdf