Advertisement
Nick-O-Rama

BouncingBall

Aug 21st, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package javafx;
  2.  
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8.  
  9. /**
  10.  *
  11.  * @author Compsci
  12.  */
  13. public class BouncingBall extends JApplet{
  14.    
  15.     private final int X = 200;
  16.     private final int WIDTH = 40;
  17.     private final int HEIGHT = 40;
  18.     private final int TIME_DELAY = 30;
  19.     private final int MOVE = 20;
  20.     private final int MINIMUM_Y = 50;
  21.     private final int MAXIMUM_Y = 400;
  22.     private int y = 400;
  23.     private Timer timer;
  24.     private boolean goingUp = true;
  25.    
  26.     public void init() {
  27.         timer = new Timer(TIME_DELAY, new TimerListener());
  28.         timer.start();
  29.        
  30.     }
  31.    
  32.     public void paint(Graphics g) {
  33.         super.paint(g);
  34.         g.setColor(Color.RED);
  35.         g.fillOval(X, y, WIDTH, HEIGHT);
  36.     }
  37.    
  38.     private class TimerListener implements ActionListener {
  39.         public void actionPerformed(ActionEvent e) {
  40.             if (goingUp){
  41.                 if (y > MINIMUM_Y)
  42.                     y -= MOVE;
  43.                 else
  44.                     goingUp = false;
  45.             }
  46.             else{
  47.                 if (y < MAXIMUM_Y)
  48.                     y += MOVE;
  49.                 else
  50.                     goingUp = true;
  51.             }
  52.            
  53.             repaint();
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement