Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 6.6</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet6.class" WIDTH=500 HEIGHT=100> </APPLET> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.Applet; import java.awt.Font; public class Applet6 extends Applet { TextField userField = new TextField(5); int userNum = -1; Font f = new Font("TimesRoman",Font.BOLD,24); public void init() { setBackground(Color.white); add(userField); } public void paint(Graphics g) { String outStr = ""; int i; g.setFont(f); if (userNum < 1) { outStr = "Enter a number between 1 and 20"; } else { for (i=0; i<userNum; i++) { if (i >= 20) break; outStr = outStr + "*"; } } g.drawString(outStr, 20, 60); } public boolean action(Event e, Object arg) { if (e.target instanceof TextField) { try { userNum = Integer.parseInt(userField.getText()); } catch (NumberFormatException x) { userNum = -1; } repaint(); return true; } return false; } }