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