buenas a todos !!
les cuento mi duda estoy tratando de ponerle un background a mi jpanel pasandole una image preeviamente cargada , cuando trato de dibujar con la funcion heredada de PaintComponent la imagen aparece null , es extrano por que todas las imagenes que pongo las uso en otro componente es mas antes de pasarla verifico de varias formas que no sea nulo pero al final es lo mismo siempre, mas abajo esta el codigo de jpanel
public class RockPosPanel extends JPanel{
Image Icon;
String url;
int x, y,w,h;
JLabel label;
public RockPosPanel(){
super(null);
super.setLayout(null);
}
public RockPosPanel(String str){
this();
x = 0;
y = 0;
w = 100;
h = 100;
url = str;
if(str != null)
{
try {
System.out.print("Cargando imagen");
Icon = ImageIO.read(new File(str));
if(Icon == null)
System.out.println("La image es null desde que la cargan");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void SetImage(Image im, int x, int y, int w, int h)
{
if(im != null)
this.Icon = im;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(Icon == null)
{
if(url !=null)
try {
Icon = ImageIO.read(new File(url));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(Icon == null)
System.out.println("La image es null desde que la cargan");
System.out.println("La image es null");
}
if(Icoo)
g.drawImage(Icon,x,y, w, h, this);
///System.out.println("image is null");
}
}
y cuando pruebo la isntancia de la clase :
Cargando imagen
la imagen no es null
image is null
image is null
image is null
image is null
image is null
image is null
quizas me falto implementar algo pero la verdad no he encontrado informacion en la red
si alguien me diera una recomendacion, seria mas que suficiente.
gracias de antemano.
no me compila pon el código completo
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
class RockPosPanel extends JPanel{
Image Icon;
String url;
int x, y,w,h;
JLabel label;
public RockPosPanel(){
super(null);
super.setLayout(null);
}
public RockPosPanel(String str){
this();
x = 0;
y = 0;
w = 100;
h = 100;
url = str;
if(str != null)
{
try {
System.out.print("Cargando imagen");
Icon = ImageIO.read(new File(str));
if(Icon == null)
System.out.println("La image es null desde que la cargan");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void SetImage(Image im, int x, int y, int w, int h)
{
if(im != null)
this.Icon = im;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(Icon == null)
{
if(url !=null)
try {
Icon = ImageIO.read(new File(url));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(Icon == null)
System.out.println("La image es null desde que la cargan");
System.out.println("La image es null");
}
if(Icon !=null)
g.drawImage(Icon,x,y, w, h, this);
///System.out.println("image is null");
}
}
ese es el codigo completo , no deberia darte errores.