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