View Single Post
  #2 (permalink)  
Old 03-20-2008, 09:20 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
// <applet code="OkAnimation" width="300" height="300"></applet> // use: >appletviewer OkAnimation.java import java.applet.*; import java.awt.*; public class OkAnimation extends Applet implements Runnable { int y; int dy = 10; Thread t = null; boolean isRunning = false; /** Applet method override */ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.fillOval(100, y, 50, 50); } /** Applet method override */ public void start() { if(!isRunning) { isRunning = true; t = new Thread(this); t.start(); } } /** Applet method override */ public void stop() { isRunning = false; if(t != null) t.interrupt(); // wake up t = null; } /** Runnable interface implementation */ public void run() { while(isRunning) { try { t.sleep( 1000 ); } catch(InterruptedException e) { stop(); } if(y + dy < 0 || y + 50 + dy > getHeight()) { // if the next move puts us out of bounds // reverse direction. dy *= -1; } y = y+dy; repaint(); } } }
Reply With Quote