Interpreter3.java
1    /* 
2     *This interpreter will paint different color dots 
3     * Commands the interpreter can recognize 
4     * -Green: paint the green dot 
5     * - Yellow: paint the yellow dot 
6     * - BLUE: paint the blue dot 
7     * - Red: paint the red dot 
8     * - HELP: show alist of the commands in a dialog message box 
9     * - EXIT: terminate the program 
10    * Adding a command in this program "random" we will see how the hell this works out?? 
11    */
12   package interpreters;
13   
14   import painter.SPainter;
15   import shapes.SCircle;
16   
17   import javax.swing.*;
18   import java.awt.*;
19   
20   public class Interpreter3 {
21       private void interpreter() {
22           //CREATE OBJECTS TO THINK WITH
23   
24           SPainter miro = new SPainter("Dot Thing", 400, 400);
25           miro.setScreenLocation(0, 0);
26           SCircle dot = new SCircle(180);
27           //REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT
28   
29           while (true) {
30               String command = JOptionPane.showInputDialog(null, "Command?");
31               if (command == null) { command = "exit"; }//user clicked on Cancel
32               
33               if (command.equalsIgnoreCase("blue")) {
34                   miro.setColor(Color.BLUE);
35                   miro.paint(dot);
36                   //Random command I added
37               } else if ( command.equalsIgnoreCase("random")) {
38                   miro.setColor(randomColor());
39                   miro.paint(dot);
40               } else if (command.equalsIgnoreCase("red")) {
41                   miro.setColor(Color.RED);
42                   miro.paint(dot);
43              
44               }else if (command.equalsIgnoreCase("green")) {
45                   miro.setColor(Color.GREEN);
46                   miro.paint(dot);
47               
48               }else if (command.equalsIgnoreCase("yellow")) {
49                   miro.setColor(Color.YELLOW);
50                   miro.paint(dot);
51               
52               } else if (command.equalsIgnoreCase("help")) {
53                   JOptionPane.showMessageDialog(null, "Valid commands are: "
54                           + "RED | BLUE | YELLOW | GREEN | RANDOM | HELP | EXIT ");
55               
56               } else if (command.equalsIgnoreCase("exit")) {
57                   miro.end();
58                   System.out.println("Thank you for viewing the dots ...");
59                   break;
60               } else {
61                   JOptionPane.showMessageDialog(null, "Unrecognizable command: "
62                           + command.toUpperCase());
63               }
64           }
65       }
66   
67       private static Color randomColor() {
68           int rv = (int) (Math.random()*256);
69           int gv = (int) (Math.random()*256);
70           int bv = (int) (Math.random()*256);
71           return new Color(rv,gv,bv);
72       }
73   
74       //Infrastructure for some simple painting
75       public Interpreter3() {
76           interpreter();
77       }
78       public static void main(String[] args) {
79           SwingUtilities.invokeLater(new Runnable() {
80               @Override
81               public void run() {
82                   new Interpreter3();
83               }
84           });
85       }
86   }
87