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