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

Thread: Help with ImageIcon

  1. #1

    Unhappy [SOLVED] Help with ImageIcon

    My background for my game isn't displaying at all. I use a mac, do I have to do something different? Heres my Panel class:
    package net.nivangerow.umadbro;
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class Panel extends JPanel implements ActionListener{
    	public Player p;
    	Image img;
    	Timer time;
    	public Panel()
    	{
    		p = new Player();
    		addKeyListener(new AL());
    		setFocusable(true);
    		ImageIcon i = new ImageIcon("/background.png");
    		img = i.getImage();
    		time = new Timer(5, this);
    		time.start();]
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		p.move();
    		repaint();
    	}
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.drawImage(img, 0, 0, null);
    		g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
    	}
    	private class AL extends KeyAdapter
    	{
    		public void keyReleased(KeyEvent e)
    		{
    			p.keyReleased(e);
    		}
    		public void keyPressed(KeyEvent e)
    		{
    			p.keyPressed(e);
    		}
    	}
    }
    At the moment my images are in the same folder as the .java files, and I use eclipse.
    Last edited by nivangerow; August 30th, 2011 at 10:44 AM.


  2. #2

    Default Re: Help with ImageIcon

    Help please, im frustrated and I want it to work! Whats wrong with it?

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    Because you bumped your thread, it will now show that this thread has received replies, and most people will assume you're already getting help so they won't bother looking at it. Plus it's only been a few minutes- a little patience goes a long way!

    Can you access that file at all? Print out the full path- is it what you expected?
    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!

  4. #4

    Default Re: Help with ImageIcon

    Sorry, im a newb on these forums. Cant I access the file if it is in the same directory as my java files (i use eclipse, i put the images in my workspace)? Like in HTML and CSS...

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    I'm not sure, can you? What happened when you tried to access it and print out the full path? What happens when you use the full path for the ImageIcon?
    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!

  6. #6

    Default Re: Help with ImageIcon

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure, can you? What happened when you tried to access it and print out the full path? What happens when you use the full path for the ImageIcon?
    I just tried it, still doesnt display...

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    Quote Originally Posted by nivangerow View Post
    I just tried it, still doesnt display...
    You're skipping over most of my questions. Saying that it doesn't display doesn't really help- when you create a File at that location, what is the full path of that File? What happens if you get rid of the leading /? Are you sure you're using the correct path separator character for your system?
    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!

  8. #8

    Default Re: Help with ImageIcon

    Quote Originally Posted by KevinWorkman View Post
    You're skipping over most of my questions. Saying that it doesn't display doesn't really help- when you create a File at that location, what is the full path of that File? What happens if you get rid of the leading /? Are you sure you're using the correct path separator character for your system?
    Here is the full filepath and it still doesnt display:
    /Users/nivangerow/Documents/workspace/U MAD BRO/src/net/nivangerow/umadbro/background.png

    Also there is an error if i use any escape characters. Nothing changes when I get rid of the leading /. I am sure that I use the correct path separator character for my system.

    Here are my specs:
    MacBook 13-inch, Mid 2010, Processor: 2.4 GHz Intel Core 2 Duo, Memory: 2 GB 1067 MHz DDR3, Graphics: NVIDIA GeForce 320M 256 MB, Software (OS): Mac OS X Lion 10.7.1 (11B26)

  9. #9

    Default Re: Help with ImageIcon

    Are you still there?

  10. #10

    Default Re: Help with ImageIcon

    Help! I answered all your questions, can you help me now?

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    Again, a little patience goes a long way. That's definitely one way to make sure people don't want to help you. I'm not going to help you if you do it again- there are hundreds of posts here, each with its own urgent user waiting on a response, and their time is just as important as yours. And the volunteers helping here do it for free, most of the time on their lunch break or between classes. You've been told about it twice now- you won't be told a third time.

    Have you tried refreshing your eclipse project?
    Last edited by KevinWorkman; August 30th, 2011 at 10:20 AM.
    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!

  12. #12

    Default Re: Help with ImageIcon

    K, ive just refreshed it. Still no success.

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    What if you take eclipse out of the equation entirely?

    Create a class that reads a file next to it. Go from there. (that's what I was hinting you do in my first response, btw, Mr. Impatient Pants)
    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!

  14. #14

    Default Re: Help with ImageIcon

    I have tried this: (its slightly edited)
    package net.nivangerow.umadbro;
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class Panel extends JPanel implements ActionListener{
    	public Player p;
    	Image img;
    	Timer time;
    	public Panel()
    	{
    		p = new Player();
    		addKeyListener(new AL());
    		setFocusable(true);
    		ImageIcon i = new ImageIcon(getClass().getResource("background.png"));
    		System.out.println(getClass().getResource("background.png"));
    		img = i.getImage();
    		time = new Timer(5, this);
    		time.start();
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		p.move();
    		repaint();
    	}
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(img, 0, 0, null);
    		g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
    	}
    	private class AL extends KeyAdapter
    	{
    		public void keyReleased(KeyEvent e)
    		{
    			p.keyReleased(e);
    		}
    		public void keyPressed(KeyEvent e)
    		{
    			p.keyPressed(e);
    		}
    	}
    }
    Still no success....

  15. #15

    Default Re: Help with ImageIcon

    Quote Originally Posted by KevinWorkman View Post
    What if you take eclipse out of the equation entirely?

    Create a class that reads a file next to it. Go from there. (that's what I was hinting you do in my first response, btw, Mr. Impatient Pants)
    Shouldnt it work with eclipse though?

  16. #16

    Default Re: Help with ImageIcon

    I have just experimented with backgrounds (Color.BLACK), etc..
    And I have set a red background to the JPanel and a green background to the frame itself. And I see green and i should actually see red. SO is my panel not being drawn or something?

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    Quote Originally Posted by nivangerow View Post
    Shouldnt it work with eclipse though?
    I've had problems with eclipse not keeping up with the state of the file system. And either way, it's good to eliminate possibilities.

    That code doesn't run for me because I don't have the Player class (take it out if it's not pertinent to the problem), and that code doesn't have a main method (add your GUI to a JFrame and show it). In other words, provide an SSCCE that demonstrates the problem, and we'll go from there.
    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!

  18. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with ImageIcon

    Quote Originally Posted by nivangerow View Post
    I have just experimented with backgrounds (Color.BLACK), etc..
    And I have set a red background to the JPanel and a green background to the frame itself. And I see green and i should actually see red. SO is my panel not being drawn or something?
    Yep, sounds like a sizing problem. What layout are you using?
    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!

  19. The Following User Says Thank You to KevinWorkman For This Useful Post:

    nivangerow (August 30th, 2011)

  20. #19

    Default Re: Help with ImageIcon

    That did it for me, they are being drawn!

Similar Threads

  1. ImageIcon Erratically Loading URL Image
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 04:05 PM
  2. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM

Tags for this Thread