Results 1 to 1 of 1
Thread: How to create animation
-
How to create animation
This Java tips creates a simple animation infrastructure using threads and
overwritten paint(Graphics g) method.
Java Code:import java.applet.Applet; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class AppletAnimation extends Applet implements Runnable { int frameNumber = -1; int delay = 100; Thread animatorThread; boolean frozen = false; public void init() { String str; addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (frozen) { frozen = false; start(); } else { frozen = true; stop(); } } }); } public void start() { if (!frozen) { if (animatorThread == null) { animatorThread = new Thread(this); } animatorThread.start(); } } public void stop() { animatorThread = null; } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); long startTime = System.currentTimeMillis(); Thread currentThread = Thread.currentThread(); while (currentThread == animatorThread) { frameNumber++; repaint(); try { startTime += delay; Thread.sleep(100); } catch (InterruptedException e) { break; } } } public void paint(Graphics g) { g.drawString("Frame " + frameNumber, 0, 30); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Animation with Animated GIF
By JavaBean in forum Java 2DReplies: 3Last Post: 06-04-2011, 04:26 PM -
How to create Animation: Paint and thread
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:42 PM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM -
Text Animation
By rossomandop@acm.org in forum AWT / SwingReplies: 4Last Post: 05-30-2008, 03:34 AM -
GridLayout with animation?
By tojas in forum AWT / SwingReplies: 3Last Post: 11-12-2007, 10:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks