All pastes #75370 Raw Edit

freeone3000

public java v1 · immutable
#75370 ·published 2006-06-30 03:03 UTC
rendered paste body
import java.awt.*;import javax.swing.*;import java.util.Random;public class RandomMovement extends JPanel implements Runnable {        private static final Dimension PREFERRED_SIZE = new Dimension(100, 100);        private static final int DIVISOR = 100000;        private long lastPaint;        private Rectangle rect;        private Random r;        public RandomMovement() {                super();                lastPaint = System.nanoTime();                rect = new Rectangle(5, 5);                r = new Random(5);        }        public void run() {                JFrame f = new JFrame("Random Movement");                this.setPreferredSize(PREFERRED_SIZE);                f.setContentPane(this);                f.pack();                f.setVisible(true);        }        public void paintComponent(Graphics g) {                long tmpTime = System.nanoTime();                modifyRectangle(nextRand(), nextRand());                g.setColor(Color.RED);                g.fillRect(rect.x, rect.y, rect.width, rect.height);                lastPaint = tmpTime;        }        private void modifyRectangle(int changex, int changey) {                rect.setLocation(rect.x + changex, rect.y + changey);        }        private int nextRand() {                return (int)((r.nextInt(2) - 1) * ((System.nanoTime()-lastPaint)/DIVISOR));        }        public static void main(String[] args) {                SwingUtilities.invokeLater(new RandomMovement());        }}