Foro de elhacker.net

Programación => Java => Mensaje iniciado por: bash en 9 Diciembre 2016, 14:10 pm



Título: JPanel no dibuja imagen [inherit]
Publicado por: bash 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.


Título: Re: JPanel no dibuja imagen [inherit]
Publicado por: sapito169 en 10 Diciembre 2016, 18:28 pm
no me compila pon el código completo


Título: Re: JPanel no dibuja imagen [inherit]
Publicado por: bash 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.