1 package interpreters; 2 3 import java.awt.Color; 4 import javax.swing.JOptionPane; 5 import javax.swing.SwingUtilities; 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 10 public class Interpreter3 { 11 private void interpreter() { 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 // 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) { 20 command = "exit"; 21 } // user click 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("help")) { 29 JOptionPane.showMessageDialog(null, "Valid commands are: " + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT "); 30 } else if (command.equalsIgnoreCase("exit")) { 31 miro.end(); 32 System.out.println("Thank you for viewing the dots ..."); 33 break; 34 } else if (command.equalsIgnoreCase("green")) { 35 miro.setColor(Color.GREEN); 36 miro.paint(dot); 37 } else if (command.equalsIgnoreCase("yellow")) { 38 miro.setColor(Color.YELLOW); 39 miro.paint(dot); 40 } else if ( command.equalsIgnoreCase("random")) { 41 miro.setColor(randomColor()); 42 miro.paint(dot); 43 } 44 else { 45 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); 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 58 // Infrastructure for some simple painting 59 60 public Interpreter3() { 61 interpreter(); 62 } 63 64 public static void main(String[] args) { 65 SwingUtilities.invokeLater(new Runnable() { 66 public void run() { 67 new Interpreter3(); 68 } 69 }); 70 } 71 } 72