package pipe.dataLayer; import pipe.gui.Constants; import java.awt.Color; import java.awt.Shape; import java.awt.Point; import java.awt.geom.*; import java.awt.Rectangle; import java.awt.*; import java.awt.event.*; import javax.swing.BorderFactory; /** * Transition - Petri-Net Transition Class * * @see
* @version 1.0 * @author James D Bloom */ public class Transition extends PlaceTransitionObject implements Constants { /** Transition is of Rectangle2D.Double*/ private Rectangle2D.Double transition; /** Place Width */ public static final int WIDTH = 10; /** Place Width */ public static final int HEIGHT = 25; private boolean enabled = false; private boolean enabledBackwards = false; public boolean highlighted = false; /** The transition rate */ public int rate = 1; /** The transform */ private AffineTransform transform; /** Counter for the Objects */ // private static int count = 0; /** Used for a unique id */ //private static int idCount = 0; /** * Create Petri-Net Transition object * * @param positionXInput X-axis Position * @param positionYInput Y-axis Position * @param idInput Transition id * @param nameInput Name * @param nameOffsetXInput Name X-axis Position * @param nameOffsetYInput Name Y-axis Position * @param color Color */ public Transition(double positionXInput, double positionYInput, String idInput, String nameInput, double nameOffsetXInput, double nameOffsetYInput, Color color){ super(positionXInput, positionYInput, idInput, nameInput, nameOffsetXInput, nameOffsetYInput, color); width = WIDTH; //sets width height = HEIGHT;//sets height // transition = new Rectangle2D.Double(positionXInput, positionYInput, width, height); transition = new Rectangle2D.Double(0, 0, width, height); //enabled = true; //this is for testing purposes only // transition = new Rectangle2D.Double(0, 0, width, height); transition = new Rectangle2D.Double( (boundsWidth()-width)/2, (boundsHeight()-height)/2, width, height); } /** * Create Petri-Net Transition object * * @param positionXInput X-axis Position * @param positionYInput Y-axis Position * @param idInput Transition id * @param color Color */ public Transition(double positionXInput, double positionYInput, String idInput, Color color){ super(positionXInput, positionYInput, idInput, color); width = WIDTH; //sets width height = HEIGHT;//sets height transition = new Rectangle2D.Double(0, 0, width, height); // enabled = true; //this is for testing purposes only //transition = new Rectangle2D.Double(0, 0, width, height); transition = new Rectangle2D.Double( (boundsWidth()-width)/2, (boundsHeight()-height)/2, width, height); } /** * Create Petri-Net Transition object * * @param positionXInput X-axis Position * @param positionYInput Y-axis Position * @param color Color */ public Transition(double positionXInput, double positionYInput, Color color) { super(positionXInput, positionYInput, color); width = WIDTH; //sets width height = HEIGHT;//sets height transition = new Rectangle2D.Double(0, 0, width, height); //enabled = true; //this is for testing purposes only // we set the bounds of the rectangle in the centre of the containing component. // this makes the rotation work ok. transition = new Rectangle2D.Double( (boundsWidth()-width)/2, (boundsHeight()-height)/2, width, height); } /** * Create Petri-Net Transition object * */ public Transition(){ } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; //we might rotate so we save the transform AffineTransform save = g2.getTransform(); if (transform != null) { g2.transform(transform); } Insets insets = getInsets(); int currentWidth = getWidth() - insets.left - insets.right; int currentHeight = getHeight() - insets.top - insets.bottom; if(highlighted==true){ //System.out.println("highlight colour"); g2.setPaint(Color.RED); g2.setStroke(new BasicStroke(1.5f)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } else if(highlighted == false) { g2.setPaint(getColor()); g2.setStroke(new BasicStroke(1.5f)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } g2.draw(transition); g2.fill(transition); } /** Rotates the Transition through the specified angle around the midpoint */ public void rotate(double angle) { //setBorder(BorderFactory.createLineBorder(Color.BLACK)); // is this the first time we've rotated ? if (transform == null) { transform = new AffineTransform(); } transform.rotate(Math.toRadians(angle),(double)boundsWidth()/2,(double)boundsHeight()/2); repaint(); } /** * Determines whether Transition is enabled * @param animationStatus Anamation status * @return True if enabled */ public boolean isEnabled(boolean animationStatus){ if(animationStatus==true){ // System.out.println("is Enabled called within Transition"); if(enabled==true){ highlighted = true; return true; } else{ highlighted = false; return false; } } else return false; } /** * Determines whether Transition is enabled backwards * @return True if enabled */ public boolean isEnabledBackwards(){ return enabledBackwards; } /** * Determines whether Transition is enabled * @return True if enabled */ public boolean isEnabled(){ return enabled; } /** * Sets whether Transition is enabled * @status enabled if True */ public void setEnabled(boolean status){ enabled = status; } /** * Sets whether Transition is enabled * @status enabled if True */ public void setEnabledBackwards(boolean status){ enabledBackwards = status; } /* called at the end of animation to reset Transitions to false*/ public void setEnabledFalse(){ enabled = false; highlighted = false; } /** * Modifies start and end coordinates of shape - does nothing * @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){ } /** * Called when the object is moved around from the MouseDragged and MouseReleased events */ public void updateSize(MouseEvent e) { //System.out.println("updating transition size..."); Insets insets = getInsets(); int eventX = e.getX(); int eventY = e.getY(); setBounds(eventX + insets.left + getX()-leftOffset() ,eventY + insets.top + getY() -topOffset(),getWidth(),getHeight()); //namelabel's position needs to be updated as well pnname.boundsSet(eventX + insets.left + getX()-leftOffset() ,eventY + insets.top + getY() -topOffset(),getWidth(),getHeight()); positionX = new Double(eventX + insets.left + getX()-leftOffset()); positionY = new Double(eventY + insets.top + getY() -topOffset()); } /* unimplemented methods at present */ public void mouseMoved(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} /** Returns the width bounds we want to use when initially creating the place on the gui */ public int boundsWidth() { return WIDTH+25; } /** Returns the height bounds we want to use when initially creating the place on the gui */ public int boundsHeight() { return HEIGHT+15; } /** Returns the distance between the outside of the component to the centre, in order to position the centre of the place where the mouse clicks on the screen */ public int topOffset() { return boundsHeight() /2; } /** Returns the distance between the outside of the component to the centre, in order to position the centre of the place where the mouse clicks on the screen */ public int leftOffset() { return boundsWidth() /2; } public void setRate(int _rate){ rate = _rate; } public int getRate() { return rate; } }