Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Imports the elements
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.geom.Rectangle2D;
- public class RectangleNode extends GraphElement
- {
- //The variables
- double xPos, yPos;
- Rectangle2D rectangle; // The rectangle
- private String label; // Label for the rectangle
- private boolean hasLabel; // Does the rectangle have a label?
- boolean selected; // Is the rectangle selected?
- // The constructors
- // All rectangles have a default set height
- /**
- Creates a rectangle at (0, 0)
- @param void
- */
- /**
- public RectangleNode()
- {
- rectangle = new Rectangle2D.Double(0, 0, super.Width, super.Height);
- }
- */
- /**
- Creates a rectangle at the given position
- @param The x and y position of the rectangle
- */
- public RectangleNode(double xPos,double yPos)
- {
- super(xPos, yPos);
- rectangle = new Rectangle2D.Double(super.xPos, super.yPos, super.Width, super.Height);
- hasLabel = false;
- selected = false;
- }
- /**
- Draws the rectangle
- @param The Graphics2D element
- */
- @Override
- void draw(Graphics2D g2)
- {
- //g2.setColor(super.defaultColor);
- if (selected) g2.setColor(Color.RED); /* Personal note: Putting setColor at
- the end of the draw method is going
- to only further confuse myself when it
- doesn't work.
- */
- else
- g2.setColor(Color.BLACK);
- if (rectangle != null) // ie A Rectangle exists
- g2.draw(rectangle); // Draw the Rectangle
- if (hasLabel)
- if (label.length() > 0)
- g2.drawString(label, (int) xPos + 5, (int) yPos + 15);
- }
- /**
- Get the width of the rectangle
- @return The width of the rectangle
- */
- public double getWidth()
- {
- return super.Width;
- }
- /**
- Adds a label to the rectangle
- @param The label to be set
- */
- public void setLabel(String s)
- {
- label = s;
- hasLabel = true;
- }
- public double getHeight()
- {
- return super.Height;
- }
- @Override
- public void setSelected(double x, double y)
- {
- if (isSelected(x, y))
- {
- selected = true;
- }
- else
- {
- selected = false;
- }
- }
- @Override
- boolean isSelected(double x, double y)
- {
- if (( x > xPos && x < xPos + getWidth()) & ( y > yPos && y < yPos + getHeight()))
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment