Hello JApplet

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

The completed applet is given below.

Hello JApplet

The X and Y values in drawString place the lower left corner of the 'H' in the center of the drawing area. If you want the center of the string to be in the center of the drawing area, you would need to determine the dimensions of the string and do some calculation. There are methods that help you do this, but let's skip those details for now.

// Put "Hello" at the center of a 300 by 150 drawing area
//
import javax.swing.JApplet;
import java.awt.*;

public class AnotherHello extends JApplet
{
  public void paint ( Graphics gr )
  { 
    setBackground( Color.lightGray );
    gr.drawString("Hello", 150, 75 );
   }
}

Here is what the applet does with your Web browser:

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.

QUESTION 6:

  • What should the source file be named for this applet?
  • What file will be created when the source file is compiled?