Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- public class MovingBall extends Applet implements Runnable
- {
- int x_pos = 10;
- int y_pos = 100;
- int x_speed = 1;
- int radius = 20;
- private Image dbImage;
- private Graphics dbg;
- int appletsize_x = 300;
- int appletsize_y = 300;
- public void init() {
- setBackground (Color.blue);
- }
- 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)
- {
- x_pos += x_speed;
- if (x_pos > appletsize_x - (radius * 6))
- {
- x_speed = -1;
- }
- else if (x_pos < radius)
- {
- x_speed = +1;
- }
- repaint();
- try
- {
- Thread.sleep (20);
- }
- catch (InterruptedException ex)
- {
- }
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void paint (Graphics g) {
- g.setColor (Color.red);
- g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * 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);
- }
- }
Add Comment
Please, Sign In to add comment