【问题标题】:Java swing simple animationJava摇摆简单动画
【发布时间】:2014-04-19 00:41:26
【问题描述】:

我在使用一个简单的动画程序时遇到了问题(是的,这是家庭作业)。该问题要求我们在 500X500 的窗口上随机创建 100 个 Particle 类对象,并使用摆动计时器移动它们。这是我遇到的问题,正在创建的粒子似乎不是随机的,但它们沿对角轴对齐,我无法找到逻辑错误,非常感谢您的帮助。这是一个应该清楚的屏幕截图。 http://i57.tinypic.com/qswbix.jpg

我还将在帖子末尾发布作业问题,以防有人发现我的问题难以理解。谢谢你的帮助

public class ParticleFieldWithTimer extends JPanel{
    private ArrayList<Particle> particle = new ArrayList<Particle>();
    Timer timer; 
    boolean b; 
      public ParticleFieldWithTimer (){
       this.setPreferredSize(new Dimension(500,500));


    for(int i = 0; i < 100; i++) { 
        particle.add(new Particle());
    }


    timer = new Timer(40, new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent ae) {
            for (Particle p : particle) {                   
                p.move();                   
            }
            repaint();              
            }           
    });
    timer.start();  

}
   public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.RED);

        for (Particle p : particle) {
        double temp1 = p.getX();
        double temp2 = p.getX();
        int tempX = (int) temp1;
        int tempY = (int) temp2;
        g2.fillRect(tempX, tempY, 3, 3);
        }           

    }
   public static void main(String[] args) {
        final JFrame f = new JFrame("ParticleField");
        final ParticleFieldWithTimer bb = new ParticleFieldWithTimer();
        f.setLayout(new FlowLayout());
        f.add(bb);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                try {
                    bb.finalize();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                f.dispose();
            }
        });
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

这是粒子类

 public class Particle {
private int x , y ;

Random r = new Random();

public Particle () {

    x = r.nextInt(500);
    y = r.nextInt(500);

}
public int getX() {
    return x;
}
public int getY() {
    return y;
}
public void move() {

    x += r.nextBoolean() ? 1 : - 1;
    y += r.nextBoolean() ? 1 : - 1;
    System.out.println("x : " + x+" y: " + y);
}



 }

创建一个类 Particle,它有两个双字段 x 和 y,一个 将这些字段初始化为 0 之间的随机值的构造函数 和 500,返回其值的方法 getX 和 getY,以及一个方法 void move() 对每个值随机加或减一 x 和 y。 (添加到 x 和 y 的量是两个独立的随机数 数字。)接下来,创建一个类 ParticleFieldWithTimer 扩展 面板。这个类的大小应该是 500 * 500 像素。它的 构造函数应该首先用 100 填充 ArrayList 字段 粒子对象,然后启动一个摆动 25 次的摆动计时器 第二。在每个滴答声中,动作侦听器应首先调用该方法 为每个粒子移动,然后调用 repaint。油漆组件 ParticleFieldWithTimer 的方法应该将每个粒子绘制为 3*3 矩形到其当前坐标。

【问题讨论】:

  • 代码中的简单错误,仅此而已。
  • 您也可以使用g2.fill(new Rectangle2D.Double(p.getX(), p.getY(), 3, 3)); 来获得相同的结果,而无需进行所有的小提琴转换......

标签: java swing animation random


【解决方案1】:

paintComponent方法中,

替换

double temp2 = p.getX();

double temp2 = p.getY();

祝你好运。

【讨论】:

  • 这只是一个很难找到的错字...没必要觉得自己像个白痴。 :)
  • @user3363135,另外,既然您的 getX() 和 getY() 方法返回 int 值,为什么要先将它们分配给双变量?
  • 哦,x 和 y 实际上应该是双精度数,而 getX() 和 getY() 应该在问题中返回双精度数,我只是在调试期间将它们更改为 int 以确保有尝试将 r.nextDouble() 转换为 0 到 500 之间的值不是逻辑错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2012-03-15
  • 2010-12-16
相关资源
最近更新 更多