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

Thread: setting image to panel

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default setting image to panel

    Hello all, I am making a 2D Scroller game, and I know how to accomplish it, however, having trouble setting an image to the background, no amount of videos or tutorials is helping. The image does not appear. If i can just figure this out I am good. Here is my code:
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
     
    public class BackgroundPanel extends JPanel
    { 
    	private BufferedImage image;
    	public BackgroundPanel()
    	{
    		try
    		{
    			image = ImageIO.read(new File("ges.jpg"));
    			JLabel picLabel = new JLabel(new ImageIcon(image));
    			setSize(300,300);
    			setVisible(true);
    		}
    		catch (IOException e){}
    	}
     
     
    }

    import javax.swing.JFrame;
     
    public class Frame
    {
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Bobby");
    		BackgroundPanel panel = new BackgroundPanel();
    		frame.add(panel);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500,400);
    	}
    }
    thanks for any help, it's bothering me


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,156
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: setting image to panel

    Don't use empty catch blocks like that. Is there an Exception you're hiding from yourself?
    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
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: setting image to panel

    no, just the program wanted a try and catch

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,156
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: setting image to panel

    That's fine, but you should never have an empty catch. At least put an e.printStackTrace() inside the catch block.

    But anyway, when are you actually adding the JLabel to the JPanel?
    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
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: setting image to panel

    ah okay, will do that, thanks for that tip, and i am adding the Label when in the Frame class I say
    BackgroundPanel panel = new BackgroundPanel();
    frame.add(panel);
    should I make a panel inside the BackgroundPanel class and add the label to that? not too good with gui stuff xD

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: setting image to panel

    Quote Originally Posted by godlynom View Post
    should I make a panel inside the BackgroundPanel class and add the label to that? not too good with gui stuff xD
    If it makes sense to do so, but only you and the code design that's in your head really can know for sure about this.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,156
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: setting image to panel

    You've posted when you add the BackgroundPanel to the JFrame. But what are you putting the image on? When are you adding that component to the BackgroundPanel?
    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
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: setting image to panel

    image = ImageIO.read(new File("ges.jpg"));
    			JLabel picLabel = new JLabel(new ImageIcon(image));
    this adds the image to the label, oh, should i do a
     add(picLabel);
    after that?

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: setting image to panel

    Quote Originally Posted by godlynom View Post
    image = ImageIO.read(new File("ges.jpg"));
    			JLabel picLabel = new JLabel(new ImageIcon(image));
    this adds the image to the label, oh, should i do a
     add(picLabel);
    after that?
    Did you try it? Did it work?

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: setting image to panel

    sorry was not near computer for while, and no it did not :/ still have the white frame screen

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,156
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: setting image to panel

    Quote Originally Posted by godlynom View Post
    sorry was not near computer for while, and no it did not :/ still have the white frame screen
    Then you need to go through a process of elimination. Are you loading the file properly? Are you sure? Use a debugger or a print statement to test your assumptions. Can you add a text-only JLabel to a JPanel? What about one with no text (try giving it an obvious background color or border to make it easier to see)? Which part of this process isn't working?
    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
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: setting image to panel

    so now that I am using the computer at home with eclipse, I see the error "Can't read input file" should i try making a folder for the image or something else, cuz just putting in the image did not work

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,156
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: setting image to panel

    Quote Originally Posted by godlynom View Post
    so now that I am using the computer at home with eclipse, I see the error "Can't read input file" should i try making a folder for the image or something else, cuz just putting in the image did not work
    You need to figure out where it thinks it's loading the file from. You can use the File class to get the absolute path.
    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!

Similar Threads

  1. Moving a shape across a panel
    By havinFun in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 17th, 2012, 05:56 PM
  2. Cant get background image to take up whole panel
    By Lanuk in forum AWT / Java Swing
    Replies: 1
    Last Post: December 13th, 2011, 07:26 AM
  3. Adding a Panel from a different class
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 25th, 2011, 07:28 AM
  4. Panel Problems?
    By Dellick17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 01:31 PM
  5. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM

Tags for this Thread