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