1 /* 2 This is a program that is going to paint dots of different colors. 3 The commands that the interpreter recognizes: 4 BLUE 5 RED 6 HELP: show list of commands 7 EXIT: terminate program 8 */ 9 10 package interpreters; 11 import java.awt.Color; 12 import javax.swing.JOptionPane; 13 import javax.swing.SwingUtilities; 14 import painter.SPainter; 15 import shapes.SCircle; 16 17 import javax.swing.*; 18 19 20 public class Interpreter1 { 21 private void interpreter() { 22 // CREATE OBJECTS TO THINK WITH 23 SPainter miro = new SPainter("Dot Thing", 400,400); 24 miro.setScreenLocation(0,0); 25 SCircle dot = new SCircle(180); 26 27 // REPEATEDLY TAKE A COMMAND FROM THE INPUT DIALOG BOX AND INTERPRET IT 28 while ( true ) { 29 String command = JOptionPane.showInputDialog(null, "Command?"); 30 if ( command == null ) { command = "exit"; } // user clicked on Cancel 31 if ( command.equalsIgnoreCase("blue") ) { 32 miro.setColor(Color.BLUE); 33 miro.paint(dot); 34 } 35 else if ( command.equalsIgnoreCase("red") ) { 36 miro.setColor(Color.RED); 37 miro.paint(dot); 38 } 39 else if ( command.equalsIgnoreCase("help")) { 40 JOptionPane.showMessageDialog(null, 41 "Valid commands are: " + "RED | BLUE | HELP | EXIT" ); 42 } 43 else if ( command.equalsIgnoreCase("exit") ) { 44 miro.end(); 45 System.out.println("Thank you for viewing the dots ..."); 46 break; 47 } 48 else { 49 JOptionPane.showMessageDialog(null, "Unrecognizable command: "); 50 } 51 } 52 } 53 54 // REQUIRED INFRASTRUCTURE FOR PAINTING 55 56 public Interpreter1() { 57 interpreter(); } 58 public static void main(String[] args) { 59 SwingUtilities.invokeLater(new Runnable() { 60 public void run() { 61 new Interpreter1(); 62 } 63 }); 64 } 65 } 66 67