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 9 of 9

Thread: Image Drawing Problem

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image Drawing Problem

    I have some problem drawin an image icon out on the screen and animate it, the thing is i cant even see the image being displayed so i guess its something wrong.

    Here is a piece of the code

    public class Board extends JPanel implements ActionListener {
     
    	Image Ghost;
    	Timer timer;
    	int x,y;
     
    	public Board(){
     
    		setBackground(Color.BLACK);
     
     
    		ImageIcon ii = new ImageIcon(("ghost.jpeg"));
    		Ghost= ii.getImage();
     
    		setDoubleBuffered(true);
     
    		x=y=10;
    		timer = new Timer(25,this);
    		timer.start();
    	}
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(Ghost,x,y,this);
    		g.dispose();
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent event) {
     
    		x += 1;
            y += 1;
     
            if (y > 240) {
                y = -45;
                x = -45;
            }
            repaint(); 
     
    	}
     
    }

    I put the ghost image in the directory where im doing the test. Is it my path thats wrong do i need some more to find the actual image ?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Image Drawing Problem

    What happens if you do this:

    System.out.println(new File("ghost.jpeg").exists());
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Drawing Problem

    Im getting the false output so its not located... So its the path name then?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Image Drawing Problem

    Quote Originally Posted by Matta View Post
    Im getting the false output so its not located... So its the path name then?
    Sounds like it. You might want to check where the class files (as opposed to the .java files) are located. Or you might want to use the full path.

    Or you could try printing out the full path of the file (the one that doesn't exist) to see where Java thinks it is.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Drawing Problem

    I have actually tried to use the full path of the image, where it is located im still getting the false error also this was known as I started of using this.getClass(),getResources("C:/Images/Ghost.jpg"); Edit: Oh and also if i added it to the where the class is i would use the workspace directory then test1/Images/Ghost.jpg")

    Then I got the nullpointexception which kinda states the same that he image is not found.

    Edit: I have read up a bit the image i have is created in paint which shouldnt make any difference, i just put it inside where my code is now in the src folder. That is suppose to work atleast what i have read but it doesnt! This is really frustrating...
    Last edited by Matta; June 9th, 2011 at 11:24 AM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Image Drawing Problem

    Your code works for me, if I create a ghost.jpeg image and place it in the same folder as my .java and .class files.

    Are you using packages? Are you sure you have the filename right (jpg vs jpeg, capitalization)?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Drawing Problem

    Yeah im pretty sure i have the right jpg stuff atleast i looked at my image properties and it said jpg so thats what im using. But thats the basics.
    Im not using any packages no. I have a folder called workspace where all my src files are located this is the right one, right? Ill get home and try it on my main computer which maybe is able to take it.

    But i have the eclipse or the workspace in different directories, or my directory for workspace is located in my user and the eclipse is folder is located on the mainboard of the computer. Aaah .. pain.. so you get it to work then its only my computer who screws everything up! i knew it...

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Image Drawing Problem

    It sounds like eclipse is putting the .class files in a different folder from your .java files, which is pretty normal. Oftentimes, people will store the images in a separate folder, then do something like "../images/image.jpg" to go up a directory, then into the images folder, then find the image. You could also copy your image file to the bin folder (or wherever your .class files are going).

    Don't just look at the image file properties- a jpg file can have an extension of .jpg or .jpeg, or .JPG for that matter. Look at the actual file name.

    I recommend ditching eclipse for a second- move the .java and the .jpg file to the desktop, then compile and run the file from the command line.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Image Drawing Problem

    Incidentally, it's not a good idea to call g.dispose() at the end of the paint method. Graphics resources are automatically released after the paint method, and disposing of them yourself can bite you if you decide to subclass and override that method to extend it. Only dispose of graphics objects that you have created or extracted from a component yourself.

Similar Threads

  1. Incremental image drawing in Java
    By ea25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2011, 08:58 AM
  2. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 04:38 PM
  3. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 04:07 PM
  4. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  5. GUI problem with image in java
    By Kok in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2009, 05:17 AM