Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-03-2010, 10:35 PM
Junior Member
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
OBLITERATOR is on a distinguished road
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:

Java 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; 05-03-2010 at 12:30 AM. Reason: Edited to make code more visible
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 05-03-2010, 12:19 AM
copeg's Avatar
Moderator
 
9 Highscores

Join Date: Oct 2009
Posts: 570
Thanks: 7
Thanked 131 Times in 125 Posts
copeg will become famous soon enoughcopeg will become famous soon enough

I'm feeling Sleepy
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; 05-03-2010 at 12:24 AM.
Reply With Quote
  #3 (permalink)  
Old 05-03-2010, 12:32 AM
Junior Member
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
OBLITERATOR is on a distinguished road
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!
Reply With Quote
  #4 (permalink)  
Old 05-03-2010, 04:10 PM
Junior Member
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
OBLITERATOR is on a distinguished road
Default Re: Background image on GUI

Java 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.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!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
JButton set background problem ellias2007 AWT / Java Swing 1 25-02-2010 04:15 AM
Array to image oxxxis Collections and Generics 1 19-01-2010 10:46 PM
Pixel Alteration in an image/image rotation bguy Java Theory & Questions 3 01-11-2009 02:50 PM
RGB - YUV - awt.Image BharatT AWT / Java Swing 0 24-02-2009 09:10 AM
How to Import an Image in JSP? jazz2k8 JavaServer Pages: JSP & JSTL 4 12-05-2008 10:55 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:53 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.