Interpreter3.java
1    /* 
2     *This intepreter is intended to pain different colored dots in a window. 
3     * 
4     * The commands that the interpreter can recognize and respond to are: 
5     *  - BLUE: paint a blue dot 
6     *  - RED: paint a red dot 
7     *  - GREEN: paint a green dot 
8     *  - YELLOW: paint a yellow dot 
9     *  - HELP: show a list of the commands in a dialogue message box 
10    *  - EXIT: terminate the program 
11    *  - RANDOM: 
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   
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("radnom")) {
44                   miro.setColor(randomColor());
45                   miro.paint(dot);
46               } else if (command.equalsIgnoreCase("help")){
47                   JOptionPane.showMessageDialog(null, "Valid commands are:"
48                           + "RED | GREEN | BLUE | 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   
61       private static 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   
69       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
70   
71       public Interpreter3() {
72           interpreter();
73       }
74   
75       public static void main(String[] args) {
76           SwingUtilities.invokeLater(new Runnable(){
77               public void run() {
78                   new Interpreter3();
79               }
80           });
81       }
82   }
83