Interpreter3.java
1    /* 
2     *This interpreter is intended to paint different colored dots in a window. 
3     */
4    package interpreters;
5    
6    import java.awt.Color;
7    import javax.swing.JOptionPane;
8    import javax.swing.SwingUtilities;
9    import painter.SPainter;
10   import shapes.SCircle;
11   
12   public class Interpreter3 {
13   
14       private void interpreter() {
15   
16           //Create objects to thikn with
17           SPainter miro = new SPainter("Dot Thing", 400, 400);
18           miro.setScreenLocation(0, 0);
19           SCircle dot = new SCircle(180);
20   
21           //Repeatedly take a command from an input dialog box and interpret it
22   
23           while (true) {
24               String command = JOptionPane.showInputDialog(null, "Command?");
25               if (command == null) {
26                   command = "exit";
27               } // user clicked on Cancel
28               if (command.equalsIgnoreCase("blue")) {
29                   miro.setColor(Color.blue);
30                   miro.paint(dot);
31               } else if (command.equalsIgnoreCase("red")) {
32                   miro.setColor(Color.red);
33                   miro.paint(dot);
34               } else if (command.equalsIgnoreCase("green")) {
35                   miro.setColor(Color.green);
36                   miro.paint(dot);
37               } else if (command.equalsIgnoreCase("yellow")) {
38                   miro.setColor(Color.yellow);
39                   miro.paint(dot);
40               } else if (command.equalsIgnoreCase("random")) {
41                   miro.setColor(randomColor());
42                   miro.paint(dot);
43               } else if (command.equalsIgnoreCase("help")) {
44                   JOptionPane.showMessageDialog(null, "Valid commands are: " + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT ");
45               } else if (command.equalsIgnoreCase("exit")) {
46                   miro.end();
47                   System.out.println("Thank you for viewing the dots ...");
48                   break;
49               } else {
50                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase());
51               }
52   
53   
54           }
55       }
56   
57       private static Color randomColor() {
58           int rv = (int)(Math.random()*256);
59           int gv = (int)(Math.random()*256);
60           int bv = (int)(Math.random()*256);
61           return new Color(rv,gv,bv);
62       }
63   
64   
65       //  INFRASTRUCTURE FOR SOME SIMPLE PAINTING
66   
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   }