import javax.swing.*; import java.awt.*; public class PRJ04 extends JFrame { public static void main (String [] args) { PRJ04 frmApp = new PRJ04(); PanelChart pnlChart = new PanelChart(); PanelPopulationInputs pnlInputs = new PanelPopulationInputs(); pnlInputs.setPnlChart(pnlChart); frmApp.add(pnlInputs, BorderLayout.WEST); frmApp.add(pnlChart, BorderLayout.EAST); frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmApp.setSize(600,500); frmApp.setVisible(true); } }import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Created by malon_000 on 5/11/14. */ public class PanelPopulationInputs extends JPanel implements ActionListener { private JLabel lblBoston; private JLabel lblMiami; private JLabel lblDallas; private JLabel lblStLouis; private JLabel lblKansasCity; private JLabel lblLosAngeles; private JLabel lblSeattle; private JTextField txtBoston; private JTextField txtMiami; private JTextField txtDallas; private JTextField txtStLouis; private JTextField txtKansasCity; private JTextField txtLosAngeles; private JTextField txtSeattle; private JButton btnChart; private PanelChart pnlChart; public void setPnlChart(PanelChart p) { pnlChart = p; } public PanelPopulationInputs() { lblBoston = new JLabel("Boston:"); lblMiami = new JLabel("Miami:"); lblDallas = new JLabel("Dallas:"); lblStLouis = new JLabel("St Louis:"); lblKansasCity = new JLabel("Kansas City:"); lblLosAngeles = new JLabel("Los Angeles:"); lblSeattle = new JLabel("Seattle:"); txtBoston = new JTextField(7); txtMiami = new JTextField(7); txtDallas = new JTextField(7); txtStLouis = new JTextField(7); txtKansasCity = new JTextField(7); txtLosAngeles = new JTextField(7); txtSeattle = new JTextField(7); btnChart = new JButton("Chart"); btnChart.setActionCommand("chart"); btnChart.addActionListener(this); setLayout(new GridLayout(8,2)); add(lblBoston); add(txtBoston); add(lblMiami); add(txtMiami); add(lblDallas); add(txtDallas); add(lblStLouis); add(txtStLouis); add(lblKansasCity); add(txtKansasCity); add(lblLosAngeles); add(txtLosAngeles); add(lblSeattle); add(txtSeattle); add(btnChart); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("chart")) { pnlChart.setPopulations(Integer.parseInt(txtBoston.getText()), Integer.parseInt(txtMiami.getText()), Integer.parseInt(txtDallas.getText()), Integer.parseInt(txtStLouis.getText()), Integer.parseInt(txtKansasCity.getText()), Integer.parseInt(txtLosAngeles.getText()), Integer.parseInt(txtSeattle.getText())); } } }import javax.swing.*; import java.awt.*; /** * Created by malon_000 on 5/11/14. */ public class PanelChart extends JPanel { private double boston; private double miami; private double dallas; private double stLouis; private double kansasCity; private double losAngeles; private double seattle; private double total; private Graphics g; public void paintComponent (Graphics g) { super.paintComponent(g); this.g = g; drawChart(); } public void setPopulations( int boston, int miami, int dallas, int stLouis, int kansasCity, int losAngeles, int seattle) { this.boston = boston; this.miami = miami; this.dallas = dallas; this.stLouis = stLouis; this.kansasCity = kansasCity; this.losAngeles = losAngeles; this.seattle = seattle; total = this.boston + this.miami + this.dallas + this.stLouis + this.kansasCity + this.losAngeles + this.seattle; repaint(); } public void drawChart() { int pctBoston = (int) (boston/total*36000); int pctMiami = (int) (miami/total*36000); int pctDallas = (int) (dallas/total*36000); int pctStLouis = (int) (stLouis/total*36000); int pctKansasCity = (int) (kansasCity/total*36000); int pctLosAngeles = (int) (losAngeles/total*36000); int pctSeattle = (int) (seattle/total*36000); int angle = 0; g.setColor(Color.magenta); g.fillArc(10, 10, 50, 50, angle, pctBoston); angle += pctBoston; g.setColor(Color.yellow); g.fillArc(10, 10, 50, 50, angle, pctMiami); angle += pctMiami; g.setColor(Color.gray); g.fillArc(10, 10, 50, 50, angle, pctDallas); angle += pctDallas; g.setColor(Color.green); g.fillArc(10, 10, 50, 50, angle, pctStLouis); angle += pctStLouis; g.setColor(Color.red); g.fillArc(10, 10, 50, 50, angle, pctKansasCity); angle += pctKansasCity; g.setColor(Color.pink); g.fillArc(10, 10, 50, 50, angle, pctLosAngeles); angle += pctLosAngeles; g.setColor(Color.blue); g.fillArc(10, 10, 50, 50, angle, pctSeattle); } }
When I comment out the adding and setting of the pnlChart on my main driver, the pnlPopulationInputs shows up fine, and it runs ok. When I add the pnlChart I get errors like crazy and a white screen. My errors:
Once more with this one, I refer back to our in class example. Our programs are set up the same, yet he has no issues with the "/ by zero" exception.Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
at PanelChart.drawChart(PanelChart.java:45)
at PanelChart.paintComponent(PanelChart.java:24)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.ja va:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.ja va:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 85)
at javax.swing.JComponent.paintChildren(JComponent.ja va:887)
at javax.swing.JComponent.paintToOffscreen(JComponent .java:5226)
at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(RepaintManager.java:1529)
at javax.swing.RepaintManager$PaintManager.paint(Repa intManager.java:1452)
at javax.swing.RepaintManager.paint(RepaintManager.ja va:1249)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(Graphi csCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGra phicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraph icsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager$3.run(RepaintManager.ja va:819)
at javax.swing.RepaintManager$3.run(RepaintManager.ja va:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(Re paintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintMana ger.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run( RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
at java.awt.EventQueue.access$200(EventQueue.java:103 )
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)