1 2 package interpreters; 3 import java.awt.Color; 4 import javax.swing.JOptionPane; 5 import javax.swing.SwingUtilities; 6 import painter.SPainter; 7 import shapes.SCircle; 8 9 10 public class Interpreter2 { 11 12 private void interpreter() { 13 // CREATE OBJECTS TO THINK WITHS 14 SPainter miro = new SPainter("Dot Thing",400,400); 15 miro.setScreenLocation(0,0); 16 SCircle dot = new SCircle(180); 17 // REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT 18 while ( true ) { 19 String command = JOptionPane.showInputDialog(null,"Command?"); 20 if ( command == null ) { command = "exit"; } 21 // user clicked on Cancel 22 if ( command.equalsIgnoreCase("blue") ) { 23 miro.setColor(Color.BLUE); 24 miro.paint(dot); 25 } else if ( command.equalsIgnoreCase("red") ) { 26 miro.setColor(Color.RED); 27 miro.paint(dot); 28 } else if ( command.equalsIgnoreCase("green") ){ 29 miro.setColor(Color.GREEN); 30 miro.paint(dot); 31 } else if ( command.equalsIgnoreCase("yellow") ) { 32 miro.setColor(Color.YELLOW); 33 miro.paint(dot); 34 } else if ( command.equalsIgnoreCase("help") ) { 35 JOptionPane.showMessageDialog(null,"Valid commands are: " + 36 ""+ "RED | BLUE | GREEN | YELLOW | HELP | EXIT "); 37 } 38 else if ( command.equalsIgnoreCase("exit") ) {miro.end(); 39 40 System.out.println("Thank you for viewing the dots ..."); 41 break; 42 } else { 43 JOptionPane.showMessageDialog(null, "Unrecognizable command: " 44 + command.toUpperCase()); 45 } 46 } 47 } 48 49 public Interpreter2() { 50 interpreter(); 51 } 52 53 public static void main(String[] args) { 54 SwingUtilities.invokeLater(new Runnable() { 55 56 public void run() { 57 new Interpreter2(); 58 } 59 }); 60 } 61 } 62 // INFRASTRUCTURE FOR SOME SIMPLE PAINTINGpublic Interpreter1() {interpreter();}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() 63 // {public void run() {new Interpreter1();}});}}