interpreter3.java
1    /* 
2     * This interpreter is intended to paint 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     * - RANDOM: paint random colored dot 
10    * - HELP: show a list of the commands in a dialog message box 
11    * - EXIT: terminate the program 
12    */
13   package interpreters;
14   import java.awt.Color;
15   import javax.swing.JOptionPane;
16   import javax.swing.SwingUtilities;
17   import painter.SPainter;
18   import shapes.SCircle;
19   public class interpreter3 {
20       private void interpreter() {
21   // CREATE OBJECTS TO THINK WITH
22           SPainter miro = new SPainter("Dot Thing",400,400);
23           miro.setScreenLocation(0,0);
24           SCircle dot = new SCircle(180);
25   // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT
26           while ( true ) {
27               String command = JOptionPane.showInputDialog(null,"Command?");
28               if ( command == null ) { command = "exit"; } // user clicked on Cancel
29               if ( command.equalsIgnoreCase("blue") ) {
30                   miro.setColor(Color.BLUE);
31                   miro.paint(dot);
32   
33               } else if ( command.equalsIgnoreCase("red") ) {
34                   miro.setColor(Color.RED);
35                   miro.paint(dot);
36   
37               } else if ( command.equalsIgnoreCase("yellow") ) {
38                   miro.setColor(Color.YELLOW);
39                   miro.paint(dot);
40   
41               } else if ( command.equalsIgnoreCase("green") ) {
42                   miro.setColor(Color.GREEN);
43                   miro.paint(dot);
44   
45   
46               } else if ( command.equalsIgnoreCase("random") ) {
47                   miro.setColor(randomColor());
48                   miro.paint(dot);
49   
50               } else if ( command.equalsIgnoreCase("help") ) {
51                   JOptionPane.showMessageDialog(null,"Valid commands are: "
52                           + "RED | GREEN | BLUE | YELLOW | 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   
60   
61               }
62           }
63       }
64   
65       private 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       public interpreter3() {
74           interpreter();
75       }
76       public static void main(String[] args) {
77           SwingUtilities.invokeLater(new Runnable() {
78               public void run() {
79                   new interpreter3();
80               }
81           });
82       }
83   }