[GUI Image] attach an image as a background picture

R

Raydog

Idea:

have a JPanel, it has some buttons and textfields....

I want to add a backgroud image to this JPanel so that all the components on
it will
overlay on top of the background.

is this easy or possible to do ?

Thanks so much :)

raydog
 
K

Knute Johnson

Raydog said:
Idea:

have a JPanel, it has some buttons and textfields....

I want to add a backgroud image to this JPanel so that all the components on
it will
overlay on top of the background.

is this easy or possible to do ?

Thanks so much :)

raydog

Yes. Just override paintComponent() of the JPanel and draw your image.
Substitute your own image file name. You can also try setting the
JLabel to opaque to see what happens.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test4 extends JPanel {
BufferedImage image;
int w,h;

public test4() {
try {
image = ImageIO.read(new File("photo.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}

public Dimension getPreferredSize() {
return new Dimension(w,h);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image,0,0,this);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
JLabel l = new JLabel("Label",JLabel.CENTER);
t.add(l,"Center");
f.add(t);
f.pack();
f.setVisible(true);
}
}
 
R

Raydog

Hi Knute:
I tried your code, but read() always gives me exception "cannot read file",
and my pic is a jpg format too.

did i miss out anything ?

THanks so much,
raydog

Raydog said:
Idea:

have a JPanel, it has some buttons and textfields....

I want to add a backgroud image to this JPanel so that all the components on
it will
overlay on top of the background.

is this easy or possible to do ?

Thanks so much :)

raydog

Yes. Just override paintComponent() of the JPanel and draw your image.
Substitute your own image file name. You can also try setting the
JLabel to opaque to see what happens.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test4 extends JPanel {
BufferedImage image;
int w,h;

public test4() {
try {
image = ImageIO.read(new File("photo.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}

public Dimension getPreferredSize() {
return new Dimension(w,h);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image,0,0,this);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
JLabel l = new JLabel("Label",JLabel.CENTER);
t.add(l,"Center");
f.add(t);
f.pack();
f.setVisible(true);
}
}
 
R

Raydog

I think i got it fixed,
somehow jpg saved by some app does not have the first a few bytes
indicationg what image
format it is.
this caused a trouble when ImageIO reading the file in.

Hi Knute:
I tried your code, but read() always gives me exception "cannot read file",
and my pic is a jpg format too.

did i miss out anything ?

THanks so much,
raydog

Raydog said:
Idea:

have a JPanel, it has some buttons and textfields....

I want to add a backgroud image to this JPanel so that all the components on
it will
overlay on top of the background.

is this easy or possible to do ?

Thanks so much :)

raydog

Yes. Just override paintComponent() of the JPanel and draw your image.
Substitute your own image file name. You can also try setting the
JLabel to opaque to see what happens.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test4 extends JPanel {
BufferedImage image;
int w,h;

public test4() {
try {
image = ImageIO.read(new File("photo.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}

public Dimension getPreferredSize() {
return new Dimension(w,h);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image,0,0,this);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
JLabel l = new JLabel("Label",JLabel.CENTER);
t.add(l,"Center");
f.add(t);
f.pack();
f.setVisible(true);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top