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