Advertisement
Tech_geek23

Block.java

Feb 28th, 2013
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3.  
  4. public class Block implements Locatable
  5. {
  6.     private int xPos;
  7.     private int yPos;
  8.     private int width;
  9.     private int height;
  10.  
  11.     private Color color;
  12.  
  13.     public Block()
  14.     {
  15.         xPos = 100;
  16.         yPos = 150;
  17.         width = 10;
  18.         height = 10;
  19.         color = new Color(0, 0, 0);
  20.     }
  21.  
  22.     //add other Block constructors - x , y , width, height, color
  23.    
  24.     public Block(int x, int y)
  25.     {
  26.         xPos = x;
  27.         yPos =y;
  28.         width = 10;
  29.         height = 10;
  30.         color = new Color(0, 0, 0);
  31.     }
  32.    
  33.     public Block(int x, int y, int wt, int ht)
  34.     {
  35.         xPos = x;
  36.         yPos = y;
  37.         width = wt;
  38.         height = ht;
  39.         color = new Color(0, 0, 0);
  40.     }
  41.     public Block(int x, int y, int wt, int ht,Color c)
  42.     {
  43.         xPos = x;
  44.         yPos = y;
  45.         width = wt;
  46.         height = ht;
  47.         color = c;
  48.     }
  49.    
  50.     public void setX(int x)
  51.     {
  52.         xPos = x;
  53.     }
  54.    
  55.     public void setY(int y)
  56.     {
  57.         yPos = y;
  58.     }
  59.    
  60.     public void setWidth(int wt)
  61.     {
  62.         width = wt;
  63.     }
  64.    
  65.     public void setHeight(int ht)
  66.     {
  67.         height = ht;
  68.     }
  69.    
  70.     public void setPos(int x, int y)
  71.     {
  72.         xPos = x;
  73.         yPos = y;
  74.     }
  75.    
  76.    //add the other set methods
  77.    
  78.  
  79.    public void setColor(Color col)
  80.    {
  81.        color = col;
  82.    }
  83.  
  84.    public void draw(Graphics window)
  85.    {
  86.     //uncomment after you write the set and get methods
  87.       window.setColor(color);
  88.       window.fillRect(getX(), getY(), getWidth(), getHeight());
  89.    }
  90.  
  91.    public void draw(Graphics window, Color col)
  92.    {
  93.  
  94.  
  95.    }
  96.    
  97.     public boolean equals(Object obj)
  98.     {
  99.         Block lhs = (Block) obj;
  100.         if(this.getX()== lhs.getX() && this.getY() == lhs.getY() && this.getHeight() == lhs.getHeight() && this.getWidth() == lhs.getWidth() && this.getColor() == lhs.getColor())
  101.             return true;
  102.         return false;
  103.     }  
  104.  
  105.    //add the other get methods
  106.     public int getX()
  107.     {
  108.         return xPos;
  109.     }
  110.    
  111.     public int getY()
  112.     {
  113.         return yPos;
  114.     }
  115.    
  116.     public int getWidth()
  117.     {
  118.         return width;
  119.     }
  120.    
  121.     public int getHeight()
  122.     {
  123.         return height;
  124.     }  
  125.  
  126.     public Color getColor()
  127.    {
  128.        return color;
  129.    }
  130.  
  131.    //add a toString() method  - x , y , width, height, color
  132.     public String toString()
  133.     {
  134.         return getX() + " " + getY() + " " + getWidth() + " " + getHeight() + " " + getColor();
  135.     }
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement