2

I was wondering if there was a function like void draw() which Processing programming language uses that gets called every frame. Or even just a function that loops infinitely when it gets called but only runs through it every time there is a new frame. I heard of something called a runnable in java how do i go about using this? also is there a better way then having a runnable with a delay like a function that is hardcoded to run every frame. Oh and also what is the function call that will allow me to see how much time (in milliseconds preferably) since the application has started running that way i can make my runnables / frame calls much more precise so that the game runs about the same speed on every computer regardless of the frame rate.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
semicolon
  • 2,530
  • 27
  • 37
  • What exactly do you mean by `frame` Are you talking about a graphical window? – Mohayemin Jul 16 '12 at 04:15
  • I mean like a frame as in FPS. Like every time the screen refreshes. 60 times a second for most decent monitors. – semicolon Jul 16 '12 at 04:35
  • I'm guessing that you're writing your first game. I'd suggest buying a book or reading some tutorials online about game development in Java. That should teach you everything you want to know. – Brad Jul 16 '12 at 04:51
  • I am just wondering why people down-voted my question, can someone tell me what i did wrong? – semicolon Jul 18 '12 at 03:04
  • 1
    it's because ignorant Java programmers dont bother to research Processing, even though you gave a link to it. I upvoted. – jesses.co.tt Jun 14 '13 at 22:44

1 Answers1

1

Perhaps you need something like this

import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Repainter extends JPanel {
    private Point topLeft;
    private int increamentX = 5;

    public Repainter() {
        topLeft = new Point(100, 100);
    }

    public void move() {
        topLeft.x += increamentX;
        if (topLeft.x >= 200 || topLeft.x <= 100) {
            increamentX = -increamentX;
        }

        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawRect(topLeft.x, topLeft.y, 100, 100);
    }

    public void startAnimation() {
        SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
            @Override
            protected Object doInBackground() throws Exception {
                while (true) {
                    move();
                    Thread.sleep(100);
                }
            }
        };

        sw.execute();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Repaint Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);

                Repainter repainter = new Repainter();

                frame.add(repainter);

                repainter.startAnimation();
                frame.setVisible(true);
            }
        });
    }
}
Mohayemin
  • 3,841
  • 4
  • 25
  • 54