Results 1 to 8 of 8

Thread: [RESOLVED] Background Image JLabel

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Resolved [RESOLVED] Background Image JLabel

    Hello
    I want to create a JPanel with a background image, and on that background image I want to arrange my buttons and text feilds etc in a border layout. Is this possibe? Having real trouble accomplishing this. Any assistence will be appreciated. I am using Java 2 at the moment. Thanks in advance

    Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.BorderLayout;
     
    public class GUI2 extends JFrame
    {
    	ImageIcon icon;
    	Image image;
     
    	public GUI2()
    	{
    		icon = new ImageIcon("background.jpg");
    		JPanel panel = new JPanel()
    		{			
    			protected void paintComponent(Graphics g)
    			{
    				//  Dispaly image at full size
    				g.drawImage(icon.getImage(), 0, 0, null); 	
    				super.paintComponent(g);
    			}
    		};
    		panel.setOpaque( false );
    		panel.setPreferredSize( new Dimension(400, 400) );	
    		getContentPane().add( panel ); 
     		panel.setLayout(new BorderLayout()); 		
     		JPanel southPanel = new JPanel();
     		JButton button = new JButton( "Hello" );
     		southPanel.setOpaque(true);
     		button.setOpaque(true);
     		southPanel.add(button); 		
     		add(southPanel, BorderLayout.NORTH); 	
     	//	southPanel.add(button);
     	//	southPanel.add(b);		
    	} 
    	public static void main(String [] args)
    	{
    		GUI2 frame = new GUI2();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(450,  250);
    		frame.setLocationRelativeTo( null );
    		frame.setVisible(true);
    	}
    }
    Last edited by 01010011; Nov 15th, 2008 at 08:28 AM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Background Image JLabel

    Ok
    #1 There is no Java2 because there is no Java1 (Not since 1997, the 2 was dropped officially last year)
    #2 You're code is working fine and is doing everything you said you wanted to do.
    #3 What's with the whitespace!! so many empty lines
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Re: Background Image JLabel

    Quote Originally Posted by ComputerJy
    Ok
    #1 There is no Java2 because there is no Java1 (Not since 1997, the 2 was dropped officially last year)
    #2 You're code is working fine and is doing everything you said you wanted to do.
    #3 What's with the whitespace!! so many empty lines
    Thank you for your reply ComputerJy.

    My problem is that I am trying to get my JPanel to show the background image instead of the default light blue background. In other words, when I add a panel to the image, a block around the panel shows the default light blue background, instead of the background image.

    Maybe I am using the setOpaque(boolean); wrong
    Last edited by 01010011; Nov 14th, 2008 at 07:56 PM.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Re: Background Image JLabel

    Quote Originally Posted by ComputerJy
    #3 What's with the whitespace!! so many empty lines
    Yeah, sorry about the excessive white space. Thanks!

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Background Image JLabel

    Here's what I'm getting
    What's wrong with it? cause I don't see anything blue except for the background
    Attached Images Attached Images  
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Re: Background Image JLabel

    When I setLayout over the background image to BoxLayout, or GridLayout, parts of the background picture gets cut out to reveal the default color instead of the picture. I actually want the background picture to remain even when I change the border layout of the main panel. Using your picture as an example, I don't want the default blue background, I want the picture of the sky to fill the panel regardless of where I place buttons and regardless of which Border layout I use.
    Last edited by 01010011; Nov 15th, 2008 at 08:26 AM.

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Background Image JLabel

    I still don't have a clear idea of what you want. But if what you want is something like the attached screenshot. Then add the southPanel to the JPanel panel instead of adding it to the JFrame itself, and set it (the southPanel) to transparent instead of opaque
    Attached Images Attached Images  
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Re: Background Image JLabel

    Quote Originally Posted by ComputerJy
    I still don't have a clear idea of what you want. But if what you want is something like the attached screenshot. Then add the southPanel to the JPanel panel instead of adding it to the JFrame itself, and set it (the southPanel) to transparent instead of opaque
    Right, that worked, your second picture shows exactly what I want -- the button with the sky background. Thank you very much ComputerJy!

Posting Permissions

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



Click Here to Expand Forum to Full Width