我有一堂课(我会以非常简单的方式说)
Window 正在绘制图形,而 Mover 正在更改它们的坐标(x,y),我不想Window
在 Mover 移动它时读取图形的坐标。
class Figure{
int x, int y;
Figure(){...}
void move(int x, int y){ //when mover is moving by this method
this.x+=x;
this.y+=y;
}
void draw(Graphics g){ //i do not want this method running
g.draw(x,y); //I USE x,y here
}
}
然后我有Mover
修改x,y
的类Figure
class Mover extends Thread{
Figure f;
Mover(Figure f){
this.f = f;
}
public void run(){
while(true){f.move(3,4);}
Thread.sleep(30);
//
}
}
最后
class Window extends JFrame(){
ArrayList<Figure> l;
public void paint(Graphics g){
while(true){
foreach
l.draw();
}
}
}