/* * 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 a red dot * - HELP: show a list of the commands in a dialog message box * - EXIT: terminate the program */ package interpreters; import painter.SPainter; import shapes.SCircle; import javax.swing.*; import java.awt.*; import static java.lang.Math.random; public class interpreter3 { //*Step 0: Method call - Main method and the obj call, if there are particular objs you're gonna use. public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new interpreter3(); } }); } //* Step 1: Remind what things you're gonna create, surely about your main class. private interpreter3() { //*Step 2: CREATE OBJECTS TO THINK WITH SPainter mirror = new SPainter("Dot Thing", 400, 400); mirror.setScreenLocation(0, 0); SCircle dot = new SCircle(180); //*Step 3: REPEATEDLY TAKE A COMMAND FROM AN INPUT DIALOG BOX AND INTERPRET IT while (true) { String command = JOptionPane.showInputDialog(null, "Command?"); //* Selection 1 if (command == null) { command = "exit"; } // user clicked on Cancel //* Selection 2 if (command.equalsIgnoreCase("BLUE")) { mirror.setColor(Color.BLUE); mirror.paint(dot); } //* Selection 3 else if (command.equalsIgnoreCase("RED")) { mirror.setColor(Color.RED); mirror.paint(dot); } //* Selection 4 else if (command.equalsIgnoreCase("Green")) { mirror.setColor(Color.green); mirror.paint(dot); } //* Selection 5 else if (command.equalsIgnoreCase("Yellow")) { mirror.setColor(Color.YELLOW); mirror.paint(dot); } else if (command.equalsIgnoreCase("Random")) { mirror.setColor(randomColor()); mirror.paint(dot); } //* Selection 6 else if (command.equalsIgnoreCase("HELP")) { JOptionPane.showMessageDialog(null, "Vaild commands are" + "BLUE|RED|GREEN|YELLOW|Random|HELP|EXIT"); } //* Selection 7 else if (command.equalsIgnoreCase("EXIT")) { mirror.end(); System.out.println("Thank you for viewing the dots ..."); break; } //* This case: NOT DECIDE ANY given selection | All the other cases if not take any selection above else { JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); } } } private static Color randomColor() { double rv = (int) (Math.random()*256); int gv = (int) (Math.random()*256); double bv = (int) (Math.random()*256); return new Color((int)rv,(int) gv,(int) bv); } }