0

my program currently reads from a text file and paints different coloured circles depending on the next value in the array. at the moment i am reading the first 10 values in the array so there should be 10 painted circles. These circles all paint at the same time, but i want them to paint one by one, so for example a two second interval between each painted circle. The code below is the section which does the reading of the values and the painting of the circles. can someone please help me. Im very confused on where and how to add the timer.

public void paintComponent(Graphics g) {
        drawShapes(g, circlesT);
    }

    public void drawShapes(Graphics g, ArrayList<Shape> circlesT) {
        Graphics2D ga = (Graphics2D) g;
        ga.drawImage(newImage, 0, 0, null);



        for (int i = 0; i < circlesT.size(); i++) {
            ga.draw(circlesT.get(i));
            ga.setPaint(Color.white);
            ga.fill(circlesT.get(i));
        }


        for (int i = 0; i < 10; i++) {


            if (read.temp.get(i) < 31 && read.temp.get(i) > 30) {
                ga.draw(circlesT.get(i));
                ga.setPaint(Color.green);
                ga.fill(circlesT.get(i));
            } else if (read.temp.get(i) < 32 && read.temp.get(i) > 31) {
                ga.draw(circlesT.get(i));
                ga.setPaint(Color.red);
                ga.fill(circlesT.get(i));
            } else if (read.temp.get(i) < 33 && read.temp.get(i) > 32) {
                ga.draw(circlesT.get(i));
                ga.setPaint(Color.yellow);
                ga.fill(circlesT.get(i));
            }
        }

    }
Vinay Lodha
  • 2,185
  • 20
  • 29
  • Each call to paint is a request to update the screen. While you're in the paint method, nothing is getting painted to the screen. Take a look at [this](http://stackoverflow.com/questions/13849184/painting-on-jframe-without-extending/13849793#13849793) or [this](http://stackoverflow.com/questions/14456072/i-am-trying-to-move-a-ball-in-applet-using-thread-but-its-not-moving/14456808#14456808) or [this](http://stackoverflow.com/questions/13548982/displaying-contents-of-string-array-in-swing-component-as-iterations-using-time/13549338#13549338) for some animato examples – MadProgrammer Feb 19 '13 at 10:48
  • You should also be calling super.paintComponent(g) - this is very important – MadProgrammer Feb 19 '13 at 11:20

1 Answers1

0

Having a timer or sleep in a painting-method is not a good idea. This will block the whole application and confuse users.

You could use an off-screen image that is build step by step in another thread, and simply display that image in the paint method.

The general layout of your code could be like:

BufferedImage img = new BufferedImage(...);
ActionListener taskPerformer = new ActionListener() {
    int currentIteration = 0;
    public void actionPerformed(ActionEvent evt) {
        drawShapes(currentIteration++);
        invalidate();
        repaint();
        if (currentIteration >= maxIterations) {
            timer.stop();
        }
    }
}
timer = new Timer(2000, taskPerformer);
timer.start();

In the drawShapes method you would paint the circles up to i onto the image img. After that, the calls to invalidate and repaint cause paintComponent to be called. There, you just use drawImage to put the content of img on the screen

king_nak
  • 11,313
  • 33
  • 58
  • 1- You're violating the single thread rules of Swing by updating the UI outside the EDT 2- I'm not sure what update is, but its not a method within any Swing or AWT component. You should be using a javax.swing.Timer – MadProgrammer Feb 19 '13 at 10:51
  • Thank you for your response. where you have the buffered img i dont understand exactly what i would put in the brackets? would i have this code outside of paint component? – user1961019 Feb 20 '13 at 10:45
  • `img` will be a member of your paint component, you'll need it in `drawShapes` (to draw on) and in `paintComponent` (to copy it to the screen). Its size will most probably be the same of your component... For type you can simply choose `TYPE_3BYTE_BGR` – king_nak Feb 20 '13 at 18:15