Interpreter3.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   
11   package interpreters;
12   
13   import java.awt.Color;
14   import javax.swing.JOptionPane;
15   import javax.swing.SwingUtilities;
16   import painter.SPainter;
17   import shapes.SCircle;
18   
19   public class Interpreter3{
20       private void interpreter() {
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   
26   
27           // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT
28           while (true) {
29               String command = JOptionPane.showInputDialog(null, "Command?");
30               if (command == null) {
31                   command = "exit";
32               } // user clicked on Cancel
33               if (command.equalsIgnoreCase("blue")) {
34                   miro.setColor(Color.BLUE);
35                   miro.paint(dot);
36               } else if (command.equalsIgnoreCase("red")) {
37                   miro.setColor(Color.RED);
38                   miro.paint(dot);
39               } else if (command.equalsIgnoreCase("green")) {
40                   miro.setColor(Color.GREEN);
41                   miro.paint(dot);
42               }else if (command.equalsIgnoreCase("yellow")){
43                   miro.setColor(Color.YELLOW);
44                   miro.paint(dot);
45               } else if (command.equalsIgnoreCase("random")){
46                   miro.setColor(randomColor());
47                   miro.paint(dot);
48               } else if (command.equalsIgnoreCase("help")) {
49                   JOptionPane.showMessageDialog(null, "Valid commands are: "
50                           + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT ");
51               } else if (command.equalsIgnoreCase("exit")) {
52                   miro.end();
53                   System.out.println("Thank you for viewing the dots ...");
54                   break;
55               } else {
56                   JOptionPane.showMessageDialog(null, "Unrecognizable command: "
57                           + command.toUpperCase());
58               }
59           }
60       }
61       private Color randomColor() {
62           int rv = (int) (Math.random() * 256);
63           int gv = (int) (Math.random() * 256);
64           int bv = (int) (Math.random() * 256);
65           return new Color(rv, gv, bv);
66       }
67       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
68   
69       public Interpreter3() {
70           interpreter();
71       }
72   
73       public static void main(String[] args) {
74           SwingUtilities.invokeLater(new Runnable() {
75               public void run() {
76                   new Interpreter3();
77               }
78           });
79       }
80   }
81