Solved

Drawing a background image on an applet

Posted on 1997-05-15
1
362 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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 67
netstat -ano | find "8000" and taskkill /f /pid 2984 3 55
Way to decrease size of apk file 9 65
eclipse apache tomcat admin console 52 94
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now