简体   繁体   中英

Java swing simple animation

i'm having trouble with a simple animation program(and yes this is homework). The question asks us to create a 100 objects of Particle class at random on a 500X500 window and move them around using a swing timer. Here's the problem i'm having, the particles being created don't appear to be random but they align along a diagonal axis, i'm having trouble finding the logic error, would really appreciate your help. Here's a screenshot that should make it clear. http://i57.tinypic.com/qswbix.jpg

I'm also posting the homework question at the end of the post in case anyone found my questions hard to understand. THank you for help

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

Here's the particle class

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



 }

Create a class Particle that has two double fields x and y, a constructor that initializes these fields to random values between 0 and 500, methods getX and getY that return their values, and a method void move() that randomly adds or subtracts one to each of the values of x and y. (The quantities added to x and y are two separate random numbers.) Next, create a class ParticleFieldWithTimer that extends JPanel. This class should prefer to be 500 * 500 pixels in size. Its constructor should first fill an ArrayList field with 100 Particle objects, then start a Swing Timer that ticks 25 times a second. At each tick, the action listener should first call the method move for each particle, and then call repaint. The paintComponent method of ParticleFieldWithTimer should draw each particle as a 3*3 rectangle to its current coordinates.

In paintComponent method,

Replace

double temp2 = p.getX();

with

double temp2 = p.getY();

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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