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