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