// Voorbeeld 14.11 // Dubbele buffering import java.awt.*; import java.applet.*; import java.awt.event.*; public class Vb14_11 extends Applet { private Animatie animatie; private Button startKnop, stopKnop; public void init() { startKnop = new Button( "Start" ); startKnop.addActionListener( new StartKnopHandler() ); stopKnop = new Button( "Stop" ); stopKnop.addActionListener( new StopKnopHandler() ); add( startKnop ); add( stopKnop ); } public void start() { if( animatie == null ) { animatie = new Animatie( this ); animatie.start(); } } public void stop() { if( animatie != null ) { animatie.nuStoppen(); animatie = null; } } class StartKnopHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { start(); } } class StopKnopHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { stop(); } } } class Animatie extends Thread { private boolean doorgaan; private Image buffer; private Graphics g, gB; private Applet applet; private int appletBreedte, appletHoogte; private int x, hoek; // constructor public Animatie( Applet applet ) { this.applet = applet; g = applet.getGraphics(); appletBreedte = applet.getSize().width; appletHoogte = applet.getSize().height; buffer = applet.createImage( appletBreedte, appletHoogte ); gB = buffer.getGraphics(); doorgaan = true; x = 0; hoek = 0; } public void run() { while( doorgaan ) { // Bouw een nieuw beeld op in de buffer gB.setColor( Color.red ); gB.fillArc( x, 100, 100, 100, hoek, 270 ); // Zet inhoud van buffer op scherm en slaap even g.drawImage( buffer, 0, 0, applet ); slaap( 10 ); // Veeg de buffer schoon gB.setColor( applet.getBackground() ); gB.fillRect(0, 0, appletBreedte, appletHoogte); hoek -= 3 ; x = (int) ( -hoek * 314.1 / 360 ); } } public void nuStoppen() { doorgaan = false; gB.dispose(); } private void slaap( int millisec ) { try { sleep( millisec ); } catch( InterruptedException e ) {} } }