20.5. Initializing an Instance Variable with Method init

Our next applet (Fig. 20.10) computes the sum of two values input by the user and displays the result by drawing a String inside a rectangle on the applet. The sum is stored in an instance variable of class AdditionApplet, so it can be used in both method init and method paint. The HTML document to load this applet into the appletviewer is shown in Fig. 20.11

Figure 20.10. Adding double values.
 1 // Fig. 20.10: AdditionApplet.java
 2 // Adding two floating-point numbers.
 3 import java.awt.Graphics;       // program uses class Graphics
 4 import javax.swing.JApplet;     // program uses class JApplet
 5 import javax.swing.JOptionPane; // program uses class JOptionPane
 6
 7 public class AdditionApplet ...

Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.