1 package interpreters; 2 3 import java.awt.Color; 4 import javax.swing.JOptionPane; 5 import javax.swing.SwingUtilities; 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 10 public class Interpreter2 { 11 private void interpreter() { 12 // Create objects to think with. 13 SPainter miro = new SPainter("Dot Thing", 400, 400); 14 miro.setScreenLocation(0, 0); 15 SCircle dot = new SCircle(180); 16 // Repeatedly take a command from an input dialog box and interpret it 17 while (true) { 18 String command = JOptionPane.showInputDialog(null, "Command?"); 19 if (command == null) { 20 command = "exit"; 21 } // user click on cancel 22 if (command.equalsIgnoreCase("blue")) { 23 miro.setColor(Color.BLUE); 24 miro.paint(dot); 25 } else if (command.equalsIgnoreCase("red")) { 26 miro.setColor(Color.RED); 27 miro.paint(dot); 28 } else if (command.equalsIgnoreCase("help")) { 29 JOptionPane.showMessageDialog(null, "Valid commands are: " + "RED | BLUE | GREEN | YELLOW | HELP | EXIT "); 30 } else if (command.equalsIgnoreCase("exit")) { 31 miro.end(); 32 System.out.println("Thank you for viewing the dots ..."); 33 break; 34 } else if (command.equalsIgnoreCase("green")) { 35 miro.setColor(Color.GREEN); 36 miro.paint(dot); 37 } else if (command.equalsIgnoreCase("yellow")) { 38 miro.setColor(Color.YELLOW); 39 miro.paint(dot); 40 } else { 41 JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase()); 42 } 43 } 44 } 45 46 47 // Infrastructure for some simple painting 48 49 public Interpreter2() { 50 interpreter(); 51 } 52 53 public static void main(String[] args) { 54 SwingUtilities.invokeLater(new Runnable() { 55 public void run() { 56 new Interpreter2(); 57 } 58 }); 59 } 60 } 61