Results 1 to 5 of 5
  1. #1
    kotorez is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Cant display an image for some reason.

    I am using Netbeans.

    For some reason I cant display an image. Can you please take a look at my code?

    Java Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.net.URL;
    
    public class ImageLoadingTutorial extends Applet{
        
        private Image spiral = null;
        
        public void paint(Graphics g){
            this.setSize(640, 480);
            if (spiral == null){
                spiral = getImage("C:/Games/hello.png");
            }
            Graphics2D g2 = (Graphics2D) g;
            g2.drawImage(spiral, 25, 50, 120, 120, this);
        }
        
        public Image getImage(String path){
            Image tempImage = null;
            try{
                URL imageURL = ImageLoadingTutorial.class.getResource(path);
                tempImage = Toolkit.getDefaultToolkit().getImage(imageURL);
            }
            catch (Exception e){
                System.out.println("An error occured - " + e.getMessage());
            }
            return tempImage;
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Cant display an image for some reason.

    How do you execute the code? Are there any error messages?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kotorez is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Cant display an image for some reason.

    Quote Originally Posted by Norm View Post
    How do you execute the code? Are there any error messages?
    Well I tried to "Clean and build project" but usually I just right click "Run file".

    No, there are no error messages. The output I get is "An error occured - null". If I comment out try/catch I would get "java.lang.NullPointerException".

    The only problem is that it cant find the file. I tried putting image file anywhere in the in the project folder, tried changing file path from "c:/folder/file.png" to "c:\\folder\\file.png" and nothing seems to work.

  4. #4
    kotorez is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Cant display an image for some reason.

    Never mind, I figured it out. What I was doing is just putting an image inside the project folder but what I had to do was to put it in a folder with classes.

  5. #5
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: Cant display an image for some reason.

    Try to do it:

    Java Code:
    g2.drawImage(getHelloImage(), 25, 50, 120, 120, this);
    
    public Image getHelloImage(){
             ImageIcon i = new ImageIcon("C:/Games/hello.png");
             return i.getImage();
    }
    I think it will work...

Similar Threads

  1. Display image
    By asai in forum New To Java
    Replies: 16
    Last Post: 04-03-2012, 01:15 PM
  2. Display Image (png)
    By vimalaranjan in forum Java Gaming
    Replies: 2
    Last Post: 05-04-2011, 12:23 AM
  3. to display image
    By asmitarnd in forum AWT / Swing
    Replies: 6
    Last Post: 04-07-2011, 11:17 AM
  4. image display
    By asmitarnd in forum AWT / Swing
    Replies: 3
    Last Post: 04-07-2011, 11:17 AM
  5. How to display image ?
    By Birkoff in forum AWT / Swing
    Replies: 7
    Last Post: 06-09-2008, 07:58 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
  •