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