Bouncing Circle : Applet « Swing JFC « Java






Bouncing Circle

Bouncing Circle
   
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/** An applet that displays a simple animation */
public class BouncingCircle extends Applet implements Runnable {
  int x = 150, y = 50, r = 50; // Position and radius of the circle

  int dx = 11, dy = 7; // Trajectory of circle

  Thread animator; // The thread that performs the animation

  volatile boolean pleaseStop; // A flag to ask the thread to stop

  /** This method simply draws the circle at its current position */
  public void paint(Graphics g) {
    g.setColor(Color.red);
    g.fillOval(x - r, y - r, r * 2, r * 2);
  }

  /**
   * This method moves (and bounces) the circle and then requests a redraw.
   * The animator thread calls this method periodically.
   */
  public void animate() {
    // Bounce if we've hit an edge.
    Rectangle bounds = getBounds();
    if ((x - r + dx < 0) || (x + r + dx > bounds.width))
      dx = -dx;
    if ((y - r + dy < 0) || (y + r + dy > bounds.height))
      dy = -dy;

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

    // Ask the browser to call our paint() method to draw the circle
    // at its new position.
    repaint();
  }

  /**
   * This method is from the Runnable interface. It is the body of the thread
   * that performs the animation. The thread itself is created and started in
   * the start() method.
   */
  public void run() {
    while (!pleaseStop) { // Loop until we're asked to stop
      animate(); // Update and request redraw
      try {
        Thread.sleep(100);
      } // Wait 100 milliseconds
      catch (InterruptedException e) {
      } // Ignore interruptions
    }
  }

  /** Start animating when the browser starts the applet */
  public void start() {
    animator = new Thread(this); // Create a thread
    pleaseStop = false; // Don't ask it to stop now
    animator.start(); // Start the thread.
    // The thread that called start now returns to its caller.
    // Meanwhile, the new animator thread has called the run() method
  }

  /** Stop animating when the browser stops the applet */
  public void stop() {
    // Set the flag that causes the run() method to end
    pleaseStop = true;
  }
}

           
         
    
    
  








Related examples in the same category

1.Icon Demo Applet
2.Socket Applet
3.Applet Socket Quote
4.Applet Print
5.Applet System Properties
6.Applet Sound
7.Show Document
8.Applet communication (talk to each other)
9.Get Applets
10.JDBC applet
11.An application and an appletAn application and an applet
12.Applet and Swing ComponentsApplet and Swing Components
13.Icon behavior in JbuttonsIcon behavior in Jbuttons
14.Signed Applet
15.Load resource in Applet
16.Scribble AppletScribble Applet
17.Toggle buttonToggle button
18.Applet Menu Bar Demo
19.Applet clock demoApplet clock demo
20.Applet event testerApplet event tester
21.Applet with parameters
22.First applet
23.AppletViewer - a simple Applet Viewer program
24.A simple loan calculator appletA simple loan calculator applet
25.URLButton -- a Button-like object that jumps to a URL
26.Get list of APPLET tags in one HTML file
27.Demonstration of Applet Methods
28.Demonstrates getParameterInfo() and getAppletInfo()
29.Walking Text Demo
30.Java Applets Can Run CGI's (on some browsers)
31.Demo Choice Applet
32.Demo Applet: Connect to Legacy System
33.ShowDocApplet: try out showDocument()
34.Bookmarks
35.Java Wave Applet Demo
36.Slide Puzzle
37.A class to allow use of the ncsa ISMAP format in java applets
38.Image Loader Applet
39.Thread Race Applet
40.Applet: Print from an Applet
41.Calling methods of an Applet from JavaScript code
42.Read an applet parameters
43.Change an applet background color
44.Passing Parameters to Java Applet
45.Display message in browser status bar
46.Your own Applet runtime environment
47.This applet is a simple button that plays an audio clip when pushed
48.EventQueue.invokeLater and JApplet
49.An applet component that draws a bar chart
50.This applet displays a greeting from the authors