How to set a background image in a Java Applet?
15,555
Solution 1
The Following could be a solution:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.IOException.*;
public class BackgroundApplet extends Applet {
Image backGround;
public void init() {
// set the size of the applet to the size of the background image.
// Resizing the applet may cause distortion of the image.
setSize(300, 300);
// Set the image name to the background you want. Assumes the image
// is in the same directory as the class file is
backGround = getImage(getCodeBase(), "save.GIF");
BackGroundPanel bgp = new BackGroundPanel();
bgp.setLayout(new FlowLayout());
bgp.setBackGroundImage(backGround);
// Add the components you want in the Applet to the Panel
bgp.add(new Button("Button 1"));
bgp.add(new TextField("isn't this cool?"));
bgp.add(new Button("Useless Button 2"));
// set the layout of the applet to Border Layout
setLayout(new BorderLayout());
// now adding the panel, adds to the center
// (by default in Border Layout) of the applet
add(bgp);
}
}
class BackGroundPanel extends Panel {
Image backGround;
BackGroundPanel() {
super();
}
public void paint(Graphics g) {
// get the size of this panel (which is the size of the applet),
// and draw the image
g.drawImage(getBackGroundImage(), 0, 0,
(int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
}
public void setBackGroundImage(Image backGround) {
this.backGround = backGround;
}
private Image getBackGroundImage() {
return backGround;
}
}
You'll find further Information here: Background Images in Java Applets
Solution 2
Here is many possible answers.
Here is the one of them.
Suppose that you have a Class BcgImageApplet which extends Applet: In order to set background image to your applet you should use this:
public class BcgImageApplet extends Applet {
Image I;
//Irelevant code avoided.
public void init() {
I=getImage(getCodeBase(),”your_picture.jpg”);
}
//Irelevant code avoided.
}
In short - in your init() method you have to set image to your Applet.
Author by
Tom Doyle
Hi. I'm Tom Doyle. I'm a web developer and designer. I own and manage a VPS in my free time. I code in PHP and mySQLi.
Updated on June 04, 2022Comments
-
Tom Doyle almost 2 years
How would I set a background-image in a Java Applet?
I have an animated .gif image, and I want it to be the background in my Java Applet. How would I do this?