1

I have a sample code that is simply to build a JFrame with a rectangle and a few buttons. I completed building the rectangles and now i'm onto placing two buttons, one start - top and one stop - bottom.

I have everything working, at least the science of it. However when i'm trying to set the start button to run the code nothing happens. I tried to see if there was an error by creating a JFrame and the code was successful. The JFrame is supposed to open with a start button that initiates the paintComponent() and the stop terminates the entire thing.

Is there anyone who could possible provide abit of guidance, i havn't slept for days trying to figure this outt.

    public static void main (String[] args){
        TwoButtonsRandomRec two = new TwoButtonsRandomRec();
        two.go();
    }

    public void go(){

        JPanel pan = new JPanel(new GridBagLayout());

        START = new JButton("START");
        START.addActionListener(new StartListener());
        STOP = new JButton("STOP");
        STOP.addActionListener(new StopListener());

        pan.add(START);
        pan.add(STOP);

        frame = new JFrame();
        frame.getContentPane().add(BorderLayout.NORTH, START);
        frame.getContentPane().add(BorderLayout.SOUTH, STOP);
        frame.setSize(500,500);       
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void GUI(){
        JFrame frame2 = new JFrame();
        frame2.setSize(500,500);       
        frame2.setVisible(true);
    }
    class StartListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
                //frame.getContentPane().add(new DrawPanel());
                //System.exit(0);
                //
            DrawPanel panel = new DrawPanel();

         }
    }

    class StopListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
               System.exit(0);
        }
    }

    /*
     * Panel created
     * rectangle drawn to random sizes
     */
    class DrawPanel extends JPanel{
        public void paintComponent(Graphics g){
                ran = new Random();
                g.setColor(new Color(ran.nextInt(255),+
                ran.nextInt(255),ran.nextInt(255)));
                height = ran.nextInt(getHeight());
                width = ran.nextInt(getWidth());
                x = ran.nextInt(getWidth()-width);
                y = ran.nextInt(getHeight()-height);
                g.fillRect(x,y,width,height);
                //repaint();
            try{
                Thread.sleep(240);
            }catch(InterruptedException ie){
            }   
            repaint();
        }

    }
}
laxonline
  • 2,657
  • 1
  • 20
  • 37

1 Answers1

2

This snippet is a killer:

class DrawPanel extends JPanel{
        public void paintComponent(Graphics g){
                ran = new Random();
                g.setColor(new Color(ran.nextInt(255),+
                ran.nextInt(255),ran.nextInt(255)));
                height = ran.nextInt(getHeight());
                width = ran.nextInt(getWidth());
                x = ran.nextInt(getWidth()-width);
                y = ran.nextInt(getHeight()-height);
                g.fillRect(x,y,width,height);
                //repaint();
            try{
                Thread.sleep(240);
            }catch(InterruptedException ie){
            }   
            repaint();
        }

    }
  1. Never, ever, call Thread.sleep(240); in the EDT
  2. Never call repaint(); from within paintComponent as this will create an infinite loop
  3. Initiating ran once is enough, don't re-instantiate it over and over in paintComponent
  4. The way you add your components to the frame is incorrect (Component, int) and not the other way around
  5. Use Java coding conventions, ie, variables and methods are camel-case and start with a lowercase letter.
  6. If you already add your buttons to a panel, then you only need to add that panel to your Frame and not your buttons. Otherwise it would mean that your panel is useless.
  7. Don't forget to super.paintComponent when you override paintComponent.

Whenever you need a component to be painted again (ie, somehow you want paintComponent() to be invoked), invoke repaint() on that component.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • 1
    @Timothy Edwards, And Lastly while you should never ever sleep on the EDT you should always get a good sleep yourself while trying to figure this out. – Extreme Coders Jan 15 '13 at 14:54
  • Thanks alot for replying, regarding the sleep, I only did that because I am not to familiar in the other techniques that would cause the rectangles to draw at a slower rate. Also is there anything I can do in the StartListener that would call the paintComponent, i've tried everything I could think of that when I select Start but nothing works. The Jframe opens, stop kills the program but start just acts as a button. I should mention before I put those buttons, the program would run with rectangles flipping through randomly. Apologise but I don't know what else to do.Honestly tried all I know. – Timothy Edwards Jan 15 '13 at 14:58
  • 2
    @TimothyEdwards Use a `javax.swing.Timer` and withing the `ActionListener` of that Timer, invoke `repaint()` on your `DrawPanel` instance (probably using something like: `DrawPanel.this.repaint();`). – Guillaume Polet Jan 15 '13 at 15:01
  • Hey thx for all the help, still got some more fiddling I guess. I tried the `DrawPanel.this.repaint();` really hoping it'll work but was unlucky. I configure some more. Oh I wanted to asked, I created another method just for the kix of it to see what would happen if you press start and had the start button pointing to a method that would create an external JFrame and funy it worked. I even tried placing the start button to exit and that work. Just doesn't want to pull from the paint component. – Timothy Edwards Jan 15 '13 at 16:49