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