【问题标题】:Animation of moving arraylist objects移动数组列表对象的动画
【发布时间】:2018-11-18 12:31:29
【问题描述】:

我正在制作动画。汽车移动和波浪正在它周围产生。 Waves 是 ArrayList 对象。我希望波浪看起来像这样:

我希望它们在汽车前部彼此靠近,而在后部彼此远离。

有了这个代码,波形看起来像这样:

(它们在后面更靠近,而不是在前面)

public void actionPerformed(ActionEvent e) {        
    CarParametrs  pa = pAuto;
    pa.xPos += pa.velX;

    HumanParametrs pc = pHuman;
    pc.xPos += pc.velX;
    synchronized (waves) {
        for (WaveParameters w : waves) {
            if(pa.velX==0 && pc.velX==0)
            {
                w.xPos = pa.xPos+50;
                w.height+=20; 
                w.width+=20; 
                w.yPos-=20/5 ; 
            }
            else{
            w.xPos = pa.xPos+50;
            w.height+=pa.velX; 
            w.width+=pa.velX; 
            for (WaveParameters ww : waves) {
                ww.xPos-=5; //for ww.xPos +5; they appear not next to each other
                ww.width+=1;

            }

            w.yPos-=pa.velX/5 ; 
            }
        }
    }

    SwingUtilities.invokeLater(()->repaint());

}

你能告诉我,如何解决这个问题吗? 整个班级是这样的:

public class PanelAnimation extends JPanel implements ActionListener{

public PanelAnimation(ResourceBundle bundle) {
    super();
    resourceBundle = bundle;

    try {                
        imageBackground = ImageIO.read(new File("bg.png"));
       } catch (IOException ex) {
            // handle exception...
       }    
}
CarParametrs pAuto = new CarParametrs();
HumanParametrs pHuman = new HumanParametrs() ;
ArrayList<WaveParameters> waves = new ArrayList<WaveParameters>();
Timer t = new Timer(60,this);
Timer draw; 
public void addAuto(){
    CarParametrs ap = new CarParametrs();
    ap.setX(0);
    pAuto = ap;
}
public void addHuman(){
    HumanParametrs acz = new HumanParametrs();
    acz.setX(0);
    pHuman = acz;
}

public void animationStart() {
    t.start();
}

public void animationStop() {
    t.stop();
}
public void addTimerAddingWaves(){
    if(draw==null) {
        draw= new Timer(300, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    WaveParameters wave = new WaveParameters(); 
                    waves.add(wave); 

                }
            });

        draw.start();
    }
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(imageBackground, 0, 0, null);
    pAuto.paint(g);
    pHuman.paint(g);
    synchronized (waves) {
        for (WaveParameters w : waves) {
            w.paint(g);
        }
    }   
}


public void actionPerformed(ActionEvent e) {        
    CarParametrs  pa = pAuto;
    pa.xPos += pa.velX;

    HumanParametrs pc = pHuman;
    pc.xPos += pc.velX;
    synchronized (waves) {
        for (WaveParameters w : waves) {
            if(pa.velX==0 && pc.velX==0)
            {
                w.xPos = pa.xPos+50;
                w.height+=20; 
                w.width+=20; 
                w.yPos-=20/5 ; 
            }
            else{
            w.xPos = pa.xPos+50;
            w.height+=pa.velX; 
            w.width+=pa.velX; 
            for (WaveParameters ww : waves) {
                ww.xPos-=5;
                ww.width+=1;
            }
            w.yPos-=pa.velX/5 ; 
            }
        }
    }
    SwingUtilities.invokeLater(()->repaint());
}

Color colorPanelAnimation;
TitledBorder titlePanelAnimation;
ResourceBundle resourceBundle;
private BufferedImage imageBackground;

public void stopTimerAddingWaves() {
    if(draw!=null) {
        draw.stop();
        draw=null;
    }
}

