The code snippet below show you how to get the URL of the document (HTML, JSP, etc.) where the Applet is embedded. To obtain this document URL we use the getDocumentBase()
method call provided by the Applet
class.
In the paint()
method below we use the getDocumentBase()
to create a URL as a link to an image to be displayed by our applet.
package org.kodejava.applet;
import java.applet.Applet;
import java.awt.*;
public class AppletDocumentBase extends Applet {
private Image logo;
@Override
public void init() {
// Locates logo image base on the URL of the document
// where the Applet is embedded which is returned by
// the getDocumentBase() method call.
//
// eg. http://localhost:8080/images/logo.jpg
logo = getImage(getDocumentBase(), "/images/logo.png");
}
@Override
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
// Draw the logo image on the Applet surface.
g.drawImage(logo, 10, 10, this);
}
}
** Deprecated: The Applet API is deprecated since JDK 9, no replacement.
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024