package pipe.dataLayer; import pipe.gui.Constants; import java.awt.Color; import java.awt.Shape; import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.*; import java.awt.*; import pipe.gui.ModeAdaptor; import javax.swing.SwingConstants; import javax.swing.BorderFactory; import java.awt.event.*; /** * Place - Petri-Net Place Class * * @see

PNML - Petri-Net XMLSchema (stNet.xsd) * @see

UML - PNML Package

* @version 1.0 * @author James D Bloom */ public class Place extends PlaceTransitionObject implements Constants { /** Initial Marking */ private Integer initialMarking = null; /** Current Marking */ private Integer currentMarking = null; /** Initial Marking X-axis Offset */ private Double markingOffsetX = null; /** Initial Marking Y-axis Offset */ private Double markingOffsetY = null; /** Ellipse2D.Double place */ private Ellipse2D.Double place; /** Place Width */ public static final int WIDTH = 30; /** Place Width */ public static final int HEIGHT = 30; /** Token Width */ public static int tWidth = 5; /** Token Height */ public static int tHeight = 5; /** * Create Petri-Net Place object * * @param positionXInput X-axis Position * @param positionYInput Y-axis Position * @param idInput Place id * @param nameInput Name * @param nameOffsetXInput Name X-axis Position * @param nameOffsetYInput Name Y-axis Position * @param initialMarkingInput Initial Marking * @param markingOffsetXInput Marking X-axis Position * @param markingOffsetYInput Marking Y-axis Position * @param color Color */ public Place(double positionXInput, double positionYInput, String idInput, String nameInput, double nameOffsetXInput, double nameOffsetYInput, int initialMarkingInput, double markingOffsetXInput, double markingOffsetYInput, Color color){ super(positionXInput, positionYInput, idInput, nameInput, nameOffsetXInput, nameOffsetYInput, color); initialMarking = new Integer(initialMarkingInput); currentMarking = new Integer(initialMarkingInput); markingOffsetX = new Double(markingOffsetXInput); markingOffsetY = new Double(markingOffsetYInput); width = WIDTH; height = HEIGHT; place = new Ellipse2D.Double(0, 0, width, height); } /** * Create Petri-Net Place object * * @param positionXInput X-axis Position * @param positionYInput Y-axis Position * @param idInput Place id * @param color Color */ public Place(double positionXInput, double positionYInput, String idInput, Color color){ super(positionXInput, positionYInput, idInput, color); width = WIDTH; height = HEIGHT; place = new Ellipse2D.Double(0, 0, width, height); } /** * Create Petri-Net Place object * * @param positionXInput X-axis Position * @param positionYInput Y-axis Position * @param color - modified by aed02 */ public Place(double positionXInput, double positionYInput, Color color){ super(positionXInput, positionYInput, color); width = WIDTH; height = HEIGHT; place = new Ellipse2D.Double(0, 0, width, height); } /** * Create empty Petri-Net Place object * */ public Place(){ } /** * Paints the Place component taking into account the number of tokens from the currentMarking * @param g The Graphics object onto which the Place is drawn. */ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; Insets insets = getInsets(); int x = insets.left; int y = insets.top; int currentWidth = getWidth() - x - insets.right; int currentHeight = getHeight() - y - insets.bottom; g2.setPaint(getColor()); g2.setStroke(new BasicStroke(1.5f)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(getColor()); g2.draw(place); g2.fill(place); int marking = getCurrentMarking(); g.setColor(Color.WHITE); // structure sees how many markings there are and fills the place in with the appropriate number. switch(marking) { case 5: g.drawOval(x+6,y+6,tWidth,tHeight); g.fillOval(x+6,y+6,tWidth,tHeight); case 4: g.drawOval(x+18,y+20,tWidth,tHeight); g.fillOval(x+18,y+20,tWidth,tHeight); case 3:g.drawOval(x+6,y+20,tWidth,tHeight); g.fillOval(x+6,y+20,tWidth,tHeight); case 2: g.drawOval(x+18,y+6,tWidth,tHeight); g.fillOval(x+18,y+6,tWidth,tHeight); case 1: g.drawOval(x+12,y+13,tWidth,tHeight); g.fillOval(x+12,y+13,tWidth,tHeight);break; case 0: break; default: g.drawString(String.valueOf(marking),x+5,y+20); } } /** * Set initial marking * * @param initialMarkingInput Integer value for initial marking */ public void setInitialMarking(int initialMarkingInput) { initialMarking = new Integer(initialMarkingInput); } /** * Set current marking * * @param currentMarkingInput Integer value for current marking */ public void setCurrentMarking(int currentMarkingInput) { currentMarking = new Integer(currentMarkingInput); } /** * Set X-axis offset for initial marking * * @param markingOffsetXInput Integer value for X-axis offset of initial marking */ public void setmarkingOffsetX(double markingOffsetXInput) { markingOffsetX = new Double(markingOffsetXInput); } /** * Set Y-axis offset for initial marking * * @param markingOffsetYInput Integer value for Y-axis offset of initial marking */ public void setmarkingOffsetY(double markingOffsetYInput) { markingOffsetY = new Double(markingOffsetYInput); } /** * Get initial marking * * @return Integer value for initial marking */ public int getInitialMarking() { if(initialMarking == null) return 0; return initialMarking.intValue(); } /** * Get current marking * * @return Integer value for current marking */ public int getCurrentMarking() { if (currentMarking == null) return 0; else return currentMarking.intValue(); } /** * Get X-axis offset for initial marking * * @return Double value for X-axis offset of initial marking */ public double getMarkingOffsetX() { if(markingOffsetX == null) return 0; return markingOffsetX.intValue(); } /** * Get Y-axis offset for initial marking * * @return Double value for X-axis offset of initial marking */ public double getMarkingOffsetY() { if(markingOffsetY == null) return 0; return markingOffsetY.intValue(); } /** * Get initial marking * * @return Integer value for initial marking */ public Integer getInitialMarkingObject() { return initialMarking; } /** * Get current marking * * @return Integer value for current marking */ public Integer getCurrentMarkingObject() { return currentMarking; } /** * Get X-axis offset for initial marking * * @return Double value for X-axis offset of initial marking */ public Double getMarkingOffsetXObject() { return markingOffsetX; } /** * Get Y-axis offset for initial marking * * @return Double value for X-axis offset of initial marking */ public Double getMarkingOffsetYObject() { return markingOffsetY; } /** * Modifies start and end X and Y coords - 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){ } /* Used to change the position of the component as it is dragged along */ public void updateSize(MouseEvent e) { Insets insets = getInsets(); int eventX = e.getX(); int eventY = e.getY(); setBounds(eventX + insets.left + getX()-leftOffset(),eventY + insets.top + getY()-topOffset(),getWidth(),getHeight()); //if the object has been moved, the name label 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) {} /** method indicates which button at the top of the panel has been pressed - this requires implementation properly. * How do we get the proper type ? */ public int getModeType() { return ModeAdaptor.getMode(); } /** Returns the width bounds we want to use when initially creating the place on the gui * @return Width bounds of Place */ public int boundsWidth() { return WIDTH +1; } /** Returns the height bounds we want to use when initially creating the place on the gui * @return Height bounds of Place */ public int boundsHeight() { return HEIGHT +1; } /** 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 * @return Top offset of Place */ 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 * @return Left offset of Place */ public int leftOffset() { return boundsWidth() /2; } }