Interpreter3.java
1    package interpreters;
2    
3    /* 
4    Blue - paint a dot 
5    Red - paint a red dot 
6    Yellow - Paint a yellow dot 
7    Green - paint a green dot 
8    help - show a list of commands in a dialog message box 
9    Exit - Terminate the program 
10    */
11   import java.awt.Color;
12   import javax.swing.SwingUtilities;
13   import javax.swing.JOptionPane;
14   import painter.SPainter;
15   import shapes.SCircle;
16   
17   
18   public class Interpreter3 {
19       private void interpreter() {
20           //Create Obkjects to think with
21           SPainter miro = new SPainter("Dot thing", 400, 400);
22           miro.setScreenLocation(0,0);
23           SCircle dot = new SCircle(180);
24   
25           //Repeatedly take a command from an input dialog and Interpret it
26           while (true) {
27               String command = JOptionPane.showInputDialog(null, "Command");
28               if ( command == null) {command = "exit";} //user clicked on cancel
29               if (command.equalsIgnoreCase("blue"))
30               {
31                   miro.setColor(Color.blue);
32                   miro.paint(dot);
33               } else if (command.equalsIgnoreCase("red")) {
34                   miro.setColor(Color.red);
35                   miro.paint(dot);
36               } else if (command.equalsIgnoreCase("green")) { //added after Int 1
37                   miro.setColor(Color.green);
38                   miro.paint(dot);
39               } else if (command.equalsIgnoreCase("yellow")) { //added after Int 1
40                   miro.setColor(Color.yellow);
41                   miro.paint(dot);
42               }else if (command.equalsIgnoreCase("random")) { //added after Int 1
43                   miro.setColor(randomColor());
44                   miro.paint(dot);
45               } else if (command.equalsIgnoreCase("help")) {
46                   JOptionPane.showMessageDialog(null,"Valid Commands are: " + "RED | BLUE | YELLOW |" +
47                           " GREEN | RANDOM | HELP| EXIT");
48               } else if (command.equalsIgnoreCase("exit")) {
49                   miro.end();
50                   System.out.println("Thank you for viewing the dots");
51   
52                   break;
53               } else {
54                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase());
55               }
56   
57           }
58       }
59   
60       private Color randomColor() {
61           int rv = (int)(Math.random()*256);
62           int gv = (int)(Math.random()*256);
63           int bv = (int)(Math.random()*256);
64               return new Color(rv,gv,bv);
65       }
66   
67       // Infrastructure fpr Some Simple Painting
68       public Interpreter3 () {
69           interpreter();
70       }
71   
72       public static void main(String[] args) {
73           SwingUtilities.invokeLater(new Runnable() {
74               @Override
75               public void run() {
76                   new Interpreter3();
77               }
78           });
79       }
80   }
81