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 * - HELP: show a list of the commands in a dialog message box 8 * - EXIT: terminate the program 9 */ 10 package interpreters; 11 12 import java.awt.Color; 13 import javax.swing.JOptionPane; 14 import javax.swing.SwingUtilities; 15 import painter.SPainter; 16 import shapes.SCircle; 17 18 public class Interpreter1 { 19 private void interpreter() { 20 // Create objects to "think" with. 21 SPainter gage = new SPainter("Dot thing", 400, 400); 22 gage.setScreenLocation(0, 0); 23 SCircle dot = new SCircle(180); 24 25 // Repeatedly takes a command from an input dialog box and interpret it. 26 while (true) { 27 String command = JOptionPane.showInputDialog(null, "Command?"); 28 if (command == null) {command = "exit";} // User clicks "Cancel" prompt 29 if (command.equalsIgnoreCase("blue")) { 30 gage.setColor(Color.BLUE); 31 gage.paint(dot); 32 } else if (command.equalsIgnoreCase("red")) { 33 gage.setColor(Color.RED); 34 gage.paint(dot); 35 } else if (command.equalsIgnoreCase("help")) { 36 JOptionPane.showMessageDialog(null,"Valid commands are: " 37 + "RED | BLUE | HELP | EXIT"); 38 } else if (command.equalsIgnoreCase("exit")) { 39 gage.end(); 40 System.out.println("Thank you for viewing the dots program."); 41 break; 42 } else { 43 JOptionPane.showMessageDialog(null, "Unrecognizable command:" 44 + command.toUpperCase()); 45 } 46 } 47 } 48 //Infrastructure for simple painting 49 public Interpreter1() { 50 interpreter(); 51 } 52 public static void main(String[] args) { 53 SwingUtilities.invokeLater(new Runnable() { 54 public void run() { 55 new Interpreter1(); 56 } 57 }); 58 } 59 }