1 package interpreters; 2 3 import java.awt.Color; 4 import javax.swing.SwingUtilities; 5 import painter.SPainter; 6 import shapes.SCircle; 7 import javax.swing.JOptionPane; 8 9 public class Interpreter3 { 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 pox 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 38 else if ( command.equalsIgnoreCase("help")) { 39 JOptionPane.showMessageDialog(null, "Valid commands are:" + 40 " RED / BLUE / GREEN / YELLOW / RANDOM / HELP / EXIT"); 41 } else if (command.equalsIgnoreCase("exit")) { 42 miro.end(); 43 System.out.println("Thank you for viewing the dots ..."); 44 break; 45 } else { 46 JOptionPane.showMessageDialog(null, "Unrecognizable command:" + 47 command.toUpperCase()); 48 49 } 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 //Infrastructure for some simple painting 62 63 public Interpreter3() { 64 interpreter(); 65 } 66 67 public static void main(String[] args) { 68 SwingUtilities.invokeLater(new Runnable() { 69 @Override 70 public void run() { 71 new Interpreter3(); 72 } 73 }); 74 } 75 76 77 }