Results 1 to 9 of 9
  1. #1
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Java Gui Issue Displaying ImageIcon

    Hello Everyone,

    I am just sort of testing on how to display a jpg image on a through a Jlabel. I am sure it is a simple error but I am still new to GUI's.
    line 31 to 35 you can see the ImageIcon and file wrapped in the JLabel. Am I doing this correctly? I verified its in the correct location and file name, but usually fails (i thought) when it is not correct.

    Thank you

    Sun

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class ScoreBoard 
    {
    	private JFrame window;
    	private JPanel panel;
    	private JLabel whitelabel, blacklabel;
    	private int whitescore, blackscore;
    	private int width, height;
    	
    	//these will be used to add images to west and east of panels border layout
    	private JLabel whiterep, blackrep;
    	private ImageIcon wimage;
    	ScoreBoard(String name)
    	{
    		 width = 600;
    		 height = 400;
    	//set JFrame parameters
    		 window = new JFrame(name);
    		 window.setSize(width, height);
    		 window.setResizable(false);
    		 window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	//ImageIcon
    		 wimage = new ImageIcon("WRook.jpg");
    	//JLabel
    		 whitelabel = new JLabel("WhiteScore :" + whitescore);
    		 blacklabel = new JLabel("BlackScore :  " + blackscore);
    		 whiterep = new JLabel(wimage);
    	//JPanel	 
    		 panel = new JPanel(new BorderLayout());
    		 panel.setBackground(Color.red);
    		 panel.add(whitelabel, BorderLayout.SOUTH);
    		 panel.add(blacklabel, BorderLayout.NORTH);
    		 panel.add(whiterep, BorderLayout.EAST);
    		 
    		
    		 
    		 window.add(panel);
    		 
    		 window.setVisible(true);
    	}
    	public static void main(String args[])
    	{
    		ScoreBoard mygame = new ScoreBoard("Chess Game");
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Gui Issue Displaying ImageIcon

    The code works for me: I get a red background with an image on the center right.

    How are you executing the code?
    If you use the java command in a folder that contains the class file and the WRook.jpg file it should work.
    Last edited by Norm; 12-02-2014 at 03:41 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Java Gui Issue Displaying ImageIcon

    I am executing the file with my Eclipse IDE. I just executed it in my windows command console now. And it works, I am not sure why it wouldn't pop up in window that was created through my eclipse ide. That is weird.

    Currently using the bordered layout, is there a way to make the image 'more center' or fit to scale in its box? just a though thats all

    thanks
    Last edited by SunDontShine; 12-02-2014 at 07:49 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Gui Issue Displaying ImageIcon

    The IDE can change where the program looks for files.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java Gui Issue Displaying ImageIcon

    Indeed. By default Eclipse will use the project directory as the working directory. You can change it in the run configuration. (right click -> run as -> run configuration)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java Gui Issue Displaying ImageIcon

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Java Gui Issue Displaying ImageIcon

    thanks for the advice gimbal2!
    That was really helpful and i will remember that

    Thanks Darryl ill go ahead and check that out!

  8. #8
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Java Gui Issue Displaying ImageIcon

    Hello Guys,

    I have one more question, So in my chess game i have a JPanel adding a JLabel of an image... To represent all of these different chess pieces on different colored squares... for example on my game Blue and Grey. Do i Need to paint the backgrounds of each "Image" to match a Blue or Grey color?

    And then change them out if that piece lands on a color of that type? I am a bit new to this so I am just trying to figure that part out. Thanks Gents

    Regards,

    SunDontShine

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java Gui Issue Displaying ImageIcon

    I would use images with a transparent background.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 03-09-2011, 02:10 PM
  2. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 10:10 PM
  3. Replies: 33
    Last Post: 09-06-2010, 10:49 PM
  4. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 08:48 PM
  5. Replies: 4
    Last Post: 03-21-2009, 11:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •