// ------------------------------------------------------------------- // Import Collections // ------------------------------------------------------------------- import java.awt.* ; import java.applet.* ; // ------------------------------------------------------------------- // User's Class Definition // ------------------------------------------------------------------- public class ChooseAColor2 extends Applet { // ------------------------------------------------------------------ // Declare a TextField Instance // ------------------------------------------------------------------ TextField mytext1 ; // ------------------------------------------------------------------ // init - Create 2 TextField Instances on the Screen // ------------------------------------------------------------------ public void init () { mytext1 = new TextField(5) ; add(mytext1) ; // mytext1.setText("1"); } // ------------------------------------------------------------------ // paint - Get the String , put it in an integer Variable , // set a color , print a text // ------------------------------------------------------------------ public void paint(Graphics g) { g.setColor(Color.black) ; g.drawString("Give a Number between 1 and 3", 70 , 50) ; String mystring1 = mytext1.getText() ; int choice = Integer.parseInt(mystring1); if ( choice == 1 ) { g.setColor(Color.blue) ; g.drawString("Blue", 70 , 70) ; } else if ( choice == 2 ) { g.setColor(Color.green) ; g.drawString("Green", 70 , 70) ; } else if ( choice == 3 ) { g.setColor(Color.red) ; g.drawString("Red", 70 , 70) ; } else { g.setColor(Color.black) ; g.drawString("Number given not between 1 and 3", 70 , 70) ; } } // ------------------------------------------------------------------ // action - repaint the screen // ------------------------------------------------------------------ public boolean action(Event event , Object arg) { repaint(); return true ; } }