ptg7068951
338 HOUR 23:Creating Java2D Graphics
Listing 23.1 defines a PiePanelclass in lines 1–54 and a PieSlicehelper
class in lines 56–64. The PiePanelclass can be used as a component in any
Java program’s GUI. To test PiePanel, you need to create a class that uses it.
Listing 23.2 contains an application that uses these panels, PieFrame.
Create a new empty Java file and enter the source code for this class from
the listing.
LISTING 23.2 The Full Text of PieFrame.java
1: importjavax.swing.*;
2: importjavax.swing.event.*;
3: importjava.awt.*;
4:
5: public classPieFrame extendsJFrame {
6: Color uneasyBeingGreen= new Color(0xCC, 0xCC, 0x99);
7: Color zuzusPetals= new Color(0xCC, 0x66, 0xFF);
8: Color zootSuit= new Color(0x66, 0x66, 0x99);
9: Color sweetHomeAvocado= new Color(0x66, 0x99, 0x66);
10: Color shrinkingViolet= newColor(0x66, 0x66, 0x99);
11: Color miamiNice= new Color(0x33, 0xFF, 0xFF);
12: Color inBetweenGreen= new Color(0x00, 0x99, 0x66);
13: Color norwegianBlue= new Color(0x33, 0xCC, 0xCC);
14: Color purpleRain= newColor(0x66, 0x33, 0x99);
15: Color freckle= newColor(0x99, 0x66, 0x33);
16:
17: publicPieFrame() {
18: super(“Pie Graph”);
19: setLookAndFeel();
20: setSize(320, 290);
21: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22: setVisible(true);
23:
24: PiePanel pie = new PiePanel(10);
25: pie.addSlice(uneasyBeingGreen, 1337);
26: pie.addSlice(zuzusPetals, 1189);
27: pie.addSlice(zootSuit, 311);
28: pie.addSlice(sweetHomeAvocado, 246);
29: pie.addSlice(shrinkingViolet, 203);
30: pie.addSlice(miamiNice, 187);
31: pie.addSlice(inBetweenGreen, 166);
32: pie.addSlice(norwegianBlue, 159);
33: pie.addSlice(purpleRain, 139);
34: pie.addSlice(freckle, 127);
35: add(pie);
36: }
37:
38: private voidsetLookAndFeel() {
39: try {
40: UIManager.setLookAndFeel(
41: “com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
42: );