📄 petri.java
字号:
/* Petri Net Simulator Author: Torguet Patrice Version: 1.0 Date: 3/25/95 Notes: based on Kyril Faenov's Finite State Machine Simulator Todo:*//* IMPORTS*/import java.applet.*;import java.awt.*;import java.lang.*;import java.util.*;import java.io.*;import java.net.*;import PetriNet;/* INTERFACES*//* PlaceTransitionIdResolver interface - classes complying to this interface must implement resolveId routine, which finds state object based on specified id.*/interface PlaceTransitionIdResolver{ /* return place object with specified id. */ Place resolvePlaceId (int id); /* return transition object with specified id. */ Transition resolveTransitionId (int id);} /* end PlaceTransitionIdResolver *//* PUBLIC CLASSES*//* Simulator class - this is the top-level applet class. It does all of the initialization, event processing, re-paint initiation and dialog window management.*/public class Petri extends Applet{ /* CLASS CONSTANTS */ /* constants secifying what to do on the next mouse click */ /* regular state */ final static int CLICK_NORMAL = 0; /* adding new place */ final static int CLICK_PLACE_ADD = 1; /* deleting place */ final static int CLICK_PLACE_REMOVE = 2; /* adding new trnasition */ final static int CLICK_TRANSITION_ADD = 3; /* removing transition */ final static int CLICK_TRANSITION_REMOVE = 4; /* selecting a place/trans for new arc */ final static int CLICK_START_NEW_ARC = 5; /* selecting a place for new arc end */ final static int CLICK_PLACE_END_NEW_ARC = 6; /* selecting a trans for new arc end */ final static int CLICK_TRANS_END_NEW_ARC = 7; /* running simulation */ final static int CLICK_RUN_SIMULATION = 8; /* selecting place/trans of the arc to be removed */ final static int CLICK_START_REMOVE_ARC = 9; /* selecting place at the end of arc to be removed */ final static int CLICK_PLACE_END_REMOVE_ARC = 10; /* selecting trans at the end of arc to be removed */ final static int CLICK_TRANS_END_REMOVE_ARC = 11; /* adding token to a place */ final static int CLICK_ADD_TOKEN = 12; /* removing token from a place */ final static int CLICK_REMOVE_TOKEN = 13; /* applet's dimensions */ final static int DIMENSION = 500; /* height of the button control area */ final static int CONTROL_HEIGHT = 75; /* INSTANCE VARIABLES */ /* petri network */ private PetriNet network; /* hint line support module */ private Help help; /* specifies what to do on the next mouse click (see the constants above) */ private int clickState; /* if set - clear the application panel on the next re-draw */ private boolean clearAll; /* if set - draggin of a place/trans is in progress */ private boolean dragging; /* sub-dialog classes */ private QuitDialog quitDialog; private AboutBox aboutBox; private FileInput fileInput; private Panel buttons; /* PUBLIC METHODS */ /* called when applet is being initialized */ public void init () { /* initialize instance variables */ clickState = 0; clearAll = true; dragging = false; /* create help and network class instances */ help = new Help (); network = new PetriNet (); /* set applet size */ resize (DIMENSION, DIMENSION); /* create button bar panel */ buttons = new Panel (); /* set layout within the panel to 5 elements per row */ buttons . setLayout (new GridLayout (0, 5)); /* add all buttons */ buttons . add (new Button ("New")); buttons . add (new Button ("Load")); buttons . add (new Button ("Save")); buttons . add (new Button ("About")); buttons . add (new Button ("Quit")); buttons . add (new Button ("Add Place")); buttons . add (new Button ("Add Token")); buttons . add (new Button ("Add Transition")); buttons . add (new Button ("Add Arc")); buttons . add (new Button ("Run")); buttons . add (new Button ("Del Place")); buttons . add (new Button ("Del Token")); buttons . add (new Button ("Del Transition")); buttons . add (new Button ("Del Arc")); buttons . add (new Button ("Stop")); /* add button panel to applet panel */ add (buttons); /* resize bar panel and position it at the top of the applet */ buttons . resize (DIMENSION, CONTROL_HEIGHT); buttons . move (0, 0); buttons . show (); /* create dialog panels, add them to applet panel and resize to desired dimensions. for some reason resizing within the dialog itself seems to have no effect. i guess i am missing something here. note that dialogs are initialized hidden and will not show up until show() method is invoked on them. */ aboutBox = new AboutBox (this); quitDialog = new QuitDialog (this); fileInput = new FileInput (this); add (aboutBox); add (quitDialog); add (fileInput); aboutBox . resize (350, 120); quitDialog . resize (100, 75); fileInput . resize (400, 120); Rectangle bounds = bounds(); Rectangle abounds; abounds = quitDialog . bounds (); quitDialog . move (bounds . x + (bounds . width - abounds . width) / 2, bounds . y + (bounds . height - abounds . height) / 2); abounds = aboutBox . bounds(); aboutBox . move (bounds . x + (bounds . width - abounds . width) / 2, bounds . y + (bounds . height - abounds . height) / 2); abounds = fileInput . bounds(); fileInput . move (bounds . x + (bounds . width - abounds . width) / 2, bounds . y + (bounds . height - abounds . height) / 2); /* let the dialogs know about the network - they will have to communicate user commands and input to it */ fileInput . setPetriNet (network, help); /* display initial message on the hint line */ help . setHelp (help . INITIAL); /* make applet show now */ show (); } /* end init */ /* override the layout method with the one that does nothing. we do not need layout for the applet panel since all the positioning being done manually. */ public synchronized void layout () { } /* end layout */ /* this method is called when repaint of the panle is requested */ public void paint (Graphics g) { Rectangle size = bounds (); /* if cleanup was requested - clear the entire panel */ if (clearAll) { clearAll = false; g . setColor (Color . lightGray); g . fillRect (0, 0, size . width, size . height); } /* repaint the components */ buttons . paint (g); network . paint (g, CONTROL_HEIGHT + 5); help . paint (g, size . width, size . height); } /* end paint */ /* this method handles all of the the events for the applet */ public boolean handleEvent (Event evt) { switch (evt . id) { case Event . MOUSE_DOWN: switch (clickState) { case CLICK_PLACE_ADD: /* tell network to create a new place at the location of the mouse click */ if (! network . addPlace (evt . x, evt . y)) help . setHelp (help . MAX_PLACES); else help . setHelp (help . EMPTY); break; case CLICK_PLACE_REMOVE: /* tell network to remove a place at the location of the mouse click */ if (! network . removePlace (evt . x, evt . y)) help . setHelp (help . NO_PLACE); break; case CLICK_TRANSITION_ADD: /* tell network to create a new place at the location of the mouse click */ if (! network . addTransition (evt . x, evt . y)) help . setHelp (help . MAX_TRANSITIONS); else help . setHelp (help . EMPTY); break; case CLICK_TRANSITION_REMOVE: /* tell network to remove a place at the location of the mouse click */ if (! network . removeTransition (evt . x, evt . y)) help . setHelp (help . NO_TRANSITION); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -