I have used a TimerTask to add a timer on a loop on one of my classes which is called by the paint component class. Basically there are white circles on the page already painted, there is then a for loop which reads a value from an array and depending on the range of the value the circle will change colour. each circle should represent the next value in the array. but its not working. I am a basic programmer and really don't understand whats going wrong. I would really appreciate if someone could give me a hand. here is my code so far:
public DoThePaint() {
String fileName;
fileName = "/Users/Desktop/test2.txt";
read = new Reader(fileName);
read.displayArrayList();
panel = new JPanel();
newImage = new ImageIcon(this.getClass().getResource("resource/background2T.png")).getImage();
circlesT = new ArrayList<Shape>();
circlesT.add(new Ellipse2D.Float(197.0f, 352.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(247.0f, 307.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(152.0f, 303.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(172.0f, 372.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(223.0f, 378.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(273.0f, 285.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(130.0f, 281.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(148.0f, 393.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(246.0f, 403.0f, 10.0f, 10.0f));
circlesT.add(new Ellipse2D.Float(297.0f, 264.0f, 10.0f, 10.0f));
}
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, 5000);
}