Click to See Complete Forum and Search --> : How to set a special gif as applet-background?


Andi
September 14th, 2000, 05:57 AM
Hi all

does anybody know:

1. how I can set a picture (jpg, gif, ...) as background for my applet?

2. how I can set a picture (jpg, gif, ...) for a special area?

Thanx,
Andi

rums1976
September 14th, 2000, 03:59 PM
>1. how I can set a picture (jpg, gif, ...) as background for my applet?

I dont think there is a function to do this. But you can extend a Panel (which can act as a simple container of components) and override the paint method to draw an image in the background. The following is a sample program. Hope this helps.

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("isnt 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;
}
}




>2. how I can set a picture (jpg, gif, ...) for a special area?
Iam not clear with this question.

rums1976

Andi
September 15th, 2000, 01:53 AM
Hi again,

thanx for the sample, quite cool - exactly what I was searching for!

Andi

Andi
September 15th, 2000, 03:07 AM
Hi again,

one more question.....

I want to put my controls (checkboxes) under each other, so I canīt use FlowLayout....I have to use GridLayout (?).
But when I use GridLayout, my background picture wonīt be shown as background, but in the bottom (beneath the controls....).

Whatīs my mistake?
Andi


int count= GetNumberOfCheckBoxes();
setSize(300,300);
backGround = getImage(getCodeBase(),"image.jpg");
BackGroundPanel bgp = new BackGroundPanel();
//bgp.setLayout(new FlowLayout());
bgp.setLayout(new GridLayout(count+2,0));
bgp.setBackGroundImage(backGround);

//create CheckBoxes
for (int i = 1; i <= count++)
{
Checkbox box = new Checkbox(GetCheckBoxText(), null, false);
box.addItemListener(this);
bgp.add(box);
}
// set the layout of the applet to Border Layout
setLayout(new BorderLayout());
add(bgp);

rums1976
September 15th, 2000, 03:49 PM
Hi
GridLayout divides the container into number of rows and columns. The component occupying a cell would fit the cell size. So all the components occupy the entire space, so you will not be able to see the background image. The best is to use the GridBagLayout in this circumstance. GridBagLayout is a custom layout for users to specify how the components want to occupy the space.
The GridBagConstraints class has various methods to position and adjust your component. The following is the same sample code with GridBagLayout and GridBagConstraints. Change it to your needs. Hope this helps. Also look at the API for GridBagLayout and the GridBagConstraints and make modifications as per your requirement.


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();

GridBagLayout gbl = new GridBagLayout();
bgp.setBackGroundImage(backGround);

bgp.setLayout(gbl);

GridBagConstraints bgc = new GridBagConstraints();

// Add the components you want in the Applet to the Panel
int count=4 ;// use your GetNumberOfCheckBoxes() here;
bgc.weightx = 1.0;
// initialize the number of Checkboxes
Checkbox[] box = new Checkbox[count];
for (int i = 0; i < count; i++){
String cbLabel = "Check box " + (i+1); // user your GetCheckBoxText() here
box[i] = new Checkbox(cbLabel,null,false);
// uncomment following line in your program
// box[i].addItemListener(this);
bgc.gridy = i;
gbl.setConstraints(box[i],bgc); // whatever constrains specified applies to the component passed
bgp.add(box[i]);
}

// 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 draws 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;
}
}




Hope this helps
rums1976

Andi
September 16th, 2000, 01:10 AM
Hi Buddy,

thanks a lot - perfect answer!

Andi