Created
May 13, 2013 08:48
-
-
Save RobinMalfait/5566999 to your computer and use it in GitHub Desktop.
JAVA: standaard voor beweging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.applet.*; | |
import java.awt.*; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
public class class_name extends Applet implements Runnable{ | |
Thread animator; | |
Integer frame, delay; | |
Dimension offDimension; | |
Image offImage; | |
Graphics offGraphics; | |
public void init() | |
{ | |
delay = 40; | |
frame = 0; | |
resize(800, 800); | |
setFocusable(true); | |
} | |
/** | |
* Double Buffering System | |
*/ | |
public void update(Graphics g) | |
{ | |
Dimension d = getSize(); | |
if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) { | |
offDimension = d; | |
offImage = createImage(d.width, d.height); | |
offGraphics = offImage.getGraphics(); | |
} | |
offGraphics.setColor(getBackground()); | |
offGraphics.fillRect(0, 0, d.width, d.height); | |
offGraphics.setColor(Color.black); | |
paintFrame(offGraphics); | |
g.drawImage(offImage, 0, 0, null); | |
} | |
public void paint(Graphics g){ update(g); } | |
public void paintFrame(Graphics g) | |
{ | |
} | |
public boolean action(Event e, Object o) { return false; } | |
public void start() | |
{ | |
animator = new Thread(this); | |
animator.start(); | |
} | |
public void run() | |
{ | |
Long l = System.currentTimeMillis(); | |
do { | |
if (Thread.currentThread() != animator) break; | |
repaint(); | |
try { | |
l += delay; | |
Thread.sleep(Math.max(l - System.currentTimeMillis(), 0)); | |
} catch (InterruptedException e) { | |
break; | |
} | |
frame++; | |
} while (true); | |
} | |
public void stop() | |
{ | |
animator = null; | |
offImage = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment