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