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 are: 5 * 6 * -BLUE: paint a blue dot 7 * -RED: paint a red dot 8 * -GREEN: paint a green dot 9 * -YELLOW: paint a yellow dot 10 * -HELP: show a list of the commands in a dialog message box 11 * -EXIT: terminate the program 12 */ 13 14 package interpreters; 15 16 import java.awt.Color; 17 import javax.swing.*; 18 19 import painter.SPainter; 20 import shapes.SCircle; 21 22 public class Interpreter2 { 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 box and interpret it 32 while(true) { 33 String command= JOptionPane.showInputDialog(null, "Command?"); 34 if (command==null) {command="exit";} // user clicked on Cancel 35 if (command.equalsIgnoreCase("blue")) { 36 miro.setColor(Color.blue); 37 miro.paint(dot); 38 } else if (command.equalsIgnoreCase("red")) { 39 miro.setColor(Color.red); 40 miro.paint(dot); 41 } else if (command.equalsIgnoreCase("green")) { 42 miro.setColor(Color.green); 43 miro.paint(dot); 44 } else if (command.equalsIgnoreCase("yellow")) { 45 miro.setColor(Color.yellow); 46 miro.paint(dot); 47 } else if (command.equalsIgnoreCase("help")) { 48 JOptionPane.showMessageDialog(null, "Valid commands are:" + "RED | BLUE | GREEN | YELLOW| HELP | EXIT |"); 49 } else if (command.equalsIgnoreCase("exit")) { 50 miro.end(); 51 System.out.println("Thank you for viewing the dots ..."); 52 break; 53 } else { 54 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); 55 56 } 57 } 58 } 59 60 // Infrastructure for some simple painting 61 62 public Interpreter2() { 63 interpreter(); 64 } 65 public static void main (String[] args) { 66 SwingUtilities.invokeLater(new Runnable() { 67 @Override 68 public void run() { 69 new Interpreter2(); 70 } 71 }); 72 } 73 } 74 75