Minimal Applet

suggest change

A very simple applet draws a filled rectangle and prints a string on the screen.

public class MyApplet extends JApplet{ 

    private String str = "StackOverflow";

    @Override
    public void init() {
        setBackground(Color.gray);
    }
    @Override
    public void destroy() {}
    @Override
    public void start() {}
    @Override
    public void stop() {}
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.yellow);
        g.fillRect(1,1,300,150);
        g.setColor(Color.red);
        g.setFont(new Font("TimesRoman", Font.PLAIN, 48));
        g.drawString(str, 10, 80);
    }
}

The main class of an applet extends javax.swing.JApplet.

Before Java 1.2 and the introduction of the Swing API, applets extended java.applet.Applet.

Applets don’t require a main method. The entry point is controlled by the life cycle. To use them, they need to be embedded in an HTML document. This is also the point where their size is defined.

<html>
  <head></head>
  <body>
     <applet code="MyApplet.class" width="400" height="200"></applet>
  </body>
</html>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents