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 * -BLUE: paint a blue dot 6 * -RED: paint a red dot 7 * -GREEN: paint a green dot 8 * -YELLOW: paint a yellow dot 9 * -RANDOM: paint a random color 10 * -HELP: show a list of the commands in a dialog message box 11 * -EXIT: terminate the program 12 * 13 */ 14 15 package interpreters; 16 17 import java.awt.Color; 18 import javax.print.attribute.standard.PresentationDirection; 19 import javax.swing.JOptionPane; 20 import javax.swing.SwingUtilities; 21 import painter.SPainter; 22 import shapes.SCircle; 23 24 25 import java.awt.*; 26 import java.sql.SQLOutput; 27 28 public class Interpreter3 { 29 30 private void interpreter(){ 31 32 //CREATE OBJECTS TO THINK WITH 33 SPainter miro= new SPainter("Dot thing", 400, 400); 34 miro.setScreenLocation(0,0); 35 SCircle dot= new SCircle(180); 36 37 //REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT 38 while (true){ 39 String command= JOptionPane.showInputDialog(null, "command?"); 40 if (command==null) {command="exit";} // user clicked on cancel 41 if (command.equalsIgnoreCase("blue")) { 42 miro.setColor(Color.BLUE); 43 miro.paint(dot); 44 } else if (command.equalsIgnoreCase("Red")) { 45 miro.setColor(Color.RED); 46 miro.paint(dot); 47 }else if (command.equalsIgnoreCase("Green")) { 48 miro.setColor(Color.GREEN); 49 miro.paint(dot); 50 } else if (command.equalsIgnoreCase("Yellow")) { 51 miro.setColor(Color.YELLOW); 52 miro.paint(dot); 53 } else if (command.equalsIgnoreCase("random")) { 54 miro.setColor(randomColor()); 55 miro.paint(dot); 56 57 58 59 } else if (command.equalsIgnoreCase("help")) { 60 JOptionPane.showMessageDialog(null, "Valid Commands are:" 61 + "RED| BLUE|GREEN|YELLOW|RANDOM| HELP| EXIT"); 62 63 } else if (command.equalsIgnoreCase("exit")) { 64 miro.end(); 65 System.out.println("Thank you for viewing the dots..."); 66 break; 67 68 } else { 69 JOptionPane.showMessageDialog(null, "Unrecognizable command:" 70 + command.toUpperCase()); 71 72 } 73 } 74 } 75 76 private static Color randomColor() { 77 78 int rv = (int) (Math.random() * 256); 79 int gv = (int) (Math.random() * 256); 80 int bv = (int) (Math.random() * 256); 81 return new Color(rv, gv, bv); 82 } 83 84 85 // INFRASTRUCTURE FOR SOME SIMPLE PAINTING 86 public Interpreter3() { 87 interpreter(); 88 89 } 90 public static void main(String []args) { 91 SwingUtilities.invokeLater(new Runnable() { 92 public void run() { 93 new Interpreter3(); 94 } 95 }); 96 } 97 }