arahman22

EllipseNode.java

Feb 21st, 2012
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.geom.Ellipse2D;
  4.  
  5.  
  6. public class EllipseNode extends GraphElement
  7. {
  8.     Ellipse2D ellipse;
  9.     protected String label;
  10.     private boolean hasLabel;
  11.     boolean selected = false;
  12.    
  13.     public EllipseNode (double x, double y)
  14.     {
  15.  
  16.         super(x, y);
  17.         ellipse = new Ellipse2D.Double(super.xPos, super.yPos, super.Width, super.Height);
  18.         hasLabel = false;
  19.         selected = false;
  20.     }
  21.    
  22.     public void setLabel(String s)
  23.     {
  24.         hasLabel = true;
  25.         label = s;
  26.     }
  27.        
  28.     public double getXpos()
  29.     {
  30.         return xPos;
  31.     }
  32.    
  33.     public double getYpos()
  34.     {
  35.         return yPos;
  36.     }
  37.    
  38.     public double getWidth()
  39.      {
  40.         return super.Width;
  41.      }
  42.    
  43.     public double getHeight()
  44.      {
  45.         return super.Height;
  46.      }
  47.    
  48.     @Override
  49.     void draw(Graphics2D g2)
  50.     {
  51.         g2.setColor(Color.BLACK);
  52.         if (selected) g2.setColor(Color.BLUE);
  53.         if (ellipse != null)
  54.             g2.draw (ellipse);
  55.         if (hasLabel)
  56.             if (label.length() > 0)
  57.                 g2.drawString(label, (int) xPos + 10, (int) yPos + 20);
  58.        
  59.     }
  60.    
  61.     @Override
  62.     void setSelected(double x, double y)
  63.     {
  64.         if (isSelected(x, y))
  65.         {
  66.             System.out.println ("This selected");
  67.             this.selected = true;
  68.         }
  69.         else
  70.         {   System.out.println ("This unselected");
  71.             this.selected = false;
  72.         }
  73.     }
  74.  
  75.     @Override
  76.     boolean isSelected(double x, double y)
  77.     {
  78.         if (( x > xPos && x < xPos + super.Width) & ( y > yPos && y < yPos + super.Height))
  79.             return true;
  80.         return false;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment