1  /*
  2   *  File:  MyMovingPolygons2.java
  3   *
  4   *  Moving polygons with boundary checking, a MovablePolygon class, 
  5   *  and double buffering
  6   *
  7   *  Copyright:  Northeast Parallel Architectures Center
  8   *
  9   */
 10  
 11  import java.applet.Applet;
 12  import java.awt.*;
 13  
 14  public class MyMovingPolygons2 extends Applet implements Runnable {
 15  
 16    // Instance variables:
 17    private final int numPoly = 4;     // number of movable polygons
 18    private MovablePolygon polygon[];  // array of movable polygons
 19    private Thread thread;             // a thread
 20    private Image buffer;              // image object for double buffering
 21    private Graphics gOffScreen;       // graphics object for double buffering
 22      
 23    // Override java.applet.Applet.init:
 24    public void init () {
 25  
 26      setBackground( Color.black );
 27      setForeground( Color.white );
 28      
 29      // Instantiate the polygon array:
 30      polygon = new MovablePolygon[ numPoly ];
 31  
 32      // Instantiate a square:
 33      polygon[0] = new MovablePolygon( 75, 75, 50, 4 );
 34      polygon[0] = ( MovablePolygon ) polygon[0].centerRotate( Math.PI/4 );
 35      polygon[0].setDelta( 2, 3 );
 36      polygon[0].setColor( Color.red );
 37  
 38      // Instantiate a regular hexagon:
 39      polygon[1] = new MovablePolygon( 125, 60, 50, 6 );
 40      polygon[1].setDelta( -3, 2 );
 41      polygon[1].setColor( Color.blue );
 42  
 43      // Instantiate an equilateral triangle:
 44      polygon[2] = new MovablePolygon( 60, 125, 50, 3 );
 45      polygon[2].setDelta( -2, 2 );
 46      polygon[2].setColor( Color.green );
 47      
 48      // Instantiate a regular octagon:
 49      polygon[3] = new MovablePolygon( 125, 125, 50, 8 );
 50      polygon[3].setDelta( 3, -2 );
 51      polygon[3].setColor( Color.yellow );
 52      
 53      // Create an off-screen image for double buffering:
 54      buffer = createImage( getSize().width, getSize().height );
 55      // Get off-screen graphics context:
 56      gOffScreen = buffer.getGraphics(); 
 57  
 58    }
 59  
 60    // Override java.applet.Applet.start:
 61    public void start() {
 62      if ( thread == null ) {
 63        thread = new Thread( this );
 64        thread.start();
 65      }
 66    }
 67    
 68    // Override java.applet.Applet.stop:
 69    public void stop() {
 70      if ( thread != null ) {
 71        thread.stop();
 72        thread = null;
 73      }
 74    }
 75    
 76    // Implement java.lang.Runnable.run:
 77    public void run() {
 78      while ( thread != null ) {
 79        try {
 80          Thread.sleep( 20 );
 81        } catch ( InterruptedException e ) {
 82          // do nothing   
 83        }
 84        repaint();
 85      }
 86    }
 87  
 88    // Override java.awt.Component.update:
 89    public void update( Graphics g ) {
 90  
 91      // Clear the buffer:
 92      gOffScreen.setColor( getBackground() );
 93      gOffScreen.fillRect( 0, 0, getSize().width, getSize().height );
 94      
 95      // Paint the polygons off screen, that is, in the buffer:
 96      paint( gOffScreen );
 97      
 98      // Draw the buffer in the applet window:
 99      g.drawImage( buffer, 0, 0, this );
100  
101    }
102    
103    // Override java.awt.Component.paint:
104    public void paint( Graphics g ) {
105    
106      // Check, move, and fill each polygon:
107      for ( int i = 0; i < numPoly; i++ ) {
108        polygon[i].checkBounds( this.getBounds() );
109        polygon[i].move();
110        polygon[i].fill( g );
111      }
112      
113    }
114    
115  }