Interpreter3.java
1    /* 
2     * This interpreter is intended to paint different colored dots in a window. 
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     *  - YELLOW: paint a yellow dot 
8     *  - GREEN: paint a green dot 
9     *  - RANDOM: paint a dot a random color 
10    *  - HELP: show a list of the commands in a dialog message box 
11    *  - EXIT: terminate the program 
12    * 
13    */
14   package interpreters;
15   import java.awt.Color;
16   import javax.swing.JOptionPane;
17   import javax.swing.SwingUtilities;
18   import painter.SPainter;
19   import shapes.SCircle;
20   
21   public class Interpreter3
22   {
23       private void interpreter()
24       {
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           // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT
30           while ( true )
31           {
32               String command = JOptionPane.showInputDialog(null,"Command?");
33               if( command == null )
34               {
35                   command = "exit";
36               }
37               // user clicked on Cancel
38               if ( command.equalsIgnoreCase("blue") )
39               {
40                   miro.setColor(Color.BLUE);miro.paint(dot);
41               }
42               else if ( command.equalsIgnoreCase("red") )
43               {
44                   miro.setColor(Color.RED);miro.paint(dot);
45               }
46               else if ( command.equalsIgnoreCase("yellow") )
47               {
48                   miro.setColor(Color.YELLOW);miro.paint(dot);
49               }
50               else if ( command.equalsIgnoreCase("green") )
51               {
52                   miro.setColor(Color.GREEN);miro.paint(dot);
53               }
54               else if (command.equalsIgnoreCase("random") )
55               {
56                   miro.setColor(randomColor());miro.paint(dot);
57               }
58               else if ( command.equalsIgnoreCase("help") )
59               {
60                   JOptionPane.showMessageDialog(null,"Valid commands are: "+ "RED | BLUE | YELLOW | GREEN | HELP | EXIT ");
61               }
62               else if ( command.equalsIgnoreCase("exit") )
63               {
64                   miro.end();System.out.println("Thank you for viewing the dots ...");break;
65               }
66               else
67               {
68                   JOptionPane.showMessageDialog(null,"Unrecognizable command: " + command.toUpperCase());
69               }
70           }
71       }
72   
73       private static Color randomColor()
74       {
75           int rv = (int)(Math.random()*256);
76           int gv = (int)(Math.random()*256);
77           int bv = (int)(Math.random()*256);
78           return new Color(rv,gv,bv);
79       }
80   
81       // INFRASTRUCTURE FOR SOME SIMPLE PAINTING
82       public Interpreter3()
83       {
84           interpreter();}public static void main(String[] args)
85   {
86       SwingUtilities.invokeLater(new Runnable() {public void run() {new Interpreter3();
87       }
88       });
89   }
90   }