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