/* *This interpreter is intended to paint different colored dots in a window. * * The commands that the interpreter can recognize and respond to are: * - BLUE: paint a blue dot * - RED: paint red dot * - HELP: show a list of commands in a dialogue message box * - EXIT: Terminate the program */ package interpreters; import java.awt.Color; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import painter.SPainter; import shapes.SCircle; public class Interpreter3 { private void interpreter() { // CREATE OBJECTS TO THINK WITH SPainter Tuchel = new SPainter("Dot Thing", 400, 400); Tuchel.setScreenLocation(0, 0); SCircle dot = new SCircle(180); //REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT while (true) { String command = JOptionPane.showInputDialog(null, "command?"); if (command == null) { command = "exit"; } //USER CLICKED ON CANCEL if (command.equalsIgnoreCase("blue")) { Tuchel.setColor(Color.BLUE); Tuchel.paint(dot); } else if (command.equalsIgnoreCase("red")) { Tuchel.setColor(Color.RED); Tuchel.paint(dot); } else if (command.equalsIgnoreCase("green")) { Tuchel.setColor(Color.GREEN); Tuchel.paint(dot); } else if (command.equalsIgnoreCase("yellow")) { Tuchel.setColor(Color.YELLOW); Tuchel.paint(dot); } else if (command.equalsIgnoreCase("random")) { Tuchel.setColor(randomColor()); Tuchel.paint(dot); } else if (command.equalsIgnoreCase("help")) { JOptionPane.showMessageDialog(null, "Valid commands are:" + "RED | BLUE | GREEN | YELLOW | HELP | RANDOM| EXIT"); } else if (command.equalsIgnoreCase("exit")) { Tuchel.end(); System.out.println("Thank you for viewing the dots..."); break; } else { JOptionPane.showMessageDialog(null, "Unrecognizable command:"); } } } private static Color randomColor() { int rv = (int) (Math.random() * 256); int gv = (int) (Math.random() * 256); int bv = (int) (Math.random() * 256); return new Color(rv, gv, bv); } //INFRASTRUCTURE FOR SOME SIMPLE PAINTING public Interpreter3() { interpreter(); } public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { public void run() { new Interpreter3(); } }); } }