arahman22

RectangleNode.java

Feb 21st, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. //Imports the elements
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.Rectangle2D;
  5.  
  6.  
  7. public class RectangleNode extends GraphElement
  8. {
  9.     //The variables
  10.     double xPos, yPos;
  11.     Rectangle2D rectangle;      // The rectangle
  12.     private String label;       // Label for the rectangle
  13.     private boolean hasLabel;   // Does the rectangle have a label?
  14.     boolean selected;   // Is the rectangle selected?
  15.  
  16.     // The constructors
  17.     // All rectangles have a default set height
  18.     /**
  19.         Creates a rectangle at (0, 0)
  20.         @param void
  21.      */
  22.     /**
  23.     public RectangleNode()
  24.     {
  25.         rectangle = new Rectangle2D.Double(0, 0, super.Width, super.Height);
  26.     }
  27.     */
  28.    
  29.     /**
  30.         Creates a rectangle at the given position
  31.         @param The x and y position of the rectangle
  32.      */
  33.     public RectangleNode(double xPos,double yPos)
  34.     {
  35.         super(xPos, yPos);
  36.         rectangle = new Rectangle2D.Double(super.xPos, super.yPos, super.Width, super.Height);
  37.         hasLabel = false;
  38.         selected = false;
  39.     }
  40.  
  41.     /**
  42.         Draws the rectangle
  43.         @param The Graphics2D element
  44.      */
  45.     @Override
  46.     void draw(Graphics2D g2)
  47.     {  
  48.         //g2.setColor(super.defaultColor);
  49.         if (selected) g2.setColor(Color.RED);   /*  Personal note: Putting setColor at
  50.                                                     the end of the draw method is going
  51.                                                     to only further confuse myself when it
  52.                                                     doesn't work.
  53.                                                 */
  54.         else
  55.             g2.setColor(Color.BLACK);
  56.         if (rectangle != null)  // ie A Rectangle exists
  57.             g2.draw(rectangle); // Draw the Rectangle
  58.         if (hasLabel)
  59.             if (label.length() > 0)
  60.                 g2.drawString(label, (int) xPos + 5, (int) yPos + 15);
  61.        
  62.     }
  63.  
  64.     /**
  65.         Get the width of the rectangle
  66.         @return The width of the rectangle
  67.     */
  68.     public double getWidth()
  69.     {
  70.         return super.Width;
  71.     }
  72.    
  73.     /**
  74.         Adds a label to the rectangle
  75.         @param The label to be set
  76.     */
  77.     public void setLabel(String s)
  78.     {
  79.             label = s;
  80.             hasLabel = true;
  81.     }
  82.  
  83.     public double getHeight()
  84.     {
  85.         return super.Height;
  86.     }
  87.  
  88.     @Override
  89.     public void setSelected(double x, double y)
  90.     {
  91.         if (isSelected(x, y))
  92.         {
  93.             selected = true;
  94.         }
  95.         else
  96.         {  
  97.             selected = false;
  98.         }
  99.     }
  100.    
  101.     @Override
  102.     boolean isSelected(double x, double y)
  103.     {
  104.         if (( x > xPos && x < xPos + getWidth()) & ( y > yPos && y < yPos + getHeight()))
  105.             return true;
  106.         return false;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment