Setting background image and greyed GlassPane in Java (Swing) - java

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!

My Main class extends JFrame and it has a BorderLayout.
Add your BorderLayout to a JPanel having, e.g. GridLayout().
This AnimationTest illustrates painting a background image behind components.
This Translucent example illustrates using an AlphaComposite; see also this AlphaTest.

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).

Related

Is there any way to stack JComponents in a JFrame?

I have a class that extends JFrame, and I have decided to create an opening panel, a JPanel with 4 JButtonss which is added add(initPanel) in the constructor.
However, ideally a background image would help the JFrame look cleaner and decorated.
Someone on here suggested I use setContentPane(), and this in fact works.
However, the image appears and all the buttons/text disappear, and I can't get them to show.
I am wondering if there is any way to stack layers / JComponents within a JFrame, so I could have my image in the background and my JPanel in the foreground.

JPanel vs JFrame when animating

I recently did a program which moves an image along the window and forces it to change its direction when it hits the borders. The animation happened within a JPanel class which was then of course added to a JFrame class which contains the main() function. Now my question is why must it be done in this manner. Can't I simply have just the JFrame and use that both as my window and my animation class which moves the image? Is the JFrame incapable of representing animation on it's own.
Regards.
Omar's answer is correct but I thought I might elaborate just a bit:
Though JFrame's are capable of hosting your animation, it is not a typical use of the JFrame. JFrame's are typically the outermost visual container and host one or more other visual containers (such as JPanel). Though there are exceptions, I would consider the JPanel the most flexible and common way to contain your animation (or other GUI 'controls') to leave you flexibility for adding visual features later.
But as Omar points out, either JFrame or JPanel can be used as they are both considered "containers" in Swing. Tying back to my last comment, using a JPanel is a bit more flexible. For instance, you might later want to incorporate your animation into an Applet/JApplet which you might find confusing and/or difficult if you used a JFrame.
I tried to just comment on Omar's answer but I am a new user and it won't let me add comments yet. Therefore, upvotes are appreciated :)
It's just better to use JPanel as you may want multiple Panels in one frame each with different animations.
Yes, you could do it with JFrame as well.

Why will image not add when JFrame has .setSize

I'm a somewhat novice programmer and I'm have some trouble adding an image to my frame. While I know how to add images generally, this specific case it does not work.
public class Tutorial extends JFrame{
Tutorial(){
JFrame frame = new JFrame("ImageTutorial");
frame.setVisible(true);
frame.setSize(750,850);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
ImageIcon image = new ImageIcon(getClass().getResource("Green Block.png"));
JLabel imagelabel = new JLabel(image);
imagelabel.setBounds(10, 10, 75, 75);
imagelabel.setOpaque(true);
frame.add(imagelabel);
Now, I've located the problem but I don't understand 'why' its a problem. When I remove
frame.setSize(750,850);
the image shows, but when its there it doesn't. How can the frame's size impact the image showing and how can I get around it?
Just curious, logically, what makes you think a frame should be visible before you add any components? Logically speaking, wouldn't it seem right to add your components first, then make the frame visible. It's like displaying a painting in an art gallery even before the painter has painted anything on it. It just makes no sense. I highly doubt setting the size has anything to do with it. IF you don't set the size of the frame, then the frame appears as small as possible. When you resize the frame, it causes a repaint, then showing the label you add. But generally, you want to always set frame visible after all you components are added, to avoid this problem.
Side note: You should stay away from null layouts. You need to learn to use Layout Managers and let them do the dynamic sizing and locating for you.

How to Display a Panel in another Panel?

I'm coding a simple graphics program in Java.
So, I have 3 classes.
The first class is the GUI w/c extends JFrame, it load the menu bar and panel (drawing class)
The second class is the drawing class, it extends JPanel, and it has simple Graphics commands.
The third class is an animation class, it displays an animation. It also extends JPanel.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class. When I try to place it there, it displays a tiny box beside the drawing class panel. I am not very good at frames and panels. Thank you very much in advance!
All JPanels have a LayoutManager which, rather handily, manages how Components are laid out. The default layout is FlowLayout, which default mode will simply place each component to the right of the last component.
If you want to change the layout to something more useful, there are many options; BorderLayout, GridLayout, GridBag layout are popular ones. Myself, I use MigLayout, an external library which is very powerful :).
As for it appearing small, try manually enforcing the size with setSize(w, h) or setPreferredSize(w, h).
You might want to write
GUIFrame.remove(drawingPanel);
GUIFrame.add(animationPanel);
GUIFrame.pack();
However the behavior may vary if you have other components added to your GUIFrame. It is difficult to help you exactly because you have not posted an SSCCE.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class.
Use a Card Layout.

JScrollPane doesn't show if BorderLayout constraint is CENTER?

Been developing a game for a while, and currently re working the GUI, or at least trying to. Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. A few projects later, and time to come back and do some more on the game, and I hit a problem...
The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. The scroll pane is a scroll pane, and the control panel a normal panel. The scroll pane contains the main game panel.
As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all.
To demonstrate this, I have created a quick video...
http://screenjel.ly/q5RjczwZjH8
As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there!
Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved!
I thank you for your time to read this.
Thanks in advance for any ideas or thoughts :)
Rel
If you'd posted some code to start with then you might have gotten a really quick answer. Luckily, you posted a link in the comments to the other response.
The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. However, that's not your problem.
The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane.
That's why you see it for NORTH and not for CENTER.
I can't get the video to show. It's been buffering for ages.
My guess would be that the scrollpane is in fact filling the center; it's just your game panel that's not being shown.
Your game panel needs to return reasonable values for getPreferredSize().
Update
Another thing you may want to do is have your game panel implement the Scrollable interface. You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions.

Categories

Resources