/*-------------------------------------------------------------------* * NTU Student: Java With Engineering Applications, Spring 1999 * Assignment # 4 - Java How To Program 2nd edition, Deitel & Deitel * Problem 9.35 on page 515 - CLASS Pie applet created to practice * drawing arcs and filling them in in order to build a Pie Chart. * * Michael Kusmierz * Xerox Corporation * Rev 1.0 May 8, 1999 * * This code represents a students work in progress * and is not to be used beyond that purpose. *-------------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; /** * This type was created in VisualAge. */ public class Pie extends Applet implements ActionListener { private TextField ivjTextField1 = null; private TextField ivjTextField2 = null; private TextField ivjTextField3 = null; private TextField ivjTextField4 = null; private Label ivjLabel1 = null; private Label ivjLabel2 = null; //--- instance variables for four user inputs to be graphed (positive) long lngInput1; long lngInput2; long lngInput3; long lngInput4; //--- total of all inputs (must be positive) private float fltTotal; //--- define the location and size of the chart private final int CHART_CENTER_X = 150; private final int CHART_CENTER_Y = 50; private final int CHART_WIDTH = 100; private final int CHART_HEIGHT = 100; /** * This method was created in VisualAge. * @param e ActionEvent */ public void actionPerformed(ActionEvent e) { getInput(); repaint(); } /******************************************************************************* * This actually draws a quad pie chart at the given location and given radius */ private void drawPieChart(Graphics g, int x, int y, int arcWidth, int arcHeight) { Color c; int startAngle,arcAngle; float fltNumberEntered = 0; //--- draw a circle c = Color.black; g.setColor( c ); arcAngle = 360; startAngle=0; g.drawArc(x-1,y-1,arcWidth+1,arcHeight+1,startAngle,arcAngle); if (fltTotal > 0) { // Prevent devide by zero //--- draw the 4 arcs that make up 360 degrees c = Color.blue; g.setColor( c ); fltNumberEntered = (float) lngInput1; arcAngle = (int) (360 * (fltNumberEntered / fltTotal)); startAngle=0; g.fillArc(x,y,arcWidth,arcHeight,startAngle,arcAngle); c = Color.red; g.setColor( c ); startAngle = startAngle+ arcAngle; fltNumberEntered = (float)lngInput2; arcAngle = (int) (360 * (fltNumberEntered / fltTotal)); g.fillArc(x,y,arcWidth,arcHeight,startAngle,arcAngle); c = Color.green; g.setColor( c ); startAngle = startAngle + arcAngle; fltNumberEntered = (float)lngInput3; arcAngle = (int) (360 * (fltNumberEntered / fltTotal)); g.fillArc(x,y,arcWidth,arcHeight,startAngle,arcAngle); //--- the last arc must fill the remainder of the circle c = Color.cyan; g.setColor( c ); startAngle = startAngle + arcAngle; arcAngle = 360 - startAngle; g.fillArc(x,y,arcWidth,arcHeight,startAngle,arcAngle); } } /** * Gets the applet information. * @return java.lang.String */ public String getAppletInfo() { return "Pie created using VisualAge for Java."; } /******************************************************************************* * This method validates that text entries are valid numbers and positive */ private void getInput() { int intCnt = 0; try { showStatus(" AN INTEGER NUMBER. Use TAB to move to the next box."); intCnt++; lngInput1 = Math.abs(Long.parseLong(ivjTextField1.getText())); intCnt++; lngInput2 = Math.abs(Long.parseLong(ivjTextField2.getText())); intCnt++; lngInput3 = Math.abs(Long.parseLong(ivjTextField3.getText())); intCnt++; lngInput4 = Math.abs(Long.parseLong(ivjTextField4.getText())); fltTotal = (float) (lngInput1 + lngInput2 + lngInput3 + lngInput4); } catch (Throwable exception) { showStatus("INVALID ENTRY NUMBER " + intCnt); } } /** * Return the Label1 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel1() { if (ivjLabel1 == null) { try { ivjLabel1 = new java.awt.Label(); ivjLabel1.setName("Label1"); ivjLabel1.setText(" any four numbers and see the pie chart fill using method fillArc "); ivjLabel1.setBounds(6, 167, 411, 18); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel1; } /** * Return the Label2 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel2() { if (ivjLabel2 == null) { try { ivjLabel2 = new java.awt.Label(); ivjLabel2.setName("Label2"); ivjLabel2.setFont(new java.awt.Font("dialog", 1, 12)); ivjLabel2.setText("Class Pie - p515 problem 9.35 in Deitel & Deitel "); ivjLabel2.setBounds(20, 12, 389, 23); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel2; } /** * Return the TextField1 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField1() { if (ivjTextField1 == null) { try { ivjTextField1 = new java.awt.TextField(); ivjTextField1.setName("TextField1"); ivjTextField1.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField1.setBounds(53, 192, 40, 23); ivjTextField1.setForeground(java.awt.Color.blue); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField1; } /** * Return the TextField2 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField2() { if (ivjTextField2 == null) { try { ivjTextField2 = new java.awt.TextField(); ivjTextField2.setName("TextField2"); ivjTextField2.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField2.setBounds(146, 192, 40, 23); ivjTextField2.setForeground(java.awt.Color.red); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField2; } /** * Return the TextField3 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField3() { if (ivjTextField3 == null) { try { ivjTextField3 = new java.awt.TextField(); ivjTextField3.setName("TextField3"); ivjTextField3.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField3.setBounds(239, 192, 40, 23); ivjTextField3.setForeground(java.awt.Color.green); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField3; } /** * Return the TextField4 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField4() { if (ivjTextField4 == null) { try { ivjTextField4 = new java.awt.TextField(); ivjTextField4.setName("TextField4"); ivjTextField4.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField4.setBounds(332, 192, 40, 23); ivjTextField4.setForeground(java.awt.Color.cyan); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField4; } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(Throwable exception) { /* Uncomment the following lines to print uncaught exceptions to stdout */ // System.out.println("--------- UNCAUGHT EXCEPTION ---------"); // exception.printStackTrace(System.out); } /******************************************************************************* * Handle the Applet init method. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ public void init() { super.init(); try { setName("Pie"); setLayout(null); setSize(426, 240); add(getTextField1(), getTextField1().getName()); add(getTextField2(), getTextField2().getName()); add(getTextField3(), getTextField3().getName()); add(getTextField4(), getTextField4().getName()); add(getLabel1(), getLabel1().getName()); add(getLabel2(), getLabel2().getName()); // user code begin {1} //--- accept entry from any text field ivjTextField1.addActionListener(this); ivjTextField2.addActionListener(this); ivjTextField3.addActionListener(this); ivjTextField4.addActionListener(this); //--- initialize entry fields ivjTextField1.setText("1"); ivjTextField2.setText("2"); ivjTextField3.setText("3"); ivjTextField4.setText("4"); // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } /** * main entrypoint - starts the part when it is run as an application * @param args java.lang.String[] */ public static void main(java.lang.String[] args) { try { Frame frame; try { Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame"); frame = (Frame)aFrameClass.newInstance(); } catch (java.lang.Throwable ivjExc) { frame = new Frame(); } Pie aPie; Class iiCls = Class.forName("Pie"); ClassLoader iiClsLoader = iiCls.getClassLoader(); aPie = (Pie)java.beans.Beans.instantiate(iiClsLoader,"Pie"); frame.add("Center", aPie); frame.setSize(aPie.getSize()); frame.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of java.applet.Applet"); exception.printStackTrace(System.out); } } /******************************************************************************* * Paint is used to test the draw PieChart method */ public void paint(Graphics g) { drawPieChart( g, CHART_CENTER_X, CHART_CENTER_Y, CHART_WIDTH, CHART_HEIGHT ); } }