Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: Images not loading (Using code from a book)

  1. #1
    Nanashi's Avatar
    Nanashi is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    3
    Rep Power
    0

    Default 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):

    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));
        }
    
    }
    Thanks in advance,
    /Nanashi

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Images not loading (Using code from a book)

    Try absolute path names for those image files and see what happens.

    kind regards,

    Jos
    Nanashi likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nanashi's Avatar
    Nanashi is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Images not loading (Using code from a book)

    Quote Originally Posted by Nanashi View Post
    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 "http://www.java-forums.org/images/filename" work, when they where in the same directory?

    Thank you so much for your help!
    /Nanashi
    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)

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    kind regards,

    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.
    Nanashi likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Nanashi's Avatar
    Nanashi is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    3
    Rep Power
    0

    Default 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

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    Nanashi likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problems with loading images in a jar
    By Reskaillev in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 08:34 PM
  2. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-22-2011, 11:18 PM
  3. Loading images from an Array
    By pinkette in forum New To Java
    Replies: 5
    Last Post: 04-15-2011, 11:37 AM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •