- import java.applet.*;
- import java.awt.*;
- public class BallApplet extends Applet implements Runnable {
- int ball_x_pos = 10;
- int ball_y_pos = 100;
- int ball_radius = 20;
- int ball_x_speed = 1;
- int ball_y_speed = 1;
- int appletsize_x = 700;
- int appletsize_y = 400;
- int mouse_x_pos;
- int mouse_y_pos;
- boolean isStoped;
- AudioClip bounce;
- Image backImage;
- private Image dbImage;
- private Graphics dbg;
- public void init() {
- bounce = getAudioClip(getCodeBase(), "bounce.au");
- backImage = getImage(getCodeBase(), "bg.jpg");
- }
- public void start() {
- Thread th = new Thread(this);
- th.start();
- }
- public void stop() {
- }
- public void destroy() {
- }
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while (true) {
- ball_x_pos += ball_x_speed;
- ball_y_pos += ball_y_speed;
- if (ball_x_pos > appletsize_x - ball_radius) {
- bounce.play();
- ball_x_speed = -1;
- } else if (ball_x_pos < ball_radius) {
- bounce.play();
- ball_x_speed = +1;
- }
- if (ball_x_pos > appletsize_x + ball_radius) {
- ball_x_pos = -20;
- }
- if (ball_y_pos > appletsize_y - ball_radius) {
- bounce.play();
- ball_y_speed = -1;
- } else if (ball_y_pos < ball_radius) {
- bounce.play();
- ball_y_speed = +1;
- }
- if (ball_x_pos > appletsize_x + ball_radius) {
- ball_x_pos = -20;
- }
- repaint();
- try {Thread.sleep(20);} catch (InterruptedException ex) {}
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void paint(Graphics g) {
- g.drawImage(backImage, 0, 0, this);
- g.setColor(Color.black);
- g.fillRect(10, mouse_y_pos, 5, 100);
- g.setColor(Color.red);
- g.fillOval(ball_x_pos - ball_radius, ball_y_pos - ball_radius, 2 * ball_radius, 2 * ball_radius);
- }
- public void update(Graphics g) {
- if (dbImage == null) {
- dbImage = createImage(this.getSize().width, this.getSize().height);
- dbg = dbImage.getGraphics();
- }
- dbg.setColor(getBackground());
- dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
- dbg.setColor(getForeground());
- paint(dbg);
- g.drawImage(dbImage, 0, 0, this);
- }
- public boolean mouseMove(Event e, int x, int y) {
- mouse_x_pos = x;
- mouse_y_pos = y;
- return false;
- }
- }
Undefined
By: Guest | Date: Jul 1 2010 09:39 | Format: None | Expires: never | Size: 2.29 KB | Hits: 870
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago