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

Thread: Background image trouble on GUI.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Background image trouble on GUI.

    Hey guys, I have a little trouble concerning this GUI below. What I tried to do is to add a background to a JFrame by creating a panel whom I set as the "background" and then adding the other panels to this one. The problem is that once the program is run It does not display all the buttons until you scroll over them and it does not display the JLabels at all. Do you perhaps have a suggestion about what i could do to solve this problem?

    here is the code.
         import java.awt.*;
       import javax.swing.*;
       import java.io.*;
     
       public class HomeScreen  extends JPanel
       {
       //public variables
          JFrame home;
          ImageIcon icon;    
     
          public HomeScreen()
          throws IOException
          {
             initframe();
             components();		
          }
     
          void initframe()
          throws IOException
          {
             home = new JFrame();
             home.setSize(600,400);		
             home.setTitle("Welcome to ENG-IT-AF");
             home.setVisible(true);
             home.setResizable(true);
             home.setLocationRelativeTo(null);
             home.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
     
          }
     
          void components()
          throws IOException
          {
     
     
          	//panels & setting the background
    			JPanel backg = new JPanel();
    			LayoutManager layout = new OverlayLayout(backg);
    			backg.setLayout(layout);
    			home.add(backg);
     
    			JPanel backimage = new JPanel();
             ImageIcon image = new ImageIcon("ENTER AN IMAGE HERE");
             backimage.add(new JLabel(image));
    			backg.add(backimage);
     
             JPanel pcomp = new JPanel();
             pcomp.setLayout(new GridLayout(2,2,5,5));
    			home.add(backg);
    			backg.add(pcomp);
     
          	//images and rescaling
             ImageIcon icon1 = new ImageIcon("ENTER ANY IMAGE HERE");
             Image image1 = icon1.getImage().getScaledInstance(200, 100, Image.SCALE_SMOOTH);
             icon1.setImage(image1);
     
             ImageIcon icon2 = new ImageIcon("ENTER ANY IMAGE HERE");
             Image image2 = icon2.getImage().getScaledInstance(200, 100, Image.SCALE_SMOOTH);
             icon2.setImage(image2);
     
          	// labels 
             JLabel lab1 = new JLabel("                              ARE YOU SOUTH AFRICAN?");
             lab1.setBounds(0,0,200,40);
             pcomp.add(lab1);
     
             JLabel lab2 = new JLabel("                              ARE YOU ENGLISH?");
             lab2.setBounds(0,0,200,40);
             pcomp.add(lab2);
          	//buttons
             JButton b1 = new JButton(icon1);
             b1.setBounds(0,0,500,300);
             pcomp.add(b1);
     
             JButton b2 = new JButton(icon2);
             b2.setBounds(0,0,300,300);
             pcomp.add(b2);
          }
          public static void main(String[] args)
          throws IOException
          {
             new HomeScreen();
          }
       }


    any help would be greatly appreciated thanks.
    Last edited by Pydra; July 7th, 2011 at 08:44 AM.


  2. #2
    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: Background image trouble on GUI.

    Instead of doing it by adding your background as a component behind the other components (which is messy), why don't you just override paintComponent and paint the background image in 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!

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

    Default Re: Background image trouble on GUI.

    Quote Originally Posted by Pydra View Post
    Do you perhaps have a suggestion about what i could do to solve this problem?
    It's hard to know where to begin - it's a mess. You're setting the frame visible before you add any components to it, you don't call pack() on it to lay out the components, you're adding the overlay panels in the wrong order, panels are opaque by default, so you need to make the top one transparent, setting bounds or sizes on GridLayout components is pointless because it sizes them to fit the grid - you should use the hgap andvgap constructor arguments (read the API docs!), and a panel border if needed... etc. I got bored at that point.

    Even if you reworked that code into something reasonable, Kevin's suggestion is just so much cleaner and simpler.

    I think you'll find some time spent on the Swing Tutorial rewarding.
    Last edited by dlorde; July 7th, 2011 at 12:22 PM.

Similar Threads

  1. Need help adding background image (noob here)
    By OpX316 in forum AWT / Java Swing
    Replies: 18
    Last Post: July 7th, 2011, 09:10 AM
  2. Is it possible to make a background to in image transparent
    By Zein in forum Java Theory & Questions
    Replies: 10
    Last Post: June 13th, 2011, 04:18 AM
  3. Trouble getting an image to display.
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 23rd, 2011, 07:52 AM
  4. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  5. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 12:10 PM