Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ball;
- import java.applet.*;
- import java.awt.*;
- public class Ball extends Applet implements Runnable
- {
- int x_pos = 10;
- int y_pos = 100;
- int radius = 20;
- int x_speed = 1;
- int appletsize_x = 200;
- int appletsize_y = -300;
- int qq = 0;
- public void init() { }
- public void start() {
- // define a new thread
- Thread th = new Thread (this);
- // start this thread
- th.start ();
- }
- public void stop() { }
- public void destroy() { }
- public boolean mouseDown (Event e, int x, int y)
- {
- x_speed = -2*(x_speed);
- qq++;
- return true;
- }
- public void run () {
- // lower ThreadPriority
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- // run a long while (true) this means in our case "always"
- while (true)
- {
- if (x_pos > appletsize_x - radius)
- {
- // Change direction of ball movement
- x_speed = -1;
- qq = 0;
- }
- if (x_pos < radius) {
- x_speed = 1;
- qq = 0;
- }
- x_pos += x_speed;
- // repaint the applet
- repaint();
- try
- {
- // Stop thread for 20 milliseconds
- Thread.sleep (20);
- }
- catch (InterruptedException ex)
- {
- // do nothing
- }
- // set ThreadPriority to maximum value
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void paint (Graphics g) {
- // set color
- g.setColor (Color.red);
- // paint a filled colored circle
- g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement