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