Advertisement
calcpage

LACS2013-Ball.java

Jun 6th, 2013
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.96 KB | None | 0 0
  1. //Ball.java     MrG     2013.0603
  2. import java.awt.Color;
  3. public class Ball
  4. {
  5.     private double r;
  6.     private double rx;
  7.     private double ry;
  8.     private double vx;
  9.     private double vy;
  10.     private Color c;
  11.  
  12.     public Ball()
  13.     {
  14.         r=Math.random();
  15.         //rx=20*Math.random()-10;
  16.         //ry=20*Math.random()-10;
  17.         rx=0.0;
  18.         ry=0.0;
  19.         int choice = (int)(4*Math.random());
  20.         if(choice==0)
  21.         {
  22.             vx=Math.random()/2;
  23.             vy=Math.random()/2;
  24.         }
  25.         if(choice==1)
  26.         {
  27.             vx=-Math.random()/2;
  28.             vy=Math.random()/2;
  29.         }
  30.         if(choice==2)
  31.         {
  32.             vx=-Math.random()/2;
  33.             vy=-Math.random()/2;
  34.         }
  35.         if(choice==3)
  36.         {
  37.             vx=Math.random()/2;
  38.             vy=-Math.random()/2;
  39.         }
  40.         c=new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
  41.     }
  42.  
  43.     public void draw()
  44.     {
  45.         StdDraw.setPenColor(c);
  46.         StdDraw.filledCircle(rx,ry,r);
  47.     }
  48.  
  49.     public void move()
  50.     {
  51.         if(Math.abs(rx)>10)
  52.         {
  53.             vx=-vx;
  54.         }
  55.         if(Math.abs(ry)>10)
  56.         {
  57.             vy=-vy;
  58.         }
  59.         rx+=vx;
  60.         ry+=vy;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement