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