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