FileDocCategorySizeDatePackage
SmoothCircle.javaAPI DocExample3684Mon Sep 22 13:30:30 BST 1997None

SmoothCircle

public class SmoothCircle extends Applet implements Runnable
An applet that displays a simple animation using double-buffering and clipping

Fields Summary
int
x
int
y
int
r
int
dx
int
dy
Dimension
size
Image
buffer
Graphics
bufferGraphics
Thread
animator
boolean
please_stop
Constructors Summary
Methods Summary
public voidinit()
Set up an off-screen Image for double-buffering

             // A flag asking animation thread to stop

          
     
    size = this.size();
    buffer = this.createImage(size.width, size.height);
    bufferGraphics = buffer.getGraphics();
  
public booleanmouseDown(java.awt.Event e, int x, int y)
Allow the user to start and stop the animation by clicking

    if (animator != null) please_stop = true;  // if running request a stop
    else start();                              // otherwise start it.
    return true;
  
public voidpaint(java.awt.Graphics g)
Draw the circle at its current position, using double-buffering

    // Draw into the off-screen buffer.
    // Note, we could do even better clipping by setting the clip rectangle
    // of bufferGraphics to be the same as that of g.
    // In Java 1.1:  bufferGraphics.setClip(g.getClip());
    bufferGraphics.setColor(this.getBackground());
    bufferGraphics.fillRect(0, 0, size.width, size.height); // clear the buffer
    bufferGraphics.setColor(Color.red);
    bufferGraphics.fillOval(x-r, y-r, r*2, r*2);            // draw the circle

    // Then copy the off-screen buffer onto the screen
    g.drawImage(buffer, 0, 0, this);
  
public voidrun()
The body of the animation thread

    while(!please_stop) {
      // Bounce the circle if we've hit an edge.
      if ((x - r + dx < 0) || (x + r + dx > size.width)) dx = -dx;
      if ((y - r + dy < 0) || (y + r + dy > size.height)) dy = -dy;

      // Move the circle.
      x += dx;  y += dy;

      // Ask the browser to call our paint() method to redraw the circle
      // at its new position.  Tell repaint what portion of the applet needs
      // be redrawn: the rectangle containing the old circle and the
      // the rectangle containing the new circle.  These two redraw requests
      // will be merged into a single call to paint()
      repaint(x-r-dx, y-r-dy, 2*r, 2*r);   // repaint old position of circle
      repaint(x-r, y-r, 2*r, 2*r);         // repaint new position of circle

      // Now pause 1/10th of a second before drawing the circle again.
      try { Thread.sleep(100); } catch (InterruptedException e) { ; }
    }
    animator = null;
  
public voidstart()
Start the animation thread

    if (animator == null) {
      please_stop = false;
      animator = new Thread(this);
      animator.start();
    }
  
public voidstop()
Stop the animation thread

 please_stop = true; 
public voidupdate(java.awt.Graphics g)
Don't clear the screen; just call paint() immediately It is important to override this method like this for double-buffering

 paint(g);