/* <applet code="Animation" Width="400" Height="400"> </applet> */ import java.applet.*; import java.awt.*; public class Animation extends Applet implements Runnable { Thread thread; int count; public void init() { count = 0; thread = new Thread(this); thread.start(); } public void paint(Graphics g) { if ( count < 500 ) { g.fillRect(count, 0, 100, 100); g.drawRect(count / 4, 100, 100, 100); g.fillRect(count / 3, 200, 100, 100); g.fillOval(count / 2, 300, 100, 100); count += 10; } else count = 0; } public void run() { while ( true ) { try { Thread.sleep(50); repaint(); } catch ( InterruptedException e ) { e.printStackTrace(); } } } }ちらつきをなくすためには下のようにする.
/* <applet code="BufferAnimation" Width="400" Height="400"> </applet> */ import java.applet.*; import java.awt.*; public class BufferAnimation extends Applet implements Runnable { Image buffer; Graphics offg; Thread thread; int count; public void init() { buffer = createImage(400, 500); offg = buffer.getGraphics(); count = 0; thread = new Thread(this); thread.start(); } public void paint(Graphics g) { if ( count < 500 ) { offg.setColor(Color.black); offg.fillRect(count, 0, 100, 100); offg.drawRect(count / 4, 100, 100, 100); offg.fillRect(count / 3, 200, 100, 100); offg.fillOval(count / 2, 300, 100, 100); g.drawImage(buffer, 0, 0, this); offg.clearRect(0, 0, 400, 500); count += 10; } else count = 0; } public void update(Graphics g) { paint(g); } public void run() { while ( true ) { try { Thread.sleep(50); repaint(); } catch ( InterruptedException e ) { e.printStackTrace(); } } } }