Bouncing Circle : Animation « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » AnimationScreenshots 
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. Is Event Dispatcher ThreadIs Event Dispatcher Thread
2. Timer based animation
3. A rotating and scaling rectangle.
4. Fade out an image: image gradually get more transparent until it is completely invisible.
5. Font size animation
6. Hypnosis animationHypnosis animation
7. Noise ImageNoise Image
8. How to create Animation: Paint and threadHow to create Animation: Paint and thread
9. How to create animationHow to create animation
10. Bounce ThreadBounce Thread
11. Animation: bounce
12. Image BouncerImage Bouncer
13. Text animationText animation
14. Buffered Animation DemoBuffered Animation Demo
15. Hypnosis SpiralHypnosis Spiral
16. Animator DemoAnimator Demo
17. Towers of Hanoi
18. Make your own animation from a series of images
19. Composition technique in this animation.
20. Animated Button
21. Animated Message Panel
22. Animated PasswordField
23. Animated TextField
24. A simple spring simulation in one dimension
w___w___w.ja___va__2s__.c_o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.