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: Paint a random colored dot 
10    *  - HELP: show a list of the commands in a dialog message 
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 miro = new SPainter("Dot Thing", 400, 400);
28           miro.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) {
35                   command = "exit";
36               } //user clicked on cancel
37               if (command.equalsIgnoreCase("blue"))
38               {
39                   miro.setColor(Color.BLUE);
40                   miro.paint(dot);
41               } else if(command.equalsIgnoreCase("red"))
42               {
43                   miro.setColor(Color.RED);
44                   miro.paint(dot);
45               } else if(command.equalsIgnoreCase("green")) {
46                   miro.setColor(Color.GREEN);
47                   miro.paint(dot);
48               }else if(command.equalsIgnoreCase("yellow")) {
49                   miro.setColor(Color.YELLOW);
50                   miro.paint(dot);
51               }else if(command.equalsIgnoreCase("random")) {
52                   miro.setColor(randomColor());
53                   miro.paint(dot);
54               } else if(command.equalsIgnoreCase("help"))
55               {
56                   JOptionPane.showMessageDialog(null, "Valid commands are: "
57                           + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT ");
58               }else if(command.equalsIgnoreCase("exit"))
59               {
60                   miro.end();
61                   System.out.println("Thank you for viewing the dots ...");
62                   break;
63               } else {
64                   JOptionPane.showMessageDialog(null, "Unrecognizable command: "
65                           + command.toUpperCase());
66               }
67           }
68       }
69   
70       private Color randomColor() {
71           int rv = (int)(Math.random()*256);
72           int gv = (int)(Math.random()*256);
73           int bv = (int)(Math.random()*256);
74           return new Color(rv,gv,bv);
75       }
76   
77       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
78   
79       public Interpreter3() {
80           interpreter();
81       }
82   
83       public static void main(String[] args) {
84           SwingUtilities.invokeLater(new Runnable() {
85               public void run() {
86                   new Interpreter3();
87               }
88           });
89       }
90   }