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