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