-1

I am trying to paint circles onto my panel but the colour of the circle is determined by some parameters. Firstly the circles should be painted as white then go into a for loop checking which paramater matches and should paint the circle in that colour. the location of the circles are stored in an array. my code i have done so far does not work. i am clearly doing something wrong but i am new to java and coding so i am very stuck. if someone could show me how to edit/change my code i would really appreciate it. ArrayList circlesT is an arraylist of the circles locations and temp is the array with values i have the parameters too.

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

    public void drawShapes(Graphics g, final ArrayList<Shape> circlesT) {
        final 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));
        }    
        Timer timer = new Timer();
        TimerTask t;
        t = new TimerTask() {
            @Override
            public void run() {
                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));
                    }                 
                }                    
            }
        };
        //repaint();
        timer.schedule(t, 0, 1000);    
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2133166
  • 3
  • 1
  • 4

1 Answers1

3

A few points.

  • You shouldn't keep hold of the Graphics object from paintComponent beyond the end of the method invocation.
  • In general you should use javax.swing.Timer instead of java.util.Timer.
  • What you should actually be doing in the timer task/action is to update the state of your data and call repaint.
  • The code doing all the painting should be called in paintComponent before it returns.
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • could you please show me using the code i supplied. sorry i am a bit confused as what to do. this is due in on thursday and getting a bit worried. – user2133166 Mar 04 '13 at 19:49
  • @user2133166: why not try to implement his suggestions yourself first, and then if you have trouble with your implementation, show us what you've done and we can help you better. Please leave all mention of due date out of your correspondence here as it has nothing to do with the Java question that you're asking and only serves to distract. 1+ to Tom H. for his excellent answer! – Hovercraft Full Of Eels Mar 04 '13 at 22:14
  • i dont understand. the paint component class calls the drawShapes method, is this a wrong form of doing this. do i need to put this code in the actual paint component class? if i was to take the timer out my code will work and it will draw the circles but all at the same time. i wanted to add a timer so that its stops for say 2 seconds after a loop then goes into the loop again – user2133166 Mar 04 '13 at 23:27
  • @user2133166: there is no "paint component" class, unless you have classes you're not showing us. Have you gone through the Java Swing graphics and Swing Timer tutorials? There the best resources for learning how to use these tools. – Hovercraft Full Of Eels Mar 04 '13 at 23:31
  • sorry i meant the paint component method. – user2133166 Mar 04 '13 at 23:35
  • @user2133166: Your Swing Timer changes fields of the class and calls `repaint()` triggering the `paintComponent(...)` method override to be called. The `paintComponent(...)` method then uses the state of the class fields to decide what to draw and where. Again check the tutorials and also search this site for examples of Swing animation (some written by me) to see more. – Hovercraft Full Of Eels Mar 04 '13 at 23:57
  • 1
    Some of my examples of Swing animation with a timer: [example](http://stackoverflow.com/a/12545773/522444), [example](http://stackoverflow.com/a/11130842/522444), [example](http://stackoverflow.com/a/13983812/522444). – Hovercraft Full Of Eels Mar 05 '13 at 00:25