Bounce Thread : Animation « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » 2D Graphics GUI » AnimationScreenshots 
Bounce Thread
Bounce Thread


/**
 @version 1.20 1999-04-25
 @author Cay Horstmann
 */

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BounceThread {
  public static void main(String[] args) {
    JFrame frame = new BounceThreadFrame();
    frame.show();
  }
}

class BounceThreadFrame extends JFrame {
  public BounceThreadFrame() {
    setSize(300200);
    setTitle("Bounce");

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = getContentPane();
    canvas = new JPanel();
    contentPane.add(canvas, "Center");
    JPanel p = new JPanel();
    addButton(p, "Start"new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        Ball b = new Ball(canvas);
        b.start();
      }
    });

    addButton(p, "Close"new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        canvas.setVisible(false);
        System.exit(0);
      }
    });
    contentPane.add(p, "South");
  }

  public void addButton(Container c, String title, ActionListener a) {
    JButton b = new JButton(title);
    c.add(b);
    b.addActionListener(a);
  }

  private JPanel canvas;
}

class Ball extends Thread {
  public Ball(JPanel b) {
    box = b;
  }

  public void draw() {
    Graphics g = box.getGraphics();
    g.fillOval(x, y, XSIZE, YSIZE);
    g.dispose();
  }

  public void move() {
    if (!box.isVisible())
      return;
    Graphics g = box.getGraphics();
    g.setXORMode(box.getBackground());
    g.fillOval(x, y, XSIZE, YSIZE);
    x += dx;
    y += dy;
    Dimension d = box.getSize();
    if (x < 0) {
      x = 0;
      dx = -dx;
    }
    if (x + XSIZE >= d.width) {
      x = d.width - XSIZE;
      dx = -dx;
    }
    if (y < 0) {
      y = 0;
      dy = -dy;
    }
    if (y + YSIZE >= d.height) {
      y = d.height - YSIZE;
      dy = -dy;
    }
    g.fillOval(x, y, XSIZE, YSIZE);
    g.dispose();
  }

  public void run() {
    try {
      draw();
      for (int i = 1; i <= 1000; i++) {
        move();
        sleep(5);
      }
    catch (InterruptedException e) {
    }
  }

  private JPanel box;

  private static final int XSIZE = 10;

  private static final int YSIZE = 10;

  private int x = 0;

  private int y = 0;

  private int dx = 2;

  private int dy = 2;
}

           
       
Related examples in the same category
1. Is Event Dispatcher ThreadIs Event Dispatcher Thread
2. Hypnosis animationHypnosis animation
3. Noise ImageNoise Image
4. How to create Animation: Paint and threadHow to create Animation: Paint and thread
5. How to create animationHow to create animation
6. Animation: bounce
7. Image BouncerImage Bouncer
8. Text animationText animation
9. Buffered Animation DemoBuffered Animation Demo
10. Bouncing CircleBouncing Circle
11. Hypnosis SpiralHypnosis Spiral
12. Animator DemoAnimator Demo
13. Towers of Hanoi
w_ww_.___j_a_v__a_2s.c_om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.