我的程序有问题。我试着在轨道上移动火车,这些火车用椭圆表示。当我尝试移动这些对象时,它们被绘制在新的位置,但它们也被绘制在以前的位置。
trensPretos是trem类的链表:
‘'public 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类
公共类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;
}
}
可能的问题是什么?
发布于 2013-12-16 01:42:51
它们是在新位置绘制的,但也是在以前的位置绘制的。
您需要先清除背景区域,然后再重新绘制列车。
https://stackoverflow.com/questions/20597460
复制相似问题