Interpreter3.java
1    /* 
2      interpreter to paint different dots in a window. 
3     commands the program can recognize: 
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     - RANDOM: paint a dot with a random color 
9     - HELP: show a list of commands in a dialog message box 
10    - EXIT: terminate the program 
11    */
12   
13   package interpreters;
14   
15   import java.awt.Color;
16   import javax.swing.JOptionPane;
17   import javax.swing.SwingUtilities;
18   import painter.SPainter;
19   import shapes.SCircle;
20   
21   public class Interpreter3 {
22       private void interpreter() {
23   
24           // objects to think with
25           SPainter miro = new SPainter("Dot Thing",400,400);
26           miro.setScreenLocation (0,0);
27           SCircle dot = new SCircle(180);
28   
29           // repeatedly take a command from an input dialog box and interpret it
30           while (true) {
31               String command = JOptionPane.showInputDialog(null, "Command?");
32               if (command == null) {command = "exit";} //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                           + "BLUE | RED | 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   
62       private static Color randomColor() {
63           int rv = (int)(Math.random()*256);
64           int gv = (int)(Math.random()*256);
65           int bv = (int)(Math.random()*256);
66           return new Color(rv,gv,bv);
67       }
68   
69   // infrastructure for painting
70   
71       public Interpreter3() {
72           interpreter();
73       }
74   
75       public static void main(String[] args) {
76           SwingUtilities.invokeLater(new Runnable() {
77               public void run() {
78                   new Interpreter3();
79               }
80           });
81       }
82   }