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

Thread: Background image on GUI

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Background image on GUI

    Hello all, i've just started java and i'm really not good at it, i've read about JPanel JFrame and the different layouts I can use and I still can't get my head round to setting an image as background with two simple buttons on top, I've tried every single combination I could think and have googled for ages, nothing seems to work... here is my code:

    import javax.swing.*;
    import java.awt.*;
     
    class Intro implements Runnable
    {
    public static void main(String[] args)
    {
    Intro program = new Intro();
    SwingUtilities.invokeLater(program);
    }
    public void run()
    {
    JFrame inicio = new JFrame("Videogame");
    inicio.setLayout(new BorderLayout());
    JPanel bt = new JPanel();
    ImageIcon image = new ImageIcon("image.jpg");
    bt.add(new JButton("START"));
    bt.add(new JButton("QUIT"));
    inicio.add(new JLabel(image));
    inicio.add(bt, BorderLayout.SOUTH);
    inicio.setDefaultCloseOperation(inicio.EXIT_ON_CLOSE);
    inicio.setSize(800,600);
    inicio.setLocationByPlatform(true);
    inicio.setVisible(true);
    }
    }
    any ideas? what happens is that the BorderLayout.SOUTH makes a huge white space next to the buttons and "over-rides" the image... is there a simpler way to do this or is there a way to make the SOUTH border translucent or similar?

    Thanks!
    Last edited by OBLITERATOR; March 4th, 2010 at 07:30 PM. Reason: Edited to make code more visible


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Background image on GUI

    There are a few ways you can do this.

    One way is to use an OverlayLayout: 1) create a JPanel and set its layout to OverlayLayout 2) add the ImageIcon to another newly created JPanel and add this to the panel created in step 1 3) create another JPanel and add your buttons there, add this JPanel to the JPanel created in step 1 4) add the JPanel created in step 1 to the JFrame.

    Another way is to override the paintComponent method of the JPanel, load the image as an Image object, and then draw the image at the position you wish.

    Both ways may require a bit of fiddling to position your buttons in the correct position.

    For future reference, using the code tags to place your code into a post make your code a lot more readable for those of us trying to diagnose the problem.
    Last edited by copeg; March 4th, 2010 at 07:24 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Background image on GUI

    Thank you! I hope that edit makes it look better although I should have indented it more!!!

    I will try tomorrow and see what happens!

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Background image on GUI

    import javax.swing.*;
    import java.awt.*;
     
    class Intro implements Runnable
    {
    public static void main(String[] args)
    {
    Intro program = new Intro();
    SwingUtilities.invokeLater(program);
    }
    public void run()
    {
    JFrame inicio = new JFrame("Videogame");
    inicio.setDefaultCloseOperation(inicio.EXIT_ON_CLOSE);
    inicio.setSize(800,600);
    inicio.setLocationByPlatform(true);
    inicio.setVisible(true);
     
    JPanel bt = new JPanel();
    bt.add(new JButton("START"));
    bt.add(new JButton("QUIT"));
     
    JPanel over = new JPanel();
    LayoutManager overlay = new OverlayLayout(over);
    over.setLayout(overlay);
     
    JPanel imagen = new JPanel();
    ImageIcon image = new ImageIcon("image.jpg");
    imagen.add(new JLabel(image));
     
    over.add(imagen);
    over.add(bt);
    inicio.add(over);
    }
    }

    I hope my code is clearer now!!

    It works fine except the second (QUIT) button takes a while to load and I don't understand why... any ideas? Sorry for the bother!

Similar Threads

  1. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 24th, 2010, 11:15 PM
  2. Array to image
    By oxxxis in forum Collections and Generics
    Replies: 1
    Last Post: January 19th, 2010, 05:46 PM
  3. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM
  4. How images stores in awt.Image class?
    By BharatT in forum AWT / Java Swing
    Replies: 0
    Last Post: February 24th, 2009, 04:10 AM
  5. How to import an .tiff image in JSP?
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: May 12th, 2008, 05:55 AM