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     * - GREEN: paint a green dot 
8     * - YELLOW: paint a yellow dot 
9     * - HELP: show a list of the commands in a dialog message box 
10    * - EXIT: terminate the program 
11    */
12   package interpreters;
13   
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       private void interpreter() {
22           // Create objects to "think" with.
23           SPainter gage = new SPainter("Dot Program", 400, 400);
24           gage.setScreenLocation(0, 0);
25           SCircle dot = new SCircle(180);
26   
27           // Repeatedly takes a command from an input dialog box and interpret it.
28           while (true) {
29               String command = JOptionPane.showInputDialog(null, "Command?");
30               if (command == null) {command = "exit";} // User clicks "Cancel" prompt
31               if (command.equalsIgnoreCase("blue")) {
32                   gage.setColor(Color.BLUE);
33                   gage.paint(dot);
34               } else if (command.equalsIgnoreCase("red")) {
35                   gage.setColor(Color.RED);
36                   gage.paint(dot);
37               } else if (command.equalsIgnoreCase("green")) {
38                   gage.setColor(Color.GREEN);
39                   gage.paint(dot);
40               } else if (command.equalsIgnoreCase("yellow")) {
41                   gage.setColor(Color.YELLOW);
42                   gage.paint(dot);
43               } else if (command.equalsIgnoreCase("help")) {
44                   JOptionPane.showMessageDialog(null,"Valid commands are: "
45                           + "RED | BLUE | GREEN | YELLOW | HELP | EXIT");
46               } else if (command.equalsIgnoreCase("exit")) {
47                   gage.end();
48                   System.out.println("Thank you for viewing the dots program.");
49                   break;
50               } else {
51                   JOptionPane.showMessageDialog(null, "Unrecognizable command:"
52                           + command.toUpperCase());
53               }
54           }
55       }
56       //Infrastructure for simple painting
57       public Interpreter2() {
58           interpreter();
59       }
60       public static void main(String[] args) {
61           SwingUtilities.invokeLater(new Runnable() {
62               public void run() {
63                   new Interpreter2();
64               }
65           });
66       }
67   }