Advertisement
Tech_geek23

Ball.java

Feb 28th, 2013
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3.  
  4. public class Ball extends Block
  5. {
  6.     private int xSpeed;
  7.     private int ySpeed;
  8.  
  9.     public Ball()
  10.     {
  11.         super(200,200);
  12.         xSpeed = 3;
  13.         ySpeed = 1;
  14.     }
  15.  
  16.     //add the other Ball constructors
  17.     public Ball(int x, int y)
  18.     {
  19.         super(x, y);
  20.         xSpeed = 3;
  21.         ySpeed = 1;
  22.     }
  23.     public Ball(int x, int y, int wt, int ht)
  24.     {
  25.         super(x,y,wt,ht);
  26.         xSpeed = 3;
  27.         ySpeed = 1;
  28.     }
  29.     public Ball(int x, int y, int wt, int ht, Color c)
  30.     {
  31.         super(x, y, wt, ht, c);
  32.         xSpeed = 3;
  33.         ySpeed = 1;
  34.     }
  35.     public Ball(int x, int y, int wt, int ht, Color c, int xSpd, int ySpd)
  36.     {
  37.         super(x, y, wt, ht, c);
  38.         xSpeed = xSpd;
  39.         ySpeed = ySpd;
  40.     }
  41.        
  42.    //add the set methods
  43.    public void setXSpeed(int xSpd)
  44.    {
  45.        xSpeed = xSpd;
  46.    }
  47.    public void setYSpeed(int ySpd)
  48.    {
  49.        ySpeed = ySpd;
  50.    }
  51.  
  52.    public void moveAndDraw(Graphics window)
  53.    {
  54.     //draw a white ball at old ball location
  55.       Ball old = new Ball(getX(), getY());
  56.       old.draw(window, Color.WHITE);
  57.  
  58.       setX(getX()+xSpeed);
  59.         //setY
  60.       setY(getY()+ySpeed);
  61.         //draw the ball at its new location
  62.       Ball nw = new Ball(getX(), getY());
  63.       nw.draw(window, Color.BLUE);
  64.    }
  65.    
  66.     public boolean equals(Object obj)
  67.     {
  68.         Ball lhs = (Ball) obj;
  69.         if(this.getX()== lhs.getX() && this.getY() == lhs.getY() && this.getHeight() == lhs.getHeight() && this.getWidth() == lhs.getWidth() && this.getColor() == lhs.getColor() && this.getXSpeed() == lhs.getXSpeed() && this.getYSpeed() == lhs.getYSpeed())
  70.             return true;
  71.         return false;
  72.     }  
  73.  
  74.    //add the get methods
  75.     public int getXSpeed()
  76.     {
  77.         return xSpeed;
  78.     }
  79.     public int getYSpeed()
  80.     {
  81.         return ySpeed;
  82.     }
  83.    
  84.    //add a toString() method
  85.     public String toString()
  86.     {
  87.         return  getX() + " " + getY() + " " + getWidth() + " " + getHeight() + " " + getColor() + " " + getXSpeed() + " " + getYSpeed();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement