import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class GraphicsSampler extends JApplet implements Runnable{ Thread animator; Color fill = Color.gray; // Equal red, green, and blue == gray Color outline = Color.black; // Same as new Color(0, 0, 255) Color conditionDrift = Color.yellow; Color CurrentStateColor = Color.red; Color ruleColor = Color.blue; Color textcolor = Color.green; // Same as new Color(255, 0, 0) Color BackgroundColor = Color.lightGray; Font fnTape = new Font("times", Font.BOLD, 30);; // The font we use for text Font fnRule = new Font("times", Font.PLAIN, 20); FontMetrics metrics; // Information about font size Image image, background; // Some images we draw with StateStructure TMStateInfo[] = new StateStructure[100]; JTextField Text; String Language,CurrentState; String SourceURL = ""; int CurrentIndex = 0; int CurrentStateID = -1; int IndexNum = 0; String CurrentInput = ""; Input inputer; private static int uninitialized = -1; private static int initialized = 0; private static int running = 1; private static int emptyString = 2; int SystemState = uninitialized; int currentState = 0; JToolBar tb = new JToolBar(); JButton btLoad = new JButton("LOAD", new ImageIcon("load.png")); JButton btRun = new JButton("RUN", new ImageIcon("run.gif")); JButton btExit = new JButton ("EXIT", new ImageIcon("exit.gif")); ActionListener JML = new ActionListener() { public void actionPerformed(ActionEvent e) { String temp = ((JButton)e.getSource()).getText(); if(temp.equalsIgnoreCase("exit")) { System.exit(0); } if(temp.equalsIgnoreCase("run")) { if(SystemState == initialized) { SetInitialState(); String txt = Text.getText(); if(txt.length()==0){ JPanel p = new JPanel(); JOptionPane.showMessageDialog(p, "please input language int the bottom Textfield"); } else { SystemState = running; Language = txt+"$"; } } else { JPanel p = new JPanel(); JOptionPane.showMessageDialog(p, "please load first"); } } if(temp.equalsIgnoreCase("load")) { if(SystemState!=running) { JFileChooser filech = new JFileChooser(); JPanel p = new JPanel(); int result = filech.showOpenDialog(p); if(result ==JFileChooser.APPROVE_OPTION) { SourceURL = filech.getName(filech.getSelectedFile()); System.out.println(SourceURL); inputer = new Input(SourceURL); IndexNum = inputer.intializeStateStructure(TMStateInfo); if (Checkduplicate(TMStateInfo)) { SystemState = initialized; //JOptionPane.showMessageDialog(p, "Turing Machine State Source File Error"); } } repaint(); SystemState = initialized; Language = ""; } } } }; public void init() { // Initialize color resources. Note the use of the Color() constructor // and the use of pre-defined color constants. for(int i = 0;i<100;i++) TMStateInfo[i] = new StateStructure(); metrics = this.getFontMetrics(fnTape); this.setSize(500, 500); this.setBackground(BackgroundColor); CurrentState = "q1"; ComponentListener WL = new ComponentListener(){ public void componentResized(ComponentEvent e) { repaint(); setVisible(false); setVisible(true); Text.setVisible(false); Text.setVisible(true); tb.setVisible(false); tb.setVisible(true); repaint(); } public void componentHidden(ComponentEvent e) { Text.setVisible(false); //Text.setVisible(true); tb.setVisible(false); // mb.setVisible(true); } public void componentMoved(ComponentEvent e) { Text.setVisible(false); Text.setVisible(true); tb.setVisible(false); tb.setVisible(true); repaint(); } public void componentShown(ComponentEvent e) { Text.setVisible(false); Text.setVisible(true); tb.setVisible(false); tb.setVisible(true); repaint(); } }; this.addComponentListener(WL); btLoad.addActionListener(JML); btRun.addActionListener(JML); btExit.addActionListener(JML); tb.add(btLoad); tb.add(btRun); tb.add(btExit); Text = new JTextField(30); Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(BorderLayout.NORTH,tb); cp.add(BorderLayout.SOUTH,Text); setVisible(false); setVisible(true); tb.setVisible(false); tb.setVisible(true); Text.setVisible(false); Text.setVisible(true); } // This method is called whenever the applet needs to be drawn or redrawn public void paint(Graphics g) { g.setFont(fnTape); // Specify the font we'll be using throughout super.paint(g); this.setBackground(BackgroundColor); if(this.SystemState==running) { ShowLanguage(Language, g,TMStateInfo[0]); if(CurrentState.equalsIgnoreCase("qreject")) { ShowResult(g,"Reject"); this.SystemState = initialized; } if(CurrentState.equalsIgnoreCase("qaccept")) { ShowResult(g,"Accept"); this.SystemState = initialized; } } boolean match = false; for(int i = 0;i< IndexNum;i++) { if((this.SystemState==running)&&(TMStateInfo[i].NextState(Language, CurrentIndex, CurrentState))) { this.ShowRule(g, TMStateInfo[i]); match = true; CurrentState = TMStateInfo[i].stateToTransfer; String temp = Language.substring(0, CurrentIndex)+TMStateInfo[i].edit+Language.substring(CurrentIndex+1, Language.length()); Language = temp; if(TMStateInfo[i].LRaction.equalsIgnoreCase("L")) CurrentIndex--; else CurrentIndex++; //ClearLanguage(Language,g,TMStateInfo[i]); g.finalize(); //g.dispose(); break; } } if((!match)&&(SystemState==running)) { SystemState = initialized; String temp =String.format("no suitable rule for current input %s under current state %s", Language.substring(CurrentIndex, CurrentIndex+1), CurrentState); JOptionPane.showMessageDialog(this, temp); } } protected void centerText(String s1, String s2, Graphics g, Color c, int x, int y, int w, int h) { int height = metrics.getHeight(); // How tall is the font? int ascent = metrics.getAscent(); // Where is the font baseline? int width1=0, width2 = 0, x0=0, x1=0, y0=0, y1=0; width1 = metrics.stringWidth(s1); // How wide are the strings? if (s2 != null) width2 = metrics.stringWidth(s2); x0 = x + (w - width1)/2; // Center the strings horizontally x1 = x + (w - width2)/2; if (s2 == null) // Center one string vertically y0 = y + (h - height)/2 + ascent; else { // Center two strings vertically y0 = y + (h - (int)(height * 2.2))/2 + ascent; y1 = y0 + (int)(height * 1.2); } g.setColor(c); // Set the color g.drawString(s1, x0, y0); // Draw the strings if (s2 != null) g.drawString(s2, x1, y1); } protected void ClearLanguage(String s1,Graphics g, StateStructure TMState) { if(SystemState==running){ int height = metrics.getHeight(); // How tall is the font? int tempWidth;// Where is the font baseline? int x0 = (1024)/2; int y0 = (768)/2; int tempx = 0; int tempy = y0; String TempString = ""; if (this.CurrentIndex>0) { TempString = Language.substring(0,CurrentIndex); // g.setColor(outline); // g.drawString(TempString,tempx,tempy); } TempString = TempString+ CurrentState; TempString = Language.substring(CurrentIndex, CurrentIndex+1); if(this.CurrentIndex (" + TMState.stateToTransfer +" , " + TMState.edit + " , " + TMState.LRaction+")"; g.setFont(fnRule); g.setColor(ruleColor); FontMetrics metrics = this.getFontMetrics(fnRule); x0 -= metrics.stringWidth(result)/2; g.drawString(result, x0, y0); } protected void ShowLanguage(String s1, Graphics g, StateStructure TMState) { if(SystemState==running){ int height = metrics.getHeight(); // How tall is the font? int tempWidth;// Where is the font baseline? int x0 = this.getWidth()/2; int y0 = this.getHeight()/2; int tempx = x0; int tempy = y0; String TempString = ""; g.setFont(fnTape); if (this.CurrentIndex>0) { TempString = Language.substring(0,CurrentIndex); tempWidth = metrics.stringWidth(TempString); tempx = x0-tempWidth; g.setColor(outline); g.drawString(TempString,tempx,tempy); } tempWidth = metrics.stringWidth(TempString); tempx = tempx+ tempWidth; TempString = CurrentState; g.setColor(CurrentStateColor); g.drawString(TempString, tempx, y0); if(CurrentIndex