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