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