1

I'm struggling with two Java GUI things at the moment.

This is the situation: I'm designing a word game using Swing components. I have a main JFrame where everything is placed (my GUI class extends JFrame). There are two things I want to do:

1st: I would like to set an image as the background image of the main frame, it has to be displayed behind all components. i've searched around but haven't found a working solution. I tried making an extended BackGroundPanel class but when I create an instance of BackGroundPanel I have no idea how to make it the background of the frame... I also haven't find a good way to load in an image from an 'images' directory in my src folder...

2nd: when the program starts the user is greeted with an undecorated JDialog, the main frame needs to be disabled, which I figured out, but I would also like to make it a bit darker. I believe it should be possible with the GlassPane, but I have no idea how to set the GlassPane to cover the panel with one color...

Help will be much appreciated, I don't think I have any helpful code to share, but I think the situation explained above gives a general idea? I would just like someone to get me on track with this so I can further work this out! Thanks!

E. V. d. B.
  • 815
  • 2
  • 13
  • 30

2 Answers2

2

My Main class extends JFrame and it has a BorderLayout.

Add your BorderLayout to a JPanel having, e.g. GridLayout().

  1. This AnimationTest illustrates painting a background image behind components.

  2. This Translucent example illustrates using an AlphaComposite; see also this AlphaTest.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • First of all thanks, but I still don't get how to do it, because adding to gridlayout will just create new cells right? Can you add a panel and on top of that a main panel in one gridlayout cell? – E. V. d. B. Mar 04 '13 at 18:34
  • No, `GridLayout()` creates a single cell that behaves much like `BorderLayout.CENTER`. – trashgod Mar 04 '13 at 18:36
  • Also, don't extend `JFrame`; extend `JPanel` or `JComponent` and override `paintComponent()`. – trashgod Mar 04 '13 at 18:39
  • so I could just add the image to the GridLayout() and then add the mainPanel with all the components? What's a good way to read an image from a directory in my source file because I never succeed in that, Icons are no problem though... – E. V. d. B. Mar 04 '13 at 18:41
  • You'll have to experiment to get the effect you want; see the answers [here](http://stackoverflow.com/q/15182329/230513) concerning resources. – trashgod Mar 04 '13 at 18:48
0

Well for your first question, you can use a label and set the icon of it:

JLabel lblimage = new JLabel("");

lblimage.setIcon(new ImageIcon(Main.class.getResource("/img/background.png")));
lblimage.setBounds(0, 0, 794, 711); //size of frame

contentPane.add(lblimage); //bottom
contentPane.add(component1); //middle low
contentPane.add(component2); //middle top
contentPane.add(component3); //top

as for your second question.. you could possibly do the same thing, just use an image with a solid color and lower the transparency, and place on top of your other components (not sure on this solution though).

Chris K
  • 1,581
  • 1
  • 10
  • 17
  • Hmmm but I already have other panels and components on my mainframe, how could I place the JLabel behind them all? – E. V. d. B. Mar 04 '13 at 17:41
  • @E. V. d. B. for better help sooner, post an [SSCCE](http://sscce.org/), short, runnable, compilable, just about decriptions in your question, otherwise everything here are shots to the dark – mKorbel Mar 04 '13 at 17:51
  • I edited the answer to show how. You would replace contentPane with whatever you add all of your components to. – Chris K Mar 04 '13 at 17:51
  • @ck1221 Yeah, but my Main class extends JFrame and it has a BorderLayout, But each segment (NORTH, EAST, SOUTH, WEST, CENTER) can only contain one component (panel) right? How can I display the full image behind everything else? I don't think that will work with just .add(image) or will it? – E. V. d. B. Mar 04 '13 at 18:02