简体   繁体   English

移动arraylist对象的动画

[英]Animation of moving arraylist objects

I am creating an animation. 我正在创建动画。 Car moves and waves are being created around it. 汽车移动并在其周围产生波浪。 Waves are ArrayList objects. Wave是ArrayList对象。 I want the waves to look like this: 我希望海浪看起来像这样:

在此处输入图片说明

I want them to be closer to each other at the front of car, and far from each other at the back. 我希望它们在汽车前部彼此靠近,而在后部彼此远离。

在此处输入图片说明

With this code waves look like this: 有了这些代码,波形看起来像这样:

(they are closer to each other at the back, not at the front) (它们在后面而不是在前面彼此靠近)

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());

}

Could you please tell me, how to solve this problem? 您能告诉我如何解决这个问题吗? Whole class looks like this: 全班看起来像这样:

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;
    }
}

And WaveParameters class is: 而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);
}

} }

The problem seems to come at when you set the x position of the wave. 设置wave的x位置时似乎出现了问题。 Let's describe it: 让我们描述一下:

  • First iteration: set wave1.xPos = auto.xPos + 50, then iterate over all waves decreasing 5 第一次迭代:设置wave1.xPos = auto.xPos + 50,然后迭代所有减小5的波
    • wave1.xPos = auto.xPos + 50 - 5 wave1.xPos = auto.xPos + 50-5
    • wave2.xPos = -5 wave2.xPos = -5
    • wave3.xPos = -5 wave3.xPos = -5
    • and so on 等等
  • Second iteration: set wave2.xPos = auto.xPos + 50, then iterate over all waves decreasing 5 第二次迭代:设置wave2.xPos = auto.xPos + 50,然后遍历所有减小5的波浪
    • wave1.xPos = auto.xPos + 50 - 10 wave1.xPos = auto.xPos + 50-10
    • wave2.xPos = auto.xPos + 50 - 5 wave2.xPos = auto.xPos + 50-5
    • wave3.xPos = -10 wave3.xPos = -10
    • and so on 等等
  • Last iteration: set waveLast.xPos = auto.xPos + 50, then iterate over all waves decreasing 5 最后一次迭代:设置waveLast.xPos = auto.xPos + 50,然后对所有减小5的波浪进行迭代
    • wave1.xPos = auto.xPos + 50 - (5 * size) wave1.xPos = auto.xPos + 50-(5 *大小)
    • wave2.xPos = auto.xPos + 50 - (5 * size-1) wave2.xPos = auto.xPos + 50-(5 * size-1)
    • wave3.xPos = auto.xPos + 50 - (5 * size-2) wave3.xPos = auto.xPos + 50-(5 * size-2)
    • and so on 等等
    • waveLast.xPos = auto.xPos + 50 - 5 waveLast.xPos = auto.xPos + 50-5

How to solve it? 怎么解决呢?

Use a single loop. 使用一个循环。

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; 
  }
}

This will make that the first wave (smallest one) more at the right, and the last wave (the biggest one) more at the left. 这将使第一个波浪(最小的波浪)在右侧更多,而最后一个波浪(最大的波浪)在左侧更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM