interpreter1.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     * - HELP: show a list of the commands in a dialog message box 
8     * - EXIT: terminate the program 
9     */
10   package interpreters;
11   import java.awt.Color;
12   import javax.swing.JOptionPane;
13   import javax.swing.SwingUtilities;
14   import painter.SPainter;
15   import shapes.SCircle;
16   public class interpreter1 {
17       private void interpreter() {
18   // CREATE OBJECTS TO THINK WITH
19           SPainter miro = new SPainter("Dot Thing",400,400);
20           miro.setScreenLocation(0,0);
21           SCircle dot = new SCircle(180);
22   // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT
23           while ( true ) {
24               String command = JOptionPane.showInputDialog(null,"Command?");
25               if ( command == null ) { command = "exit"; } // user clicked on Cancel
26               if ( command.equalsIgnoreCase("blue") ) {
27                   miro.setColor(Color.BLUE);
28                   miro.paint(dot);
29               } else if ( command.equalsIgnoreCase("red") ) {
30                   miro.setColor(Color.RED);
31                   miro.paint(dot);
32               } else if ( command.equalsIgnoreCase("help") ) {
33                   JOptionPane.showMessageDialog(null,"Valid commands are: "
34                           + "RED | BLUE | HELP | EXIT ");
35               } else if ( command.equalsIgnoreCase("exit") ) {
36                   miro.end();
37                   System.out.println("Thank you for viewing the dots ...");
38                   break;
39               } else {
40                   JOptionPane.showMessageDialog(null, "Unrecognizable command: ");
41   
42   
43               }
44           }
45       }
46       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
47       public interpreter1() {
48           interpreter();
49       }
50       public static void main(String[] args) {
51           SwingUtilities.invokeLater(new Runnable() {
52               public void run() {
53                   new interpreter1();
54               }
55           });
56       }
57   }