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