Advertisement
Guest User

My Bouncing Ball

a guest
Nov 26th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Color;
  3.  
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.ActionEvent;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JComponent;
  9. import javax.swing.Timer;
  10.  
  11. class BouncingBall extends JComponent implements ActionListener {
  12.  
  13.     private int x = 0;
  14.     private int y = 0;
  15.     private int size = 50;
  16.  
  17.     private int horizontalStep = 1;
  18.     private int verticalStep = 1;
  19.  
  20.     private int delay = 2;
  21.  
  22.     private Color color = Color.BLUE;
  23.  
  24.     private BouncingBall() {
  25.        
  26.         // set canvas to an initial size,
  27.         // otherwise it won't show up
  28.         this.setSize(1, 1);
  29.        
  30.         // create timer to redraw the circle
  31.         // after the delay time has expired
  32.         Timer timer = new Timer(delay, this);
  33.        
  34.         // start the timer
  35.         timer.start();
  36.     }
  37.  
  38.     public void actionPerformed(ActionEvent e) {
  39.  
  40.         // boundaries
  41.         int west = 0;
  42.         int south = this.getHeight() - 1 - size;
  43.         int east = this.getWidth() - 1 - size;
  44.         int north = 0;
  45.  
  46.         // increment step for x coordinate,
  47.         // check for boundaries and invert
  48.         // direction if necessary
  49.         this.x = this.x + this.horizontalStep;
  50.         if (this.x < west || this.x > east) {
  51.             this.horizontalStep = this.horizontalStep * -1;
  52.             this.x = this.x + 2 * this.horizontalStep;
  53.         }
  54.  
  55.         // dito for y coordinate
  56.         this.y = this.y + this.verticalStep;
  57.         if (this.y < north || this.y > south) {
  58.             this.verticalStep = this.verticalStep * -1;
  59.             this.y = this.y + 2 * this.verticalStep;
  60.         }
  61.  
  62.         // fancy gimmick for changing the color
  63.         // when the circle hits a boundary
  64.         if (this.x == west || this.x == east || this.y == north || this.y == south) {
  65.             this.color = color.equals(Color.BLUE) ? Color.RED : Color.BLUE;
  66.         }
  67.  
  68.         // call for repainting the canvas
  69.         this.repaint();
  70.     }
  71.  
  72.     @Override
  73.     public void paintComponent(Graphics g) {
  74.  
  75.         // fit canvas size to window size
  76.         this.setSize(this.getParent().getSize());
  77.        
  78.         // fill background
  79.         g.setColor(Color.WHITE);
  80.         g.fillRect(0, 0, this.getWidth(), this.getHeight());
  81.  
  82.         // draw circle shape
  83.         g.setColor(this.color);
  84.         g.fillOval(x, y, size, size);
  85.     }
  86.  
  87.     public static void main(String[] args) {
  88.  
  89.         JFrame window = new JFrame("My Bouncing Ball");
  90.         window.setLocation(100, 100);
  91.         window.setSize(800, 600);
  92.         window.getContentPane().add(new BouncingBall());
  93.         window.setVisible(true);
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement