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