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