Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class Ball
  5. {
  6.     public int x, y, r, maxx, maxy;
  7.  
  8.     //public static needed to be referenced in context
  9.     public static int dx = 1, dy = 1;
  10.  
  11.     public Color c;
  12.  
  13.     public Ball(int X, int Y, int R, Color C)
  14.     {
  15.         x = X;
  16.         y = Y;
  17.         r = R;
  18.         c = C;
  19.     }
  20.  
  21.     public void draw(Graphics g)
  22.     {
  23.         g.setColor(c);
  24.         g.fillOval(x-r, y-r, 2*r, 2*r);
  25.     }
  26.  
  27.     public void move()
  28.     {
  29.         x = x + dx;
  30.         y = y + dy;
  31.  
  32.         if(x > maxx)
  33.             dx=-dx;
  34.  
  35.         if(x <= 0)
  36.             dx=-dx;
  37.  
  38.         if(y > maxy)
  39.             dy=-dy;
  40.  
  41.         if(y <= 0)
  42.             dy=-dy;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement