Add a background image to applet
Hi. Sorry if this question has been asked to death, but i have searched for the best part of the last 2 hours for a solution to my problem which is as follows:
I have been tying to place a background image on an applet for part of a project i have to do at uni. From what i have read this is very simple and i have tried all possiblities to get it working, all to no avail.
The code below is what i have for this:
import java.awt.*;
import java.applet.*;
public class BackImage extends Applet
{
Image background;
public void init()
{
background = getImage(getCodeBase(), "Images/back.gif");
setVisible(true);
}
public void paint(Graphics g)
{
g.drawImage(background, 0, 0, this);
}
}
When using textpad to compile and run this, the command window opens and closes instantly and the applet window doesnt even open. when opening the html file in firefox, all i am greeted with is a white applet area but no error messages in the console.
I have checked and checked again that locations for the image are correct and all seems fine, so i put it to you guys to help me if you could.
thanks
=============
S.Harvey
[1255 byte] By [
KooTwoa] at [2007-11-26 19:19:53]

1. Switch from the AWT to Swing.
2. My favorite way it to draw the image with a border. That way I can
decorate an existing container without having to subclass it. Have fun!
import java.awt.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.*;
public class BackgroundBorderExample {
public static void main(String[] args) throws IOException {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame("BackgroundBorderExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea area = new JTextArea(24,80);
area.setForeground(Color.WHITE);
area.setOpaque(false);
area.read(new FileReader(new File("BackgroundBorderExample.java")), null);
final JScrollPane sp = new JScrollPane(area);
sp.setBackground(Color.BLACK);
sp.getViewport().setOpaque(false);
f.getContentPane().add(sp);
f.setSize(600,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
String url = "http://blogs.sun.com/jag/resource/JagHeadshot.jpg";
final Border bkgrnd = new CentredBackgroundBorder(ImageIO.read(new URL(url)));
Runnable r = new Runnable() {
public void run() {
sp.setViewportBorder(bkgrnd);
sp.repaint();
}
};
SwingUtilities.invokeLater(r);
}
}
import java.awt.*;
import java.awt.image.*;
import javax.swing.border.*;
public class CentredBackgroundBorder implements Border {
private final BufferedImage image;
public CentredBackgroundBorder(BufferedImage image) {
this.image = image;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
int x0 = x + (width-image.getWidth())/2;
int y0 = y + (height-image.getHeight())/2;
g. drawImage(image, x0, y0, null);
}
public Insets getBorderInsets(Component c) {
return new Insets(0,0,0,0);
}
public boolean isBorderOpaque() {
return true;
}
}
Here's the demo as an applet.
import java.awt.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.*;
public class BackgroundBorderExampleApplet extends JApplet {
public void init() {
JTextArea area = new JTextArea(24,80);
area.setText("add some text");
area.setForeground(Color.WHITE);
area.setOpaque(false);
final JScrollPane sp = new JScrollPane(area);
sp.setBackground(Color.BLACK);
sp.getViewport().setOpaque(false);
this.getContentPane().add(sp);
String url = "http://blogs.sun.com/jag/resource/JagHeadshot.jpg";
try {
final Border bkgrnd = new CentredBackgroundBorder(ImageIO.read(new URL(url)));
Runnable r = new Runnable() {
public void run() {
sp.setViewportBorder(bkgrnd);
sp.repaint();
}
};
SwingUtilities.invokeLater(r);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks again for the replies. In case you're interested, my code now looks like this:
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import javax.swing.*;
import java.net.*;
public class BackgroundTest extends Applet
{
Image background;
public void init()
{
try{
background = getImage(new URL("file:///c:/documents and settings/sandra/desktop/uni work/coding/coding/images/back.gif"));//getCodeBase(), "back.gif");
}
catch(Exception e){e.printStackTrace();}
setVisible(true);
}
public void paint(Graphics g)
{
g.drawImage(background, 0, 0, this);
}
}
=========
S.Harvey