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