473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.     whitePawn = ImageIO.read(new File("c:\\finalbig\\pawnwhite.gif"));
  3. } catch (IOException e) {System.out.println(" Cannot load images");}
  4.  
in paint:
Expand|Select|Wrap|Line Numbers
  1.   g2d.drawImage(whitePawn, 0,30, this);
================

and if I put loading instruction in paint before drawImage() it works ok.
================
all in paint:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.     whitePawn = ImageIO.read(new File("c:\\finalbig\\pawnwhite.gif"));
  3. } catch (IOException e) {System.out.println(" Cannot load images");}
  4.  
  5.   g2d.drawImage(whitePawn, 0,30, this);
  6.  
===============

Variable whitePawn is a field.
If needed I can post more code.
Thanks in advance for answering my stupid question.
Nov 19 '08 #1
4 3326
Nepomuk
3,112 Expert 2GB
Is anything that effects any of the variables used done in between?

Greetings,
Nepomuk
Nov 20 '08 #2
No,as far as I see nothing is done.
I use NetBeans 6.1.

This code draws picture:

Expand|Select|Wrap|Line Numbers
  1. package drawingpieces;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.imageio.*;
  7. import java.awt.image.*;
  8.  
  9. public class Example03 extends Frame {
  10.  
  11.     public BufferedImage whitePawn,  whiteKnight,  whiteBishop,  whiteRook,  whiteQueen,  whiteKing;
  12.  
  13.     public static void main(String args[]) {
  14.         new Example03();
  15.     }
  16.  
  17.     public Example03() {
  18.         super("Java 2D Example03");
  19.  
  20.         setSize(500, 500);
  21.         setVisible(true);
  22.  
  23.        /* try {
  24.             whitePawn = (BufferedImage) ImageIO.read(new File("c:\\pawnwhite.gif"));
  25.         } catch (IOException e) {
  26.             System.out.println(" Cannot load images of the pieces");
  27.         }*/
  28.  
  29.  
  30.  
  31.         addWindowListener(new WindowAdapter() {
  32.  
  33.             public void windowClosing(WindowEvent e) {
  34.                 dispose();
  35.                 System.exit(0);
  36.             }
  37.         });
  38.     }
  39.  
  40.     public void paint(Graphics g) {
  41.         Graphics2D g2d = (Graphics2D) g;
  42.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  43.                 RenderingHints.VALUE_ANTIALIAS_ON);
  44.  
  45.        try {
  46.             whitePawn = ImageIO.read(new File("c:\\pawnwhite.gif"));
  47.         } catch (Exception e) {
  48.         }
  49.  
  50.         g2d.drawImage(whitePawn,200, 200, this);
  51.  
  52.     }
  53. }

And this doesn't draw:


Expand|Select|Wrap|Line Numbers
  1. package drawingpieces;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.imageio.*;
  7. import java.awt.image.*;
  8.  
  9. public class Example03 extends Frame {
  10.  
  11.     public BufferedImage whitePawn,  whiteKnight,  whiteBishop,  whiteRook,  whiteQueen,  whiteKing;
  12.  
  13.     public static void main(String args[]) {
  14.         new Example03();
  15.     }
  16.  
  17.     public Example03() {
  18.         super("Java 2D Example03");
  19.  
  20.         setSize(500, 500);
  21.         setVisible(true);
  22.  
  23.         try {
  24.             whitePawn = (BufferedImage) ImageIO.read(new File("c:\\pawnwhite.gif"));
  25.         } catch (IOException e) {
  26.             System.out.println(" Cannot load images of the pieces");
  27.         }
  28.  
  29.  
  30.  
  31.         addWindowListener(new WindowAdapter() {
  32.  
  33.             public void windowClosing(WindowEvent e) {
  34.                 dispose();
  35.                 System.exit(0);
  36.             }
  37.         });
  38.     }
  39.  
  40.     public void paint(Graphics g) {
  41.         Graphics2D g2d = (Graphics2D) g;
  42.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  43.                 RenderingHints.VALUE_ANTIALIAS_ON);
  44.  
  45.  
  46.  
  47.         g2d.drawImage(whitePawn,200, 200, this);
  48.  
  49.     }
  50. }
Nov 20 '08 #3
JosAH
11,448 Expert 8TB
Make that 'setVisible(true)' statement the last statement of your constructor
when everything is properly initialized (so the images aren't null anymore).

kind regards,

Jos
Nov 20 '08 #4
Thanks VERY much. It works now!
Nov 20 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Brian Genisio | last post by:
Hey all, I am trying to figure out what the constructors for the Image object are. I have figured out that in IE and Mozilla, the Image object, and the HTMLImageElement are the same, from a...
2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
4
by: Ed Sutton | last post by:
I have been searching for how to get an icon resource and load it into an ImageList. I added an *.ico file to my project and set the files "Build Action" to "Embedded Resource". Is there any easy...
68
by: Nak | last post by:
Hi there, This might sound like a silly question but, technically could an image object be used for any image format? For example if I were to make plugins for my application, could they be...
1
by: SonyMan | last post by:
HI I have a VB.NET application in which my Image (from a picturebox) is opened by the user in another application like MSPaint or Photoshop or whatever he chooses. The image does open there.But...
3
by: Andres Corrada-Emmanuel | last post by:
Hello, I have installed PIL 1.1.5 on Windows with Python 2.4. I'm unable to open .tiff images that I can open and view using Windows Explorer. In other words, this simple test fails: import...
2
by: Marcolino | last post by:
Hy Guys, I have a problem loading JPG in a Picture Box. I need to load a jpg files into a PB and then unlock the original JPG. I'm using following code: Dim picture1 As PictureBox Dim...
4
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.