Interpreter2.java
1    /* 
2     * This interpreter is intended to paint different colored dots in a window. 
3     * 
4     * The commands that the interpreter can recognize and respond to are: 
5     *  - BLUE: paint a blue dot 
6     *  - RED: paint a red dot 
7     *  - YELLOW: paint a yellow dot 
8     *  - GREEN: paint a green dot 
9     *  - HELP: show a list of the commands in a dialog message box 
10    *  - EXIT: terminate the program 
11    * 
12    */
13   package interpreters;
14   import java.awt.Color;
15   import javax.swing.JOptionPane;
16   import javax.swing.SwingUtilities;
17   import painter.SPainter;
18   import shapes.SCircle;
19   
20   public class Interpreter2
21   {
22       private void interpreter()
23       {
24           // CREATE OBJECTS TO THINK WITH
25           SPainter miro = new SPainter("Dot Thing",400,400);
26           miro.setScreenLocation(0,0);
27           SCircle dot = new SCircle(180);
28           // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT
29           while ( true )
30           {
31               String command = JOptionPane.showInputDialog(null,"Command?");
32               if( command == null )
33               {
34                   command = "exit";
35               }
36               // user clicked on Cancel
37               if ( command.equalsIgnoreCase("blue") )
38               {
39                   miro.setColor(Color.BLUE);miro.paint(dot);
40               }
41               else if ( command.equalsIgnoreCase("red") )
42               {
43                   miro.setColor(Color.RED);miro.paint(dot);
44               }
45               else if ( command.equalsIgnoreCase("yellow") )
46               {
47                   miro.setColor(Color.YELLOW);miro.paint(dot);
48               }
49               else if ( command.equalsIgnoreCase("green") )
50               {
51                   miro.setColor(Color.GREEN);miro.paint(dot);
52               }
53               else if ( command.equalsIgnoreCase("help") )
54               {
55                   JOptionPane.showMessageDialog(null,"Valid commands are: "+ "RED | BLUE | YELLOW | GREEN | HELP | EXIT ");
56               }
57               else if ( command.equalsIgnoreCase("exit") )
58               {
59                   miro.end();System.out.println("Thank you for viewing the dots ...");break;
60               }
61               else
62               {
63                   JOptionPane.showMessageDialog(null,"Unrecognizable command: " + command.toUpperCase());
64               }
65           }
66       }
67       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
68       public Interpreter2()
69       {
70           interpreter();}public static void main(String[] args)
71   {
72       SwingUtilities.invokeLater(new Runnable() {public void run() {new Interpreter2();
73       }
74       });
75   }
76   }
77