Interpreter3.java
1    /* 
2    An extension of the interpreter2 program with random function 
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 box 
11   -EXIT:terminate the program 
12    
13    */
14   
15   package interpreters;
16   
17   import painter.SPainter;
18   import shapes.SCircle;
19   import javax.swing.*;
20   import java.awt.*;
21   
22   public class Interpreter3 {
23       private void interpreter(){
24           //Create objects to think with
25           SPainter miro = new SPainter("Dot Thing",400,400);
26           miro.setScreenLocation(0,0);
27           SCircle dot = new SCircle(180);
28   
29           //Repeatedly take a command from an input dialog box and interpret it
30           while (true){
31               String command = JOptionPane.showInputDialog(null,"Command?");
32               if (command == null) { command = "exit";} //user clicked on cancel
33   
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("green")) {
43                   miro.setColor(Color.GREEN);
44                   miro.paint(dot);
45               }
46               else if (command.equalsIgnoreCase("yellow")) {
47                   miro.setColor(Color.YELLOW);
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,"Valid commands are: " + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT");
56               }
57               else if (command.equalsIgnoreCase("exit")){
58                   miro.end();
59                   System.out.println("Thank you for viewing the dots....;)");
60                   break;
61               }
62               else {
63                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase());
64                   System.out.println("Type 'HELP' for list of commands");
65               }
66           }
67       }
68   
69       private 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       //Infrastructure for some simple painting
77   
78       public Interpreter3() {
79           interpreter();
80       }
81   
82       public static void main(String[] args){
83           SwingUtilities.invokeLater(new Runnable(){
84               public void run() {
85                   new Interpreter3();
86               }
87           });
88       }
89   
90   }
91