1 package interpreters; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 6 import javax.swing.*; 7 import java.awt.*; 8 9 public class Interpreter3 { 10 private void interpreter() { 11 // create objects to think with 12 SPainter micro = new SPainter("Dot Thing",400,400); 13 micro.setScreenLocation(0,0); 14 SCircle dot = new SCircle(180); 15 16 //repeatedly take a command from an inpit dialog box and interpret it 17 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 micro.setColor(Color.blue); 23 micro.paint(dot); 24 } else if (command.equalsIgnoreCase("red")) { 25 micro.setColor(Color.red); 26 micro.paint(dot); 27 } else if (command.equalsIgnoreCase("green")) { 28 micro.setColor(Color.green); 29 micro.paint(dot); 30 } else if (command.equalsIgnoreCase("yellow")) { 31 micro.setColor(Color.yellow); 32 micro.paint(dot); 33 } else if (command.equalsIgnoreCase("random")) { 34 micro.setColor(randomColor()); 35 micro.paint(dot); 36 } else if (command.equalsIgnoreCase("help")) { 37 JOptionPane.showMessageDialog(null,"Valid commands are: " + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT"); 38 39 } else if (command.equalsIgnoreCase("exit")) { 40 micro.end(); 41 System.out.println("Thank you for viewing the dots..."); 42 break; 43 44 } else { 45 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); 46 47 } 48 } 49 } 50 51 private Color randomColor() { 52 int rv = (int) (Math.random()*256); 53 int gv = (int) (Math.random()*256); 54 int bv = (int) (Math.random()*256); 55 return new Color(rv,gv,bv); 56 } 57 // Infrastructure for some simple painting 58 59 public Interpreter3() { 60 interpreter(); 61 62 } 63 public static void main(String[] args) { 64 SwingUtilities.invokeLater(new Runnable() { 65 public void run() { 66 new Interpreter3(); 67 68 } 69 }); 70 } 71 } 72 73