Swing: how to paint an animation over every component, JPanel, JButton, etc? - java

I'm trying to paint over every component in my Swing application.
what I have:
jButton b = new JButton();
b.addActionListener(e -> fillEntireScreen())
f.add(b);
f.setVisible()...f.setSize()...f.setDefaultCloseOperation()...
where fillEntireScreen() simply is an animation that expands over the screen. I override Component's paintComponent(), painting over the frame, not a panel or anything.
fileEntireScreen() works without the button, but does not work when I add the button, as the animation is behind the button.
how can I paint over the button?
Thanks!

Use a glassPane instead - See How to Use Root Panes for more details and How can I paint in an specific JPanel when more than one in same frame- Java for an example.
Alternatively you could use a JLayer, but's more complicated and may not suit your needs. See How to Decorate Components with the JLayer Class

Related

Adding a background image to JFrame using JLabel

I added a background image to a JFrame using a JLabel. But there are other JLabels in a panel added to the particular JFrame. After adding this background image all over the JFrame, other JLabels are not visible. How do I get them be visible?
Several options:
Make the JLabel opaque and make it the contentPane, giving it a decent layout, give it a decent getPreferredSize() override that makes sense in the context of your problem (JLabel's own override for this method won't work for you, since it depends on text and icons that the label holds and not on components added).
And then add other components to it -- to the JLabel.
Or:
Use a JPanel as the background image displayer
Display the image in its paintComponent(...) method override.
Give it decent layout manager(s) and if needed, a getPreferredSize() override.
Also:
Take care that some added components, such as other JPanels, are set to be non-opaque.

Some JPanel's backgrounds fade when I interact with page

As i wrote in the title, I've this problem with JPanel.
My page is structured as a mainJPanel, another JPanel inside the main (where I paint a background) and some component added to this background panel.
Everytime I interact with something (for example i click a button) the background of the components fade to a lighter version of themselves.
I really can't figure out why.
mainPanel have setOpaque(false) and setLayout(null);
backgroundPanel have setOpaque(false) and setLayout(null) (i painted the background when i create the class backgroundPanel that extends JPanel, with an override of the method paintComponent);
all the components that becomes lighter have setOpaque(true) and setLayout(null);
the background of this components are a custom grey (rgb(232,232,232)).
Thank you all
Using setOpaque(false) should not cause a problem (ie. JLabels are non opaque). It sounds like a painting problem when using transparent colors.
Check out Background With Transparency for an explanation of the problem and a couple of solutions.

For what component should I add KeyListener?

I have a GUI.
This GUI class extends JFrame.
In top of the screen there is a JLabel with some text.
In the center of the screen there is a JPanel, and n x n JButtons were added to this JPanel.
I need a KeyListener to react if I press a key.
For what component (JFrame, JLabel, JPanel, JButtons) should I add the KeyListener to work my program normally?
The JFrame would be the smartest choice given that it is a top level ancestor you would be able to avoid focus problems. However, if you say you were to implement a JTextField it would then be necessary to add the keylistener to the JTextField because focus is drawn away from parallel components in order to consume the ability to type into the field.
For what component should I add KeyListener?
you can to use Keylistener for (is designated)
prehistoric AWT Component (Swing based on AWT)
for more than 3 keyPressed in the same time or to determine the order
but then is for Swing JComponents
better to use KeyEventDispatcher or AWTEventListener (both react to singnals from keyboard)
otherwise use KeyBindings (e.g. Swing JComponents uses KeyBindings internally)

Java - control Z order of JPanels

In short, my need is to have a background Image in my java app, and upon some event, create some other graphics on top of that image.
I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program, and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything.
Is there any way to control the Z index/order of the JPanels, or am I going about this completely wrong?
You can use the setComponentZOrder() to handle Z-Order in your application.
Resources :
JavaDoc - Container.setComponentZOrder
oracle.com - Mixing heavy and light components
Sounds strange to add mutiple JPanels and use z-order. I would suggest you either simple add ONE JPanel with the paintComponent(Graphics g) method overwritten, where you draw the correct image according to your events.
Or use the CardLayout, and add JLabels (with different images), then when your event triggers, use
CardLayout cl = (CardLayout)getLayout();
cl.show(this, "card3");
to show the correct JLabel.
The JLayeredPane is designed for just this purpose. It allows you to control the Z-order of components.
I was thinking I would use a JPanel to
draw the background image in, add it
at to my JFrame the start of program,
Sounds reasonable.
and then add other JPanels on top of
that upon certain events. The problem
is Swing gives the JPanels added first
the highest Z index, so what should be
my background is showing up on top of
everything.
Adding a panel to a panel is just like adding another component to the panel. The child component will be painted on top of the parent panel. There is no need to play around with Z-Order.
The key is to set a layout manager on the panel with the image. Then each panel you add to the image panel must be made non-opaque so that the background image will be painted.
Maybe the Background Panel can help you out. It automatically makes any component added directly to the panel non-opaque.

Adding components to a GLCanvas

I'm working with an application that draws on a GLCanvas. I'd like to add a "floating menu" on top of it (something I would do in Swing by adding a menu to the glass pane). Since GLCanvas doesn't extend Container, what would be the suggested way to do this?
GLCanvas inherits from java.awt.Component, so when you add a GLCanvas to your JFrame, you could use the glasspane on your containing JFrame.
Or, depending on the visual effect you want, you could, after your scene is done rendering on the GLCanvas, add a GL call to glOrtho, and then draw your menu on top of the scene using primitives in GL itself, (though then you'd be stuck rigging your own callback behaviors and such... I'm not sure from the question if you want to get into that).
Are you talking about a popup menu? You can add a MouseListener to your GLCanvas (since it extends from Component), in the MouseListener, check the mouseEvent.isPopupTrigger(), and if so, create your JPopupMenu - since you want to show it over a heavyweight component, call setLightweightPopupEnabled(false) before showing the JPopupMenu - then call show(glCanvas, x, y) on your JPopupMenu.

Categories