Interpreter3.java
1    /* 
2    this interpreter is intended to paint different colored dots in a window 
3     
4    commands 
5        BLUE: paint a blue dot 
6        RED:Paint a red dot 
7        Yellow 
8        Green 
9        Random 
10       HELP: show a list of commands 
11       EXIT: terminate program 
12    */
13   
14   package interpreters;
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           // Create objects to think with
24           SPainter miro = new SPainter("Dot Thing",400,400);
25           miro.setScreenLocation(0,0);
26           SCircle dot = new SCircle(180);
27   
28           //repetedly take a command form an input box and interpret it
29           while (true) {
30               String command = JOptionPane.showInputDialog(null,"Command");
31               if ( command == null) { command = "exit"; } //user clicked on cancel
32               if ( command.equalsIgnoreCase("blue")){
33                   miro.setColor(Color.BLUE);
34                   miro.paint(dot);
35               } else if ( command.equalsIgnoreCase("red")){
36                   miro.setColor(Color.red);
37                   miro.paint(dot);
38               } else if ( command.equalsIgnoreCase("Yellow")){
39                   miro.setColor(Color.yellow);
40                   miro.paint(dot);
41               }else if ( command.equalsIgnoreCase("Green")){
42                   miro.setColor(Color.green);
43                   miro.paint(dot);
44               }else if ( command.equalsIgnoreCase("Random")){
45                   miro.setColor(randomColor());
46                   miro.paint(dot);
47               } else if (command.equalsIgnoreCase("help")) {
48                   JOptionPane.showMessageDialog(null,"valid commands are:"
49                           + "RED | BLUE | HELP | EXIT | YELLOW | GREEN | RANDOM ");
50               } else if (command.equalsIgnoreCase("exit")) {
51                   miro.end();
52                   System.out.println("Thank you for viewing the dots");
53                   break;
54               } else {
55                   JOptionPane.showMessageDialog(null, "Unrecognizable command: "
56                           + command.toUpperCase());
57               }
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   
68       // infrastructure
69       public Interpreter3(){
70           interpreter();
71       }
72       public static void main(String[] args){
73           SwingUtilities.invokeLater(new Runnable(){
74               public void run(){
75                   new Interpreter3();
76               }
77           });
78       }
79   }