Drawing a background image on an applet

Posted on 1997-05-15
Medium Priority
Last Modified: 2008-03-10
I have an applet that just has a few textfields and two buttons on it.  The html page that this applet runs on has
a background image called altgry02.jpg.  How can I draw this background image on the applet also so that the applet appears to blend in with the html page.  This is the code for my applet:

    A basic extension of the java.applet.Applet class

import java.awt.*;
import java.applet.*;

public class TestApplet extends Applet {

      public void init() {

            // Take out this line if you don't use symantec.itools.net.RelativeURL

            textField1 = new java.awt.TextField();
            textField2 = new java.awt.TextField();
            button1 = new java.awt.Button("button");
            button2 = new java.awt.Button("button");
            label1 = new java.awt.Label("This is my applet");

      public boolean handleEvent(Event event) {
            return super.handleEvent(event);

      java.awt.TextField textField1;
      java.awt.TextField textField2;
      java.awt.Button button1;
      java.awt.Button button2;
      java.awt.Label label1;

What do I need to add to get the background image altgry02.jpg to be drawn on this applet and then have
the components on the applet drawn over this image?
Thanks in advance.
Question by:Yoda
1 Comment

Accepted Solution

Ted Hopp earned 100 total points
ID: 1220467
You can draw a background for your applet by writing a paint() method for the applet that draws the image. (Retrieve the image using the applet's getImage(URL) routine.)

Unfortunately, there is no way to ensure that the applet's background image lines up seamlessly with the background for the page. The problem is that browsers do not start wallpapering the background for a page at the top left pixel! That is, the background image is shifted some number of pixels horizontally, vertically, or both as it is painted. What's worse, this effect varies wildly across browsers.

The gory details can be found in an article, "Browser Offset is Not Your Friend" (http://www.dsiegel.com/tips/wonk14/) by David Siegel.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
A discussion about automated testing of Web Applications utilizing Selenium, along with illustrated configuration steps for the Jenkins open source tool.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question