Java JPanel清除
Java JPanel clear
我需要创建一个 2d 模拟,但是在设置新的 "frame" 时旧的不会被清除。
我想要一些 Circles 在 Arena 中移动,并且每个循环都应该删除旧的圆圈并产生新的圆圈。一切正常,但旧的没有被清除并且仍然可见,这就是我需要改变的。
游戏循环
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
for(Schleimpilz s : this.pilz){
s.move();
fenster.getArena().repaint();;
fenster.getArena().paintPilz();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
那就是竞技场 Class(扩展 Jpanel),需要清除 Jpanel 的地方
public void paintPilz() {
//how to clear the old values here?
super.paintComponent(graphics);
setBackground(Color.WHITE);
for(Schleimpilz s : schleimpilz){
printNewSchleimpilz(s.getLocX(), s.getLocY());
}
}
您应该在 paintComponent 方法中完成所有渲染。这就是旧形状如何被移除(重绘时)。
要制作动画/创建新圈子,您应该使用 javax.swing.Timer 而不是 Thread.sleep
new Timer(10, new ActionListener() {
s.move();
fenster.getArena().repaint();
});
我需要创建一个 2d 模拟,但是在设置新的 "frame" 时旧的不会被清除。
我想要一些 Circles 在 Arena 中移动,并且每个循环都应该删除旧的圆圈并产生新的圆圈。一切正常,但旧的没有被清除并且仍然可见,这就是我需要改变的。
游戏循环
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
for(Schleimpilz s : this.pilz){
s.move();
fenster.getArena().repaint();;
fenster.getArena().paintPilz();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
那就是竞技场 Class(扩展 Jpanel),需要清除 Jpanel 的地方
public void paintPilz() {
//how to clear the old values here?
super.paintComponent(graphics);
setBackground(Color.WHITE);
for(Schleimpilz s : schleimpilz){
printNewSchleimpilz(s.getLocX(), s.getLocY());
}
}
您应该在 paintComponent 方法中完成所有渲染。这就是旧形状如何被移除(重绘时)。
要制作动画/创建新圈子,您应该使用 javax.swing.Timer 而不是 Thread.sleep
new Timer(10, new ActionListener() {
s.move();
fenster.getArena().repaint();
});