Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: When I add a 2nd panel the result is all white

  1. #1
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default When I add a 2nd panel the result is all white

    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:
    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)
    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.
    Last edited by meni; May 11th, 2014 at 04:53 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: When I add a 2nd panel the result is all white

    Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
    at PanelChart.drawChart(PanelChart.java:45)
    Look at line 45 and find the divisor with the 0 value. Then backtrack in the code to see why the value is 0.

    Is the paintComponent() method called BEFORE the setPopulations() method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I add a 2nd panel the result is all white

    Quote Originally Posted by Norm View Post
    Look at line 45 and find the divisor with the 0 value. Then backtrack in the code to see why the value is 0.
    I actually got the white problem fixed. It's not throwing the exception anymore. Now it is just not showing up the second panel on the frame. I've edited the program in my original post to show the mistake I changed to its current state.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: When I add a 2nd panel the result is all white

    not showing up the second panel
    What is the variable name for the "second panel"?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I add a 2nd panel the result is all white

    Quote Originally Posted by Norm View Post
    What is the variable name for the "second panel"?
    pnlChart is the variable for it. It has been declared and added to the frame in the main program.

    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);

    As shown there

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: When I add a 2nd panel the result is all white

    Try debugging the code by adding a println() statement in that class's paintComponent() method that prints out the bounds of the component: the getBounds() method returns the value to print. The print out will show you the size and location of the panel.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I add a 2nd panel the result is all white

    Quote Originally Posted by Norm View Post
    Try debugging the code by adding a println() statement in that class's paintComponent() method that prints out the bounds of the component: the getBounds() method returns the value to print. The print out will show you the size and location of the panel.
    java.awt.Rectangle[x=574,y=0,width=10,height=461]
    I get this in my console. So if I am reading it right. The uppper left corner of that panel is at x=574 and y=0?

    If that's the case, then I need to get past the x value on my fillArc method?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: When I add a 2nd panel the result is all white

    I need to get past the x value on my fillArc method?
    I'm not sure what you are saying. What does "get past x value" mean?

    Did you see this: width=10,
    That's not very wide.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I add a 2nd panel the result is all white

    Quote Originally Posted by Norm View Post
    I'm not sure what you are saying. What does "get past x value" mean?
    Did you see this: width=10, That's not very wide.
    Yes, why is that? I am needing to put a pie chart on the second panel, or right side of the frame.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: When I add a 2nd panel the result is all white

    EAST is too small. Try CENTER
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    meni (May 11th, 2014)

  12. #11
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: When I add a 2nd panel the result is all white

    Quote Originally Posted by Norm View Post
    EAST is too small. Try CENTER
    Thank you again! I got the problem solved and it looks like the diagram he wanted.

Similar Threads

  1. wanting 2nd octet of an IP address
    By roofninja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 11th, 2012, 04:20 AM
  2. BufferedReader - Freezes on 2nd read
    By mds1256 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 14th, 2011, 03:26 PM
  3. Replies: 2
    Last Post: June 8th, 2010, 02:14 PM
  4. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 08:50 PM