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 box 
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 micro = new SPainter("Dot Thing", 400, 400);
27           micro.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 ) { command = "exit" ;}     //user clicked on cancel
34               if (command.equalsIgnoreCase("blue")) {
35                   micro.setColor(Color.BLUE);
36                   micro.paint(dot);
37               }
38               else if (command.equalsIgnoreCase("red")) {
39                   micro.setColor(Color.RED);
40                   micro.paint(dot);
41               }
42               else if (command.equalsIgnoreCase("green")) {
43                   micro.setColor(Color.GREEN);
44                   micro.paint(dot);
45               }
46               else if (command.equalsIgnoreCase("yellow")) {
47                   micro.setColor(Color.YELLOW);
48                   micro.paint(dot);
49               }
50               else if (command.equalsIgnoreCase("help")) {
51                   JOptionPane.showMessageDialog(null, "Valid commands are: "
52                           + "RED | BLUE | GREEN | YELLOW | HELP | EXIT ");
53               }
54               else if (command.equalsIgnoreCase("exit")) {
55                   micro.end();
56                   System.out.println("Thank you for viewing the dots...");
57                   break;
58               }
59               else {
60                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " +
61                           command.toUpperCase());
62               }
63   
64           }
65       }
66   
67       //INFRASTRUCTURE FOR SOME SIMPLE PAINTING
68   
69       public Interpreter2() {
70           interpreter();
71       }
72       public static void main(String [] args) {
73           SwingUtilities.invokeLater(new Runnable() {
74               @Override
75               public void run() {
76                   new Interpreter2();
77               }
78           });
79       }
80   }
81