Results 1 to 6 of 6
- 11-15-2011, 11:24 AM #1
Images not loading (Using code from a book)
Hi!
I'm sort of new to Java. I usually code in PHP, C++ and ActionScript.
My problem is this,
I'm trying to follow a book named "Developing games in Java" written by David Brackeen. In chapter 2, there is an example of how to add images to a full screen application. What he does is that he adds a JPG background image, and then 4 PNG images. I tried to do it like I always do, by writing the code by myself looking at the book. It didn't work. I searched for errors in the code, changed some things, tried different things, but it didn't work. Then I tried to use his own code, that I downloaded from his website. That didn't work either.. I tried to find another way to add an image, and now I've been searching all morning.. I can't seem to figure out a way to implement images in any other way into this class that's written in this book.. My Java programming level isn't just high enough.
I've tried to understand the code written in the book as much as possible, and I think that I understand most of it. I just can't understand why the images aren't showing up. I'd appreciate if someone could help me on the right track. Here's the code for the file where the images load, downloaded from the authors website (I've modified the brackets and some spaces so that it becomes easier to read):
Thanks in advance,Java Code:import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; public class ImageTest extends JFrame { public static void main(String[] args) { DisplayMode displayMode; if (args.length == 3) { displayMode = new DisplayMode( Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), DisplayMode.REFRESH_RATE_UNKNOWN); } else { displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); } ImageTest test = new ImageTest(); test.run(displayMode); } private static final int FONT_SIZE = 24; private static final long DEMO_TIME = 10000; private SimpleScreenManager screen; private Image bgImage; private Image opaqueImage; private Image transparentImage; private Image translucentImage; private Image antiAliasedImage; private boolean imagesLoaded; public void run(DisplayMode displayMode) { setBackground(Color.blue); setForeground(Color.white); setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE)); imagesLoaded = false; screen = new SimpleScreenManager(); try { screen.setFullScreen(displayMode, this); loadImages(); try { Thread.sleep(DEMO_TIME); } catch (InterruptedException ex){} } finally { screen.restoreScreen(); } } public void loadImages() { bgImage = loadImage("images/background.jpg"); opaqueImage = loadImage("images/opaque.png"); transparentImage = loadImage("images/transparent.png"); translucentImage = loadImage("images/translucent.png"); antiAliasedImage = loadImage("images/antialiased.png"); imagesLoaded = true; // signal to AWT to repaint this window repaint(); } private Image loadImage(String fileName) { return new ImageIcon(fileName).getImage(); } public void paint(Graphics g) { // set text anti-aliasing if (g instanceof Graphics2D) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } // draw images if (imagesLoaded) { g.drawImage(bgImage, 0, 0, null); drawImage(g, opaqueImage, 0, 0, "Opaque"); drawImage(g, transparentImage, 320, 0, "Transparent"); drawImage(g, translucentImage, 0, 300, "Translucent"); drawImage(g, antiAliasedImage, 320, 300, "Translucent (Anti-Aliased)"); } else { g.drawString("Loading Images...", 5, FONT_SIZE); } } public void drawImage(Graphics g, Image image, int x, int y, String caption) { g.drawImage(image, x, y, null); g.drawString(caption, x + 5, y + FONT_SIZE + image.getHeight(null)); } }
/Nanashi
- 11-15-2011, 01:14 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Images not loading (Using code from a book)
Try absolute path names for those image files and see what happens.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-15-2011, 01:20 PM #3
Re: Images not loading (Using code from a book)
Thank you very much! It works when I try absolute paths!
I can't explain how I feel in words after finding such an easy solution after 5 hours...
I'd like to know though, why didn't the "images/filename" work, when they where in the same directory?
Thank you so much for your help!
/Nanashi
- 11-15-2011, 01:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Images not loading (Using code from a book)
The directory where your program runs is not the directory you think it is; add the following line to see where your program runs (somewhere near the top of your main( ... ) method will do)
kind regards,Java Code:System.out.println(System.getProperty("user.dir"));
Jos
ps. the forum software mutilates your quoted text (see the "filename"); don't pay any attention to it ...Last edited by JosAH; 11-15-2011 at 01:34 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-15-2011, 01:39 PM #5
Re: Images not loading (Using code from a book)
Thank you so much Jos.
It appears the program was running from the project directory. Usually, I put the images in the bin directory since that's where they used to work.
When I put the images in the directory above the bin directory, which is the project directory, I didn't have to use absolute paths!
Thank you so much, finally I can continue with the book, :D
I can't explain how happy I am now, :'3
Once again, thank you!
/Nanashi
- 11-15-2011, 02:33 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Images not loading (Using code from a book)
You're welcome of course; normally those pictures are put in a 'jar' file together with the .class files; a .jar file is a little filing system (so to speak) and you can get your pictures as 'resources' with a path relative to the class that tries to load them or absolute, where the .jar file itself can be considered the 'root' of the 'filing system'.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Problems with loading images in a jar
By Reskaillev in forum New To JavaReplies: 2Last Post: 10-10-2011, 08:34 PM -
Loading Images
By TacoManStan in forum Java GamingReplies: 9Last Post: 09-22-2011, 11:18 PM -
Loading images from an Array
By pinkette in forum New To JavaReplies: 5Last Post: 04-15-2011, 11:37 AM -
Loading Images - Imp
By Thulasiraman in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 09:33 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks