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: paints a randomly colored dot 10 * - HELP: show a list of the commands in a dialog message box 11 * - EXIT: terminate the program 12 */ 13 package interpreters; 14 15 import java.awt.Color; 16 import javax.swing.JOptionPane; 17 import javax.swing.SwingUtilities; 18 import painter.SPainter; 19 import shapes.SCircle; 20 21 public class Interpreter3 { 22 private void interpreter() { 23 // Create objects to "think" with. 24 SPainter gage = new SPainter("Dot Program", 400, 400); 25 gage.setScreenLocation(0, 0); 26 SCircle dot = new SCircle(180); 27 28 // Repeatedly takes 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 clicks "Cancel" prompt 32 if (command.equalsIgnoreCase("blue")) { 33 gage.setColor(Color.BLUE); 34 gage.paint(dot); 35 } else if (command.equalsIgnoreCase("red")) { 36 gage.setColor(Color.RED); 37 gage.paint(dot); 38 } else if (command.equalsIgnoreCase("green")) { 39 gage.setColor(Color.GREEN); 40 gage.paint(dot); 41 } else if (command.equalsIgnoreCase("yellow")) { 42 gage.setColor(Color.YELLOW); 43 gage.paint(dot); 44 } else if (command.equalsIgnoreCase("random")) { 45 gage.setColor(randomColor()); 46 gage.paint(dot); 47 } else if (command.equalsIgnoreCase("help")) { 48 JOptionPane.showMessageDialog(null,"Valid commands are: " 49 + "RED | BLUE | GREEN | YELLOW | RANDOM | HELP | EXIT"); 50 } else if (command.equalsIgnoreCase("exit")) { 51 gage.end(); 52 System.out.println("Thank you for viewing the dots program."); 53 break; 54 } else { 55 JOptionPane.showMessageDialog(null, "Unrecognizable command:" 56 + command.toUpperCase()); 57 } 58 } 59 } 60 // Creates method to select a random color. 61 private Color randomColor() { 62 int rv = (int)(Math.random()*256); 63 int gv = (int)(Math.random()*256); 64 int bv = (int)(Math.random()*256); 65 return new Color(rv,gv,bv); 66 } 67 68 //Infrastructure for simple painting. 69 public Interpreter3() { 70 interpreter(); 71 } 72 public static void main(String[] args) { 73 SwingUtilities.invokeLater(new Runnable() { 74 public void run() { 75 new Interpreter3(); 76 } 77 }); 78 } 79 }