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