Freudian Slip
Freudian Slip

Reputation: 31

Executing and Displaying Graphics Every Second Rather Than At Once

I'm working on a simulation for the growth of an organism, using jLabels for the organisms. However, when I implement a for loop and a timer to try and show it moving, it freezes and then displays the final position of the label rather than showing it move. Could anyone explain to me why this is happening?

public class TestView extends FrameView {
   public TestView(SingleFrameApplication app) {
      super(app);
      initComponents();
      picture = new JLabel();
      picture.setIcon(new ImageIcon(System.getProperty("user.dir") +
            File.separator + "mouse.gif"));
      picture.setBounds(0, 0, 100, 100);
      mainPanel.add(picture);

      for (int x = 0; x < 20; x++) {
         move();
         wait(50);
      }
   }

   public static void wait(int n) {
       long t0, t1;
       t0 = System.currentTimeMillis();
       do {
           t1 = System.currentTimeMillis();
       } while (t1 - t0 < n);
   }

   public static void move() {
       picture.setBounds(picture.getX() + 5, picture.getY(), 100, 100);
   }

Upvotes: 1

Views: 66

Answers (1)

Catalina Island
Catalina Island

Reputation: 7126

You might like this example of diffusion limited aggregation and other simulations.

Upvotes: 1

Related Questions