而 WaveParameters 类是:

public class WaveParameters {

int xPos=0;
int yPos = 375;
int width=60;
int height=60;
int velX = 5 ;
private Color color = Color.white;
public int getVelX() {
    return velX;
}

public void setVelX(int velX) {
    this.velX = velX;
}

public int getX() {
    return xPos;
}

public void setX(int xPos) {
    this.xPos = xPos;
}

public int getY() {
    return xPos;
}

public void setY(int yPos) {
    this.yPos = yPos;
}

public int getWidth(){
  return width;
} 

public int getHeight(){
  return height;
}

public void setWidth(int width) {
    this.width = width;
}

public void setHeight(int height) {
    this.height = height;
}

public Color getColor() {
    return color;
}
public void setColor(Color color) {
    this.color = color;
}

public void paint(Graphics g){
  g.setColor(getColor());
  g.drawOval(xPos,yPos,width/2,height/2);
}

}

【问题讨论】:

  • 不要进行图像重定向。阅读Minimal, Complete, and Verifiable example
  • 首先,w.xPos = pa.xPos+50; 行将重置每个波的位置(第二个循环中所做的不会生效,因为xPos 将被覆盖)。其次,你确定pAuto在处理波浪位置时没有被修改吗?
  • @SergioLema 根据第一 - 你到底是什么意思?确实,在汽车周围添加了下一波,但是在 xPos = 0 时,有时会出现新的圆圈而不是消失,这是真的,但是如何解决呢?根据第二个 - 它没有被修改

标签: java for-loop animation arraylist graphics


【解决方案1】:

问题似乎是在设置波浪的 x 位置时出现的。我们来描述一下:

  • 第一次迭代:设置 wave1.xPos = auto.xPos + 50,然后迭代所有减少 5 的波
    • wave1.xPos = auto.xPos + 50 - 5
    • wave2.xPos = -5
    • wave3.xPos = -5
    • 等等
  • 第二次迭代:设置wave2.xPos = auto.xPos + 50,然后迭代所有波减少5
    • wave1.xPos = auto.xPos + 50 - 10
    • wave2.xPos = auto.xPos + 50 - 5
    • wave3.xPos = -10
    • 等等
  • 最后一次迭代:设置 waveLast.xPos = auto.xPos + 50,然后迭代所有波减少 5
    • wave1.xPos = auto.xPos + 50 - (5 * size)
    • wave2.xPos = auto.xPos + 50 - (5 * size-1)
    • wave3.xPos = auto.xPos + 50 - (5 * size-2)
    • 等等
    • waveLast.xPos = auto.xPos + 50 - 5

如何解决?

使用单个循环。

for (int i = 0; i < waves.size; i++) {
  if (pa.velX == 0 && pc.velX == 0) {
    w.xPos = pa.xPos + 50;
    w.height += 20; 
    w.width += 20; 
    w.yPos -= 20/5;
  } else {
    w.xPos = pa.xPos + 50 - (5 * i);
    w.height += pa.velX; 
    w.width += pa.velX + (i + 1);
    w.yPos -= pa.velX/5; 
  }
}

这将使第一波(最小的)更多在右侧,最后一波(最大的)在左侧。

【讨论】:

  • 其实还是一样——前面他们相距很远,几乎在车后的同一个地方。但它们只是比我的代码更宽
  • 我假设xPos是横轴,yPos是纵轴,不是吗?
  • 好的,您是否尝试过设置高度、宽度和 yPos 的绝对值,而不是在每个周期增加/减少它们?作为w.height = pa.velX + iw.width = pa.velX + iw.yPos = pa.velX/5。零速也一样。
  • 那么根本就没有波浪出现。我认为这个问题只是 `ww.xPos-=5;` 的问题,因为那个波浪看起来完全符合我的要求,但是倒置(前后)
  • 第一个迭代的wave,是最小的还是最大的?
猜你喜欢
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多