1 package interpreters; 2 3 /* 4 This interpreter is intended to paint different colored dots in a window; 5 The commands that the interpreter can recognize and respond to are: 6 7 -blue: paint a blue dot 8 -red : paint a red dot 9 -green: paint a green dot 10 -yellow: paint a yellow dot 11 -HELP: s'how a list of the commands in a dialog message box 12 -EXIT: terminate the program 13 14 */ 15 16 import painter.SPainter; 17 import shapes.SCircle; 18 19 import javax.swing.*; 20 import java.awt.*; 21 22 23 public class Interpreter2 { 24 25 //Createe objects to think with 26 private void interpreter() { 27 28 SPainter miro = new SPainter("Dot Thing", 400, 400); 29 miro.setScreenLocation(0, 0); 30 SCircle dot = new SCircle(180); 31 32 //Repeatedly take a command from an input dialog box and interpret it 33 34 while (true) { 35 String command = JOptionPane.showInputDialog(null, "Command?"); 36 if (command == null) {command = "exit"; } //user clicked on Cancel 37 38 if (command.equalsIgnoreCase("blue")) { 39 miro.setColor(Color.BLUE); 40 miro.paint(dot); 41 } 42 else if (command.equalsIgnoreCase("red")) { 43 miro.setColor(Color.RED); 44 miro.paint(dot); 45 } 46 else if (command.equalsIgnoreCase("green")) { 47 miro.setColor(Color.GREEN); 48 miro.paint(dot); 49 } 50 else if (command.equalsIgnoreCase("yellow")) { 51 miro.setColor(Color.YELLOW); 52 miro.paint(dot); 53 } 54 else if (command.equalsIgnoreCase("help")) { 55 JOptionPane.showMessageDialog(null, "Valid commands are: " + "RED | BLUE | YELLOW | GREEN | HELP | EXIT"); 56 } 57 else if (command.equalsIgnoreCase("exit")) { 58 miro.end(); 59 System.out.println("Thank you for viewing the dots...."); 60 break; 61 } 62 else { 63 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); 64 } 65 } 66 } 67 68 public Interpreter2() { interpreter(); }; 69 70 public static void main(String[] args) { 71 SwingUtilities.invokeLater(new Runnable() { 72 @Override 73 public void run() { 74 new Interpreter2(); 75 } 76 }); 77 } 78 79 } 80