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