Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: won't load png

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default won't load png

    The following code does not load the png image can someone tell me what im doing wrong the syntax is correct but i get a uncaught fetching error. It cant find the png image. Any suggestions would be appreciated. thanks ahead of time.



    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.net.*;
    import java.awt.geom.*;

    public class DrawImage extends JFrame
    {
    private Image image;

    public static void main(String[] args)
    {
    new DrawImage();
    }

    public DrawImage()
    {
    super("DrawImage");
    setSize(600,600);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Toolkit tk = Toolkit.getDefaultToolkit();
    image=tk.getImage(getURL("castle.png"));
    }

    private URL getURL(String filename)
    {
    URL url=null;
    try
    {
    url=this.getClass().getResource(filename);
    }
    catch(Exception e)
    {

    }
    return url;
    }
    public void paint(Graphics g)
    {
    Graphics2D g2d = (Graphics2D)g;

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0,0,getSize().width,getSize().height) ;

    g2d.drawImage(image,0,40,this);
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: won't load png

    a) Post the error

    b)....
    try
    {
    url=this.getClass().getResource(filename);
    }
    catch(Exception e)
    {
     
    }

    Do not let exceptions fall through, at the least print out the stack trace.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: won't load png

    tried it stil doesnt draw the image

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: won't load png

    thanks thought

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: won't load png

    Quote Originally Posted by NewAtJava View Post
    tried it stil doesnt draw the image
    That tells us nothing about the problem, especially when an exception could be thrown and you are ignoring it.

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: won't load png

    it cant find the url that is what im trying to say

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: won't load png

    uncaught fetching image also

Similar Threads

  1. Java Applet Will Not Load.
    By PaulMoretti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 05:42 AM
  2. Image Load
    By shadihrr in forum Java Theory & Questions
    Replies: 4
    Last Post: August 26th, 2010, 06:56 AM
  3. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM
  4. [SOLVED] I cant load the icon!!!!
    By chronoz13 in forum AWT / Java Swing
    Replies: 12
    Last Post: January 22nd, 2010, 03:52 AM
  5. Arraylist Save and Load
    By frankycool in forum Collections and Generics
    Replies: 1
    Last Post: November 14th, 2009, 06:48 AM