Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Drawing a background image on an applet

Posted on 1997-05-15
1
Medium Priority
?
380 Views
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() {
            super.init();

            // Take out this line if you don't use symantec.itools.net.RelativeURL
        //symantec.itools.lang.Context.setDocumentBase(getDocumentBase());

            //{{INIT_CONTROLS
            setLayout(null);
            addNotify();
            resize(350,229);
            textField1 = new java.awt.TextField();
            textField1.reshape(96,61,150,28);
            add(textField1);
            textField2 = new java.awt.TextField();
            textField2.reshape(97,101,150,28);
            add(textField2);
            button1 = new java.awt.Button("button");
            button1.reshape(96,146,60,23);
            add(button1);
            button2 = new java.awt.Button("button");
            button2.reshape(177,146,59,23);
            add(button2);
            label1 = new java.awt.Label("This is my applet");
            label1.reshape(41,21,262,25);
            add(label1);
            //}}
      }

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

      //{{DECLARE_CONTROLS
      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.
0
Comment
Question by:Yoda
1 Comment
 

Accepted Solution

by:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

824 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