1 /* 2 * This interpreter is intended to paint different colored dots in a window. 3 * The commands that the interpreter can recognize and respond to are: 4 * - BLUE: paint a blue dot 5 * - RED: paint a red dot 6 * - GREEN: paint a green dot 7 * - YELLOW: paint a yellow dot 8 * - HELP: show a list of the commands in a dialog message box 9 * - EXIT: terminate the program 10 */ 11 12 package interpreters; 13 import java.awt.Color; 14 import javax.swing.JOptionPane; 15 import javax.swing.SwingUtilities; 16 import painter.SPainter; 17 import shapes.SCircle; 18 public class Interpreter2 { 19 private void interpreter() { 20 // CREATE OBJECTS TO THINK WITH 21 SPainter miro = new SPainter("Dot Thing", 400, 400); 22 miro.setScreenLocation(0, 0); 23 SCircle dot = new SCircle(180); 24 // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT 25 while (true) { 26 String command = JOptionPane.showInputDialog(null, "Command?"); 27 if (command == null) { 28 command = "exit"; 29 } // user clicked on Cancel 30 if (command.equalsIgnoreCase("blue")) { 31 miro.setColor(Color.BLUE); 32 miro.paint(dot); 33 } else if (command.equalsIgnoreCase("green")) { 34 miro.setColor(Color.GREEN); 35 miro.paint(dot); 36 } else if (command.equalsIgnoreCase("yellow")) { 37 miro.setColor(Color.YELLOW); 38 miro.paint(dot); 39 } else if (command.equalsIgnoreCase("red")) { 40 miro.setColor(Color.RED); 41 miro.paint(dot); 42 } else if (command.equalsIgnoreCase("help")) { 43 JOptionPane.showMessageDialog(null, "Valid commands are: " 44 + "RED | BLUE | GREEN | YELLOW | HELP | EXIT "); 45 } else if (command.equalsIgnoreCase("exit")) { 46 miro.end(); 47 System.out.println("Thank you for viewing the dots ..."); 48 break; 49 } else { 50 JOptionPane.showMessageDialog(null, "Unrecognizable command: " 51 + command.toUpperCase()); 52 } 53 } 54 } 55 56 // INFRASTRUCTURE FOR SOME SIMPLE PAINTING 57 public Interpreter2() { 58 interpreter(); 59 } 60 61 public static void main(String[] args) { 62 SwingUtilities.invokeLater(new Runnable() { 63 public void run() { 64 new Interpreter2(); 65 } 66 }); 67 } 68 }