Interpreter3.java
1    /* 
2     * This interpreter is intended to paint different colored dots in a window. 
3     * The commands that the interpreter can recognize and respond to are: 
4     *  - Blue: paint a blue dot 
5     *  - Red: paint a red dot 
6     *  - Yellow: paint a yellow dot 
7     *  - Green: paint a green dot 
8     *  - Help: show a list of the commands in a dialog message box 
9     *  - Exit: terminate the program 
10   */
11   package interpreters;
12   
13   import painter.SPainter;
14   import shapes.SCircle;
15   
16   import javax.swing.*;
17   import java.awt.*;
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           //Repeatedly take a command from a input dialog vox and interpret it
26           while ( true ) {
27               String command = JOptionPane.showInputDialog(null, "Command?");
28               if (command == null) { command = "exit"; } // user clicked cancel
29               if (command.equalsIgnoreCase("blue")) {
30                   miro.setColor(Color.blue);
31                   miro.paint(dot);
32               } else if (command.equalsIgnoreCase("red")) {
33                   miro.setColor(Color.red);
34                   miro.paint(dot);
35               } else if (command.equalsIgnoreCase("yellow")) {
36                   miro.setColor(Color.yellow);
37                   miro.paint(dot);
38               } else if (command.equalsIgnoreCase("green")) {
39                   miro.setColor(Color.green);
40                   miro.paint(dot);
41               } else if (command.equalsIgnoreCase("random")) {
42                   miro.setColor(randomColor());
43                   miro.paint(dot);
44               }else if (command.equalsIgnoreCase("help")) {
45                   JOptionPane.showMessageDialog(null, "Valid commands are: " + "Red | Blue | Yellow | Green | Random | Help | Exit ");
46               }else if (command.equalsIgnoreCase("exit")) {
47                   miro.end();
48                   System.out.println("Thank you for viewing the dots ...");
49                   break;
50               } else {
51                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase());
52               }
53           }
54       }
55   
56       private Color randomColor() {
57           int rv = (int)(Math.random()*256);
58           int gv = (int)(Math.random()*256);
59           int bv = (int)(Math.random()*256);
60           return new Color(rv,gv,bv);
61       }
62   
63       //Infrastructure for some simple painting
64   
65       public Interpreter3(){
66           interpreter();
67       }
68   
69       public static void main(String[] args) {
70           SwingUtilities.invokeLater(new Runnable() {
71               public void run() {
72                   new Interpreter3();
73               }
74           });
75       }
76   }
77