elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Curso de javascript por TickTack


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  JPanel no dibuja imagen [inherit]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: JPanel no dibuja imagen [inherit]  (Leído 1,882 veces)
bash

Desconectado Desconectado

Mensajes: 258



Ver Perfil
JPanel no dibuja imagen [inherit]
« en: 9 Diciembre 2016, 14:10 pm »

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

Código
  1. public class RockPosPanel  extends JPanel{
  2.  
  3.      Image Icon;
  4.      String url;
  5.  int x, y,w,h;
  6.  
  7.  JLabel label;
  8.  
  9.  
  10.   public RockPosPanel(){
  11.   super(null);
  12.       super.setLayout(null);
  13.   }
  14.  
  15.   public RockPosPanel(String str){
  16.   this();
  17.  
  18.   x = 0;
  19.   y = 0;
  20.   w = 100;
  21.   h = 100;
  22.   url = str;
  23.   if(str != null)
  24.   {
  25.   try {
  26.       System.out.print("Cargando imagen");
  27.   Icon = ImageIO.read(new File(str));
  28.       if(Icon == null)
  29.       System.out.println("La image es null desde que la cargan");
  30.  
  31.   } catch (IOException e) {
  32.  
  33. e.printStackTrace();
  34. }
  35.  
  36.  
  37.  
  38.   }
  39.   }
  40.  
  41.  
  42.  
  43.   public void SetImage(Image im, int x, int y, int w, int h)
  44.   {
  45.  
  46.   if(im != null)
  47.   this.Icon = im;
  48.  
  49.   this.x = x;
  50.   this.y = y;
  51.   this.w = w;
  52.   this.h = h;
  53.   }
  54.  
  55. @Override
  56.  protected void paintComponent(Graphics g) {
  57.    super.paintComponent(g);
  58.  
  59.      if(Icon == null)
  60.      {
  61.      if(url !=null)
  62. try {
  63. Icon = ImageIO.read(new File(url));
  64. } catch (IOException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. }
  68.       if(Icon == null)
  69.       System.out.println("La image es null desde que la cargan");      
  70.  
  71.      System.out.println("La image es null");
  72.      }
  73.  
  74.      if(Icoo)
  75.      g.drawImage(Icon,x,y, w, h, this);
  76.  
  77.     ///System.out.println("image is null");
  78.  
  79. }
  80.  
  81. }
  82.  


y cuando pruebo la isntancia de la clase :
Código
  1. Cargando imagen
  2. la imagen no es null
  3. image is null
  4.  
  5. image is null
  6.  
  7. image is null
  8.  
  9. image is null
  10.  
  11. image is null
  12.  
  13. 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.


En línea

gracias por responder mis dudas
sapito169


Desconectado Desconectado

Mensajes: 628



Ver Perfil
Re: JPanel no dibuja imagen [inherit]
« Respuesta #1 en: 10 Diciembre 2016, 18:28 pm »

no me compila pon el código completo


En línea

bash

Desconectado Desconectado

Mensajes: 258



Ver Perfil
Re: JPanel no dibuja imagen [inherit]
« Respuesta #2 en: 13 Diciembre 2016, 17:30 pm »


Código
  1. import java.awt.Graphics;
  2. import java.awt.Image;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.Scanner;
  8.  
  9. import javax.imageio.ImageIO;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12.  
  13.  class RockPosPanel  extends JPanel{
  14.  
  15.    Image Icon;
  16.    String url;
  17.  int x, y,w,h;
  18.  
  19.  JLabel label;
  20.  
  21.  
  22.   public RockPosPanel(){
  23.   super(null);
  24.       super.setLayout(null);
  25.   }
  26.  
  27.   public RockPosPanel(String str){
  28.   this();
  29.  
  30.   x = 0;
  31.   y = 0;
  32.   w = 100;
  33.   h = 100;
  34.   url = str;
  35.   if(str != null)
  36.   {
  37.   try {
  38.       System.out.print("Cargando imagen");
  39.   Icon = ImageIO.read(new File(str));
  40.       if(Icon == null)
  41.       System.out.println("La image es null desde que la cargan");
  42.  
  43.   } catch (IOException e) {
  44.  
  45. e.printStackTrace();
  46. }
  47.  
  48.  
  49.  
  50.   }
  51.   }
  52.  
  53.  
  54.  
  55.   public void SetImage(Image im, int x, int y, int w, int h)
  56.   {
  57.  
  58.   if(im != null)
  59.   this.Icon = im;
  60.  
  61.   this.x = x;
  62.   this.y = y;
  63.   this.w = w;
  64.   this.h = h;
  65.   }
  66.  
  67. @Override
  68.  protected void paintComponent(Graphics g) {
  69.    super.paintComponent(g);
  70.  
  71.      if(Icon == null)
  72.      {
  73.      if(url !=null)
  74. try {
  75. Icon = ImageIO.read(new File(url));
  76. } catch (IOException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80.       if(Icon == null)
  81.       System.out.println("La image es null desde que la cargan");      
  82.  
  83.      System.out.println("La image es null");
  84.      }
  85.  
  86.      if(Icon !=null)
  87.      g.drawImage(Icon,x,y, w, h, this);
  88.  
  89.     ///System.out.println("image is null");
  90.  
  91. }
  92.  
  93. }
  94.  
  95.  

ese es el codigo completo , no deberia darte errores.

En línea

gracias por responder mis dudas
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Agregar imagen a JPanel
Java
gasoft 4 18,558 Último mensaje 30 Octubre 2009, 06:47 am
por gasoft
Visualizar imagen en jPanel de una ventana principal
Java
KidJoe 3 4,776 Último mensaje 2 Septiembre 2010, 21:23 pm
por Leyer
Colocar una imagen en un Jpanel error
Java
mapers 1 3,563 Último mensaje 9 Agosto 2011, 21:13 pm
por JonathanR
Añadir imagen a un JPanel
Java
juancaa 3 4,639 Último mensaje 22 Junio 2012, 13:24 pm
por juancaa
poner imagen en jpanel
Java
owen 2 4,726 Último mensaje 21 Febrero 2016, 22:18 pm
por owen
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines