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 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 Interpreter1 { 20 21 private void interpreter() { 22 23 //CREATE OBJECTS TO THINK WITH 24 SPainter micro = new SPainter("Dot Thing", 400, 400); 25 micro.setScreenLocation(0,0); 26 SCircle dot = new SCircle(180); 27 28 //REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT 29 while (true) { 30 String command = JOptionPane.showInputDialog(null, "Command?"); 31 if (command == null ) { command = "exit" ;} //user clicked on cancel 32 if (command.equalsIgnoreCase("blue")) { 33 micro.setColor(Color.BLUE); 34 micro.paint(dot); 35 } 36 else if (command.equalsIgnoreCase("red")) { 37 micro.setColor(Color.RED); 38 micro.paint(dot); 39 } 40 else if (command.equalsIgnoreCase("help")) { 41 JOptionPane.showMessageDialog(null, "Valid commands are: " 42 + "RED | BLUE | HELP | EXIT "); 43 } 44 else if (command.equalsIgnoreCase("exit")) { 45 micro.end(); 46 System.out.println("Thank you for viewing the dots..."); 47 break; 48 } 49 else { 50 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + 51 command.toUpperCase()); 52 } 53 54 } 55 } 56 57 //INFRASTRUCTURE FOR SOME SIMPLE PAINTING 58 59 public Interpreter1() { 60 interpreter(); 61 } 62 public static void main(String [] args) { 63 SwingUtilities.invokeLater(new Runnable() { 64 @Override 65 public void run() { 66 new Interpreter1(); 67 } 68 }); 69 } 70 } 71