Interpreter3.java
1    /* 
2    This is a program that is going to paint dots of different colors. 
3    The commands that the interpreter recognizes: 
4     BLUE 
5     RED 
6     YELLOW 
7     GREEN 
8     HELP: show list of commands 
9     EXIT: terminate program 
10    RANDOM 
11    */
12   
13   package interpreters;
14   import java.awt.Color;
15   import javax.swing.JOptionPane;
16   import javax.swing.SwingUtilities;
17   import painter.SPainter;
18   import shapes.SCircle;
19   
20   import javax.swing.*;
21   
22   
23   public class Interpreter3 {
24       private void interpreter() {
25           // CREATE OBJECTS TO THINK WITH
26           SPainter miro = new SPainter("Dot Thing", 400,400);
27           miro.setScreenLocation(0,0);
28           SCircle dot = new SCircle(180);
29   
30           // REPEATEDLY TAKE A COMMAND FROM THE 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                   miro.setColor(Color.BLUE);
36                   miro.paint(dot);
37               }
38               else if ( command.equalsIgnoreCase("red") ) {
39                   miro.setColor(Color.RED);
40                   miro.paint(dot);
41               }
42               else if ( command.equalsIgnoreCase("yellow") ) {
43                   miro.setColor(Color.YELLOW);
44                   miro.paint(dot);
45               }
46               else if ( command.equalsIgnoreCase("green") ) {
47                   miro.setColor(Color.GREEN);
48                   miro.paint(dot);
49               }
50               else if ( command.equalsIgnoreCase("random") ) {
51                   miro.setColor(randomColor());
52                   miro.paint(dot);
53               }
54               else if ( command.equalsIgnoreCase("help")) {
55                   JOptionPane.showMessageDialog(null,
56                           "Valid commands are: " + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT" );
57               }
58               else if ( command.equalsIgnoreCase("exit") ) {
59                   miro.end();
60                   System.out.println("Thank you for viewing the dots ...");
61                   break;
62               }
63               else {
64                   JOptionPane.showMessageDialog(null, "Unrecognizable command. ");
65               }
66           }
67       }
68   
69       private static Color randomColor() {
70           int rv = (int)(Math.random()*256);
71           int gv = (int)(Math.random()*256);
72           int bv = (int)(Math.random()*256);
73           return new Color (rv,gv,bv);
74       }
75   
76       // REQUIRED INFRASTRUCTURE FOR PAINTING
77   
78       public Interpreter3() {
79           interpreter(); }
80       public static void main(String[] args) {
81           SwingUtilities.invokeLater(new Runnable() {
82               public void run() {
83                   new Interpreter3();
84               }
85           });
86       }
87   }
88