Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package example10;
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.JApplet;
- /**
- * @author pajensen
- *
- */
- public class BouncingBall extends JApplet implements Runnable
- {
- // Object variables
- double x, y;
- double xVelocity, yVelocity;
- public void init ()
- {
- x = 50;
- y = 0;
- xVelocity = 1;
- yVelocity = 0;
- // Begin a new thread of execution.
- Thread otherExecutionThread;
- otherExecutionThread = new Thread (this);
- otherExecutionThread.start();
- // At this point, the old thread is still running,
- // but a new thread has started.
- System.out.println ("The old thread is still running.");
- }
- public void paint(Graphics g)
- {
- // Clear background
- g.setColor(Color.BLUE.darker());
- g.fillRect(0, 0, getWidth(), getHeight());
- // Draw a ball.
- g.setColor(Color.YELLOW);
- g.fillOval((int)x, (int)y, 40, 40);
- g.setColor(Color.GREEN);
- g.fillArc((int)x+20, (int)y+10, 10, 10, 45, 45);
- }
- public void run ()
- {
- System.out.println ("The other thread has started.");
- while(true)
- {
- try
- {
- Thread.sleep(20); // Fifty times a second.
- }
- catch(Exception e) {}
- y = y + yVelocity;
- x = x + xVelocity;
- if (y > this.getHeight()-40)
- yVelocity = -(Math.abs(yVelocity));
- else
- yVelocity += 0.1; // Fixed after class, used to be before the if statement
- if (x > this.getWidth()-40)
- xVelocity = -(Math.abs(xVelocity));
- if (x < 0)
- xVelocity = +(Math.abs(xVelocity));
- repaint();
- }
- }
- }
Add Comment
Please, Sign In to add comment