1 /* 2 An extension of the interpreter1 program with more dots 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 -GREEN:paint a green dot 8 -YELLOW:paint a yellow dot 9 -HELP:show a list of the commands in a dialog message box 10 -EXIT:terminate the program 11 12 */ 13 14 package interpreters; 15 16 import painter.SPainter; 17 import shapes.SCircle; 18 import javax.swing.*; 19 import java.awt.*; 20 21 public class Interpreter2 { 22 private void interpreter(){ 23 //Create objects to think with 24 SPainter miro = new SPainter("Dot Thing",400,400); 25 miro.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 33 if (command.equalsIgnoreCase("blue")){ 34 miro.setColor(Color.BLUE); 35 miro.paint(dot); 36 } 37 else if (command.equalsIgnoreCase("red")){ 38 miro.setColor(Color.RED); 39 miro.paint(dot); 40 } 41 else if (command.equalsIgnoreCase("green")) { 42 miro.setColor(Color.GREEN); 43 miro.paint(dot); 44 } 45 else if (command.equalsIgnoreCase("yellow")) { 46 miro.setColor(Color.YELLOW); 47 miro.paint(dot); 48 } 49 else if (command.equalsIgnoreCase("help")){ 50 JOptionPane.showMessageDialog(null,"Valid commands are: " + "RED | BLUE | GREEN | YELLOW | HELP | EXIT"); 51 } 52 else if (command.equalsIgnoreCase("exit")){ 53 miro.end(); 54 System.out.println("Thank you for viewing the dots....;)"); 55 break; 56 } 57 else { 58 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); 59 System.out.println("Type 'HELP' for list of commands"); 60 } 61 } 62 } 63 64 //Infrastructure for some simple painting 65 66 public Interpreter2() { 67 interpreter(); 68 } 69 70 public static void main(String[] args){ 71 SwingUtilities.invokeLater(new Runnable(){ 72 public void run() { 73 new Interpreter2(); 74 } 75 }); 76 } 77 78 } 79