Cannot load image in constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ValValVal
    New Member
    • Oct 2008
    • 27

    Cannot load image in constructor

    Hi!
    Please help with following question, this is really something I have not a clue why this is happening.

    I want to display BufferedImage in my JFrame. Now I need to load images first from disk.

    if I execute this code in constructor of the program and after try to paint
    in my paint function then nothing is painted.
    =============== =
    in constructor:
    [code=java]
    try {
    whitePawn = ImageIO.read(ne w File("c:\\final big\\pawnwhite. gif"));
    } catch (IOException e) {System.out.pri ntln(" Cannot load images");}
    [/code]
    in paint:[code=java]
    g2d.drawImage(w hitePawn, 0,30, this);[/code]
    =============== =

    and if I put loading instruction in paint before drawImage() it works ok.
    =============== =
    all in paint:
    [code=java]
    try {
    whitePawn = ImageIO.read(ne w File("c:\\final big\\pawnwhite. gif"));
    } catch (IOException e) {System.out.pri ntln(" Cannot load images");}

    g2d.drawImage(w hitePawn, 0,30, this);
    [/code]
    ===============

    Variable whitePawn is a field.
    If needed I can post more code.
    Thanks in advance for answering my stupid question.
    Last edited by Nepomuk; Nov 19 '08, 10:33 PM. Reason: Please use [CODE] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Is anything that effects any of the variables used done in between?

    Greetings,
    Nepomuk

    Comment

    • ValValVal
      New Member
      • Oct 2008
      • 27

      #3
      No,as far as I see nothing is done.
      I use NetBeans 6.1.

      This code draws picture:

      Code:
      package drawingpieces;
      
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import javax.imageio.*;
      import java.awt.image.*;
      
      public class Example03 extends Frame {
      
          public BufferedImage whitePawn,  whiteKnight,  whiteBishop,  whiteRook,  whiteQueen,  whiteKing;
      
          public static void main(String args[]) {
              new Example03();
          }
      
          public Example03() {
              super("Java 2D Example03");
      
              setSize(500, 500);
              setVisible(true);
      
             /* try {
                  whitePawn = (BufferedImage) ImageIO.read(new File("c:\\pawnwhite.gif"));
              } catch (IOException e) {
                  System.out.println(" Cannot load images of the pieces");
              }*/
      
      
      
              addWindowListener(new WindowAdapter() {
      
                  public void windowClosing(WindowEvent e) {
                      dispose();
                      System.exit(0);
                  }
              });
          }
      
          public void paint(Graphics g) {
              Graphics2D g2d = (Graphics2D) g;
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON);
      
             try {
                  whitePawn = ImageIO.read(new File("c:\\pawnwhite.gif"));
              } catch (Exception e) {
              }
      
              g2d.drawImage(whitePawn,200, 200, this);
      
          }
      }

      And this doesn't draw:


      Code:
      package drawingpieces;
      
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import javax.imageio.*;
      import java.awt.image.*;
      
      public class Example03 extends Frame {
      
          public BufferedImage whitePawn,  whiteKnight,  whiteBishop,  whiteRook,  whiteQueen,  whiteKing;
      
          public static void main(String args[]) {
              new Example03();
          }
      
          public Example03() {
              super("Java 2D Example03");
      
              setSize(500, 500);
              setVisible(true);
      
              try {
                  whitePawn = (BufferedImage) ImageIO.read(new File("c:\\pawnwhite.gif"));
              } catch (IOException e) {
                  System.out.println(" Cannot load images of the pieces");
              }
      
      
      
              addWindowListener(new WindowAdapter() {
      
                  public void windowClosing(WindowEvent e) {
                      dispose();
                      System.exit(0);
                  }
              });
          }
      
          public void paint(Graphics g) {
              Graphics2D g2d = (Graphics2D) g;
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON);
      
            
      
              g2d.drawImage(whitePawn,200, 200, this);
      
          }
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Make that 'setVisible(tru e)' statement the last statement of your constructor
        when everything is properly initialized (so the images aren't null anymore).

        kind regards,

        Jos

        Comment

        • ValValVal
          New Member
          • Oct 2008
          • 27

          #5
          Thanks VERY much. It works now!

          Comment

          Working...