Background image in JPanel
i' using eclipse3 and have this code:
private JPanel getJPanelCv(){
if (jPanelCv ==null){
...
jLabelNomeCv.setText("Nome da Conven玢o");
...
jPanelCv =new JPanel(){
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
/*
Color amarelo = new Color(255,255,210);
Color azul = new Color(225,245,255);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradiente = new GradientPaint (0, 0, azul, 600, 600, amarelo);
g2d.setPaint(gradiente);
g2d.fillRect(0,0, 846, 491);*/
Image imagem =new ImageIcon("img/mar.jpg").getImage();
g.drawImage(imagem, 0, 0,this);
}
};
...
}
return jPanelCv;
}
i'v my jpg inside img folder that lies inside bin dir; if i use gradient, all is fine, but cant get the background image to show up
Can anybody help sorting this out?
TiA
[1521 byte] By [
iRun4Funa] at [2007-10-2 12:52:02]

Your code is wrong for a custom panel implementation.
Try and study this:
import javax.swing.*;
import java.awt.*;
public class ImageGradientPanel extends JPanel{
Image image;
public ImageGradientPanel(){
image = new ImageIcon("kissy.png").getImage();
}
public Dimension getPreferredSize(){
return new Dimension(846, 491);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color amarelo = new Color(255,255,210);
Color azul = new Color(225,245,255);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradiente
= new GradientPaint (0, 0, azul, 600, 600, amarelo);
g2d.setPaint(gradiente);
g2d.fillRect(0,0, 846, 491);
g.drawImage(image, 100, 60, this);
}
/* main for testing */
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = frame.getContentPane();
con.add(new ImageGradientPanel(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
hiwaa at 2007-7-13 10:04:58 >

thanks for your reply, hiwa
i found out how to get it right:
jPanelUtentes = new JPanel() {
private static final long serialVersionUID = 5804996883063343937;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color amarelo = new Color(255,255,210);
Color azul = new Color(225,245,255);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradiente = new GradientPaint (0, 0, azul, 600, 600, amarelo);
g2d.setPaint(gradiente);
g2d.fillRect(0,0, 846, 491);
Image imagem = null;
try {
imagem = ImageIO.read(new java.net.URL(getClass()
.getResource("../img/mar.jpg"), "../img/mar.jpg"));
}
catch(Exception e){
System.out.println("n鉶 h?fundo");
}
g.drawImage(imagem, 0, 0, this);
}
};