我的程序有问题。我正试图在火车上移动火车,这些列车用椭圆表示。当我尝试移动这些对象时,它们会在新位置绘制,但它们也会在之前的位置绘制。
trensPretos是trem类的链接列表:
'public LinkedList trensPretos = new LinkedList();'
这是我的代码:
public void AtualizaTrensPretos()
{
int currentX;
int currentY;
// trens pretos se movem
for (Trem t:trensPretos)
{
while(t.get_x() < 1100)
{
currentX = t.get_x();
currentY = t.get_y();
t.setNew_x(currentX + moveX);
// antes da linha reta
if (t.get_x() < 270)
{
t.setNew_y(currentY + moveY);
}
else if(t.get_x() > 730)
{// depois da linha reta
t.setNew_y(currentY - (moveY+1));
}
setChanged();
notifyObservers(t);
}
// removo o trem após ele passar pelo cruzamento
// trensPretos.remove(t);
}
}
// Observer
// recebo trem e desenho
g2d = (Graphics2D) this.getGraphics();
if (arg instanceof Trem)
{
if (g2d != null)
{
g2d.setColor(((Trem) arg).getColor());
g2d.fill(((Trem) arg).getEllipse());
}
}
// Trem class
公共课堂Trem {
private int posX;
private int posY;
private Color corTrem;
private Ellipse2D formaDoTrem;
private int sizeX = 30;
private int sizeY = 30;
public Trem(Color corDoTrem)
{
formaDoTrem = new Ellipse2D.Double();
this.corTrem = corDoTrem;
}
public Color getColor()
{
return this.corTrem;
}
public void setNew_x(int x)
{
this.posX = x;
}
public void setNew_y(int y)
{
this.posY = y;
}
public int get_x()
{
return this.posX;
}
public int get_y()
{
return this.posY;
}
public Ellipse2D getEllipse()
{
this.formaDoTrem.setFrame(posX, posY, sizeX, sizeY);
return this.formaDoTrem;
}
}
可能是什么问题?
答案 0 :(得分:1)
它们被绘制在新位置,但它们也被绘制在之前的位置。
在重新喷漆之前,您需要先清除背景区域。