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