package pipe.dataLayer; import java.awt.Color; import java.awt.Shape; import java.awt.Point; import java.awt.geom.*; import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * ArrowLabel - Class which depicts an arrow attached to an ArcLabel. This class instead of the Arc will have MouseListeners attached to it. * * @see

ArrowLabel UML

* @version 1.0 * @author Camilla Clifford */ public class ArrowLabel extends PetriNetObject { private int width = 15; private int height = 15; private int x; private int y; private double rotate; private Line2D.Double line1; private Line2D.Double line2; private int arcx; private int arcy; private double midx; private double midy; /** we maintain a reference to the parent in order that we use the arrow to talk to the arc */ private Arc parent; /** Label which displays the weighting of the arc it's attached to. */ private NameLabel weightLabel; public ArrowLabel(Arc _parent) { parent = _parent; String initVal=""; if (parent.getWeighting() >1) initVal = String.valueOf(parent.getWeighting()); weightLabel = new NameLabel(initVal); // extract some useful information Rectangle arcBounds = parent.getBounds(); // get the upper lh corner of the enclosing arc arcx = arcBounds.x; arcy = arcBounds.y; // get the arc midpoint - this is where we want to attach the arrow. midx = arcBounds.getWidth()/2; midy = arcBounds.getHeight()/2; // set some initial x and y values to work from x = arcx; y = arcy; // set up the bounds and rotate of the arrow calcShapeSettings(parent.getQuad(), parent.getArcAngle()); //System.out.println("ArrowLabel bounds: " + x + " " + y ); // actually set the bounds setBounds(x,y,width,height); //System.out.println("WeightLabel bounds: " + (x-10) + " " + (y-10) ); //weightLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); weightLabel.setBounds(x-10,y-10,20,20); } /** This calculates the rotation angle we need to apply to the arrow, and will also handle setting the bounds as that would require this same switch statement. * * @param quad The quadrant in which the arc has been drawn (relates to the angle) * @param angle The angle of the drawn arc */ public void calcShapeSettings(int quad,double angle) { rotate = angle; switch(quad) { case Arc.NE: x += midx; y += midy; break; case Arc.SE: rotate = 180-rotate; x += midx; y += midy; break; case Arc.SW: rotate = 180 + rotate; x += midx; y+=midy; break; case Arc.NW: rotate = 360-rotate; x +=midx; y += midy; break; default: /*System.out.println("default")*/; } // we need to shift the bounds left by half the width of the arrows bounds x -= width/2; y -= height/2; } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setPaint(Color.BLACK); g2.setStroke(new BasicStroke(1.5f)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); AffineTransform save = g2.getTransform(); AffineTransform transform = new AffineTransform(); // rotate the arrow in line with the arc angle transform.rotate(Math.toRadians(rotate),(double)width()/2,(double)height/2); g2.transform(transform); line1 = new Line2D.Double( (double)width()/2,0,0,(double)height()); line2 = new Line2D.Double( (double)width()/2,0,(double)width(),(double)height()); g2.draw(line1); g2.draw(line2); g2.setTransform(save); } /** required by PetrinetObject, does nothing for now * @param e Mouse Event */ public void updateSize(MouseEvent e){ } /** * required by PetrinetObject, does nothing for now * @param startX Start X-axis position * @param startY Start Y-axis position * @param endX End X-axis position * @param endY End Y-axis position */ public void modify(double startX, double startY, double endX, double endY) { } /** Returns width without overriding the superclass methods * @return Width of label */ public int width() { return width; } /** * Returns height without overriding the superclass methods * @return Height of label */ public int height() { return height; } /** * Returns Label's Arc * @return Arc parent of label */ public Arc getArc() { return parent; } /** * Returns label which displays the weighting of the arc it's attached to. * @return Label which displays the weighting of the arc parent. */ public NameLabel getNameLabel() { return weightLabel; } }