我想畫圓到我的面板上,但圓的顏色是由一些參數決定的。首先,應將圓塗成白色,然後進入for循環,檢查參數是否匹配,並應以該顏色繪製圓。圓圈的位置存儲在一個數組中。我迄今爲止所做的代碼不起作用。我顯然做錯了,但我是新來的Java和編碼,所以我很困難。如果有人能告訴我如何編輯/更改我的代碼,我會非常感激。 ArrayList circlesT是圓圈位置的數組列表,temp也是數組,其值也是我的參數。定時器與油漆組件
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);
}
*「週四到期」*太本地化。 – 2013-03-04 21:52:17