Simple Applet Display Methods

Comprehensive study notes, diagrams, and exam preparation for Simple Applet Display Methods.

Simple Applet Display Methods

Definition

Simple Applet Display Methods are the basic ways used in Java applets to show text, graphics, colors, and other visual output inside the applet window. In the Applet class, these display actions are mainly performed by methods such as paint(), repaint(), showStatus(), and the graphics methods available through the Graphics object passed to paint(). These methods help an applet present information on the screen and update the display whenever needed.


Main Content

1. paint(Graphics g) Method

  • paint(Graphics g) is the most important display method in a Java applet because it is automatically called by the browser or applet viewer whenever the applet needs to draw itself.
  • The method receives a Graphics object g, which provides drawing functions such as drawString(), drawLine(), drawRect(), fillRect(), drawOval(), and many others.

Detailed explanation:

  • The paint() method is where all the visual output of an applet is usually written.
  • It is called whenever the applet starts, is resized, uncovered after being hidden, or when repaint() is requested.
  • Inside this method, the programmer can draw text, shapes, images, and colors on the applet screen.
  • The paint() method belongs to the display process because an applet does not usually create its own window; instead, it draws on the area provided by the browser or applet viewer.

Example:

import java.applet.Applet;
import java.awt.Graphics;

public class DisplayApplet extends Applet {
    public void paint(Graphics g) {
        g.drawString("Welcome to Applet Display Methods", 20, 30);
        g.drawRect(20, 50, 150, 80);
    }
}

Key points:

  • paint() is called automatically.
  • It requires a Graphics object.
  • It is used for all drawing operations.

2. repaint() Method

  • repaint() is used to request that the applet be redrawn.
  • It does not draw directly; instead, it tells the system that the applet should call update() and then paint() again.

Detailed explanation:

  • When the applet content changes, the display must be refreshed.
  • Instead of calling paint() directly, the programmer should call repaint().
  • This is important because the AWT and applet system manage the drawing process internally, and repaint() works safely within that mechanism.
  • repaint() is useful when text, shapes, or values displayed in the applet have changed due to user actions or timers.

Example:

import java.applet.Applet;
import java.awt.Graphics;

public class RepaintApplet extends Applet {
    int x = 20;

    public void paint(Graphics g) {
        g.drawString("X position: " + x, 20, 30);
    }

    public void updatePosition() {
        x = x + 10;
        repaint();
    }
}

Key points:

  • repaint() refreshes the screen.
  • It triggers the painting process indirectly.
  • It is preferred over calling paint() manually.

3. showStatus(String msg) Method

  • showStatus(String msg) displays a message in the browser’s status area instead of inside the applet window.
  • It is useful for showing short messages, instructions, or feedback to the user.

Detailed explanation:

  • Applets often need to communicate with the user without cluttering the drawing area.
  • The showStatus() method is used for temporary messages.
  • The message is usually visible in the status bar of the browser or applet viewer.
  • This method is especially helpful for showing mouse coordinates, action messages, or loading information.

Example:

import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class StatusApplet extends Applet implements MouseMotionListener {
    public void init() {
        addMouseMotionListener(this);
    }

    public void mouseMoved(MouseEvent e) {
        showStatus("Mouse at: " + e.getX() + ", " + e.getY());
    }

    public void mouseDragged(MouseEvent e) {
        showStatus("Dragging at: " + e.getX() + ", " + e.getY());
    }
}

Key points:

  • showStatus() prints messages in the status bar.
  • It improves user interaction.
  • It is not used for drawing on the applet canvas.

Working / Process

  1. The applet is loaded by the browser or applet viewer, and its init() method is executed to prepare the applet for use.
  2. When the applet needs to display output, the system automatically calls paint(Graphics g), where all drawing commands are written using the Graphics object.
  3. If the content changes, repaint() is called to request a new drawing cycle, and showStatus() may be used at any time to display messages in the status bar.

Advantages / Applications

  • Allows the applet to display text, shapes, and graphical output easily within the browser area.
  • Makes it possible to update the screen dynamically when data or user input changes.
  • Helps provide user feedback through status messages without using extra screen space.

Summary

  • paint() is the main method for displaying output in an applet.
  • repaint() is used to refresh the applet display.
  • showStatus() displays messages in the browser status area.
  • Important terms to remember: Applet, Graphics, paint(), repaint(), showStatus(), update(), drawString(), drawRect()