Interpreter3.java
1    /* 
2    * Program takes an input from a user (RED | BLUE | GREEN | YELLOW | RANDOM |  HELP | EXIT) and draws/exits based on user input. 
3    * Created by Hunter Gersitz on 26 SEP 2019 ~ SUNY Oswego Fall 2019 
4    */
5    package interpreters;
6    
7    import painter.SPainter;
8    import shapes.SCircle;
9    
10   import javax.swing.*;
11   import java.awt.*;
12   
13   public class Interpreter3
14   {
15   
16       private static Color randomColor()
17       {
18           int rv = (int) (Math.random()*256);
19           int gv = (int) (Math.random()*256);
20           int bv = (int) (Math.random()*256);
21           return new Color(rv, gv, bv);
22       }
23   
24       private void interpreter()
25       {
26           //  CREATE OBJECTS TO THINK WITH
27           SPainter miro = new SPainter("Dot Thing", 400, 400);
28           miro.setScreenLocation(0,0);
29           SCircle dot = new SCircle(180);
30   
31           //  REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG AND INTERPRET IT
32           while ( true )
33           {
34               String command = JOptionPane.showInputDialog(null, "Command?");
35               if ( command == null ) { command = "exit"; } // user clicked on Cancel
36               if ( command.equalsIgnoreCase("blue") )
37               {
38                   miro.setColor(Color.BLUE);
39                   miro.paint(dot);
40               }
41               else if ( command.equalsIgnoreCase("red") )
42               {
43                   miro.setColor(Color.RED);
44                   miro.paint(dot);
45               }
46               else if ( command.equalsIgnoreCase("green") )
47               {
48                   miro.setColor(Color.GREEN);
49                   miro.paint(dot);
50               }
51               else if ( command.equalsIgnoreCase("yellow") )
52               {
53                   miro.setColor(Color.YELLOW);
54                   miro.paint(dot);
55               }
56               // Random color
57               else if ( command.equalsIgnoreCase("random") )
58               {
59                   miro.setColor(randomColor());
60                   miro.paint(dot);
61               }
62               
63               else if ( command.equalsIgnoreCase("help") )
64               {
65                   JOptionPane.showMessageDialog(null, "Valid command are: " + "RED | BLUE | GREEN | YELLOW | RANDOM |  HELP | EXIT");
66               }
67               else if ( command.equalsIgnoreCase("exit") )
68               {
69                   miro.end();
70                   System.out.println("Thank you for viewing the dots ...");
71                   break;
72               }
73               else {
74                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase());
75               }
76           }
77       }
78   
79       //  INFRASTRUCTURE FOR SOME SIMPLE PAINTING
80   
81   
82       public Interpreter3()
83       {
84           interpreter();
85       }
86   
87       public static void main(String[] args)
88       {
89           SwingUtilities.invokeLater(new Runnable()
90           {
91               public void run()
92               {
93                   new Interpreter3();
94               }
95           });
96       }
97   }
98