I'm making a microwave simulation program and having an annoying problem. I want the microwaves viewing window to change color on a button press, to visualize that the microwave is on. However, because I also have a jlabel with an icon (food image) in the background, I need the background to be semi transparent. I've accomplished this adding an alpha value to the jPanel (cookingWindow).
It starts like this
So what I've basically done is set an actionEvent to the start button with the following code.
cookingWindow.setBackground(new Color (250,234,1, 150));
cookingWindow.repaint();
This works, except this happens
When I minimise and maximise the window, everything goes back to place. But obviously, it would be preferable if you didn't have to minimise the window. Any ideas on how to stop this visual bug?
See Backgrounds With Transparency for an explanation of the problem and a couple of solutions.
Basically the problem is how the Swing opaque property handles transparent backgrounds (it doesn't).
Revalidating and repainting the jFrame seems to do fix everything up. I was previously only repainting the jPanel.
Related
I have a JPanel v. Function below creates a full-screen Jframe, called window, which will feature this JPanel. I have:
protected final void fullScreenMulti(final JPanel v) {
final JFrame window= new JFrame();
window.setUndecorated(true);
window.add(v);
//window.setVisible(true);
.
.
.
window.validate();
window.repaint();
GraphicsDevice gdev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gdev.setFullScreenWindow(window);
DisplayMode mode = gdev.getDisplayMode();
window.setSize(mode.getWidth(), mode.getHeight());
}
This works for the default render that is written for the code. However, selecting the GL renderer which uses JOGL, makes the fullscreen frame blank and I get a white screen. You will have to switch-tab in and out, or press Start for the Panel to be painted. I tried changing the background of window using:
Container c = window.getContentPane();
c.setBackground(Color.yellow);
that doesn't do anything though and I still get a white screen. (Although when i debug, I can see that the background property is successfully applied.)
Also clicking on the blank area of a second screen(The window is hosted on the first screen), or even clicking the Start button, SOMETIMES makes the frame go Black and my mouse adaptor fails to work afterwards. (what exactly could be happenning here, how can I investigate it?) Other times, it has the same effect of Alt-tab.
Haven't done much Swing stuff in Java and a I'm bit lost. Seeing that it seems to be working fine on one renderer and not on the other, suggests that it has something to do with the GL renderer, but the renderer works fine when rendering JPanels in my non-full screen mode; Also my understanding is that the role of the renderers is merely to buffer the video that is featured on my JPanel.(I mean that's what renderers do, right?) so it is probably a case of some setting not being specified above, but the original renderer takes care of it. Any suggestions what that might be?
edit: needless to say, taking out window.add(v) will make the frame render fine and the result of changing frame background etc. that was previously not working, will now be visible on the frame.
This is a known problem. -Dsun.java2d.noddraw=true solves it.
I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p
I've created an applet which has one large panel to display data surrounded by several controls (buttons, textfields, etc.). The large panel contains several layers of labels which I render myself.
The controls all have tooltips associated with them, and some of these tooltips overlap the main panel. When they disappear, they leave a hole in the main panel image until the main panel is repainted.
Now mind you, this does not always happen. It only occurs when the cursor is in a certain range. If you get far enough to either the left or right (no difference noted for changes along the Y axis), the holes are painted over when the tooltip disappears.
I'm not well-versed on how tooltips and repainting are supposed to work, and if this is a sign that there's something dreadfully wrong with my program, but if I can just call repaint on the main panel whenever the tooltip disappears, I should be fine. Is there something I can override in tooltip to make this happen?
I'm using Swing
Thanks.
To answer your question (after you found a solution by the comments): Swing has some quite elaborate repaint management built in. When a tooltip disappears, the rectangle below it is repainted.
Now, which components have to be repainted? All those who overlap with the given rectangle, and are not themselves hidden (in the region in question) by other components - but only opaque components count here. (This is the whole reason we need the opaque property on JComponent - to optimize repainting.)
Your label declared itself being opaque, but did not really paint its whole area on a paintComponent, and such the region of the tooltip which should have been covered by the label stayed unpainted.
Declaring your label to be partly transparent caused also the concerning region of the component behind it to be repainted.
I am implementing ToolTip in Java as to make users having an easier time to use the product. Though tooltip that are at the borders of the JFrame and ends up outside the JFrame starts to "flicker". I've tried lots of things (like moving the tooltip so it should be inside the Jframe, controlling the painting so it ends up within the JFrame and so on) though it doesn't work.
Anyone got any expertise within the field that know how to avoid this problem?
Cheers,
Skarion
When a tooltip is displayed in a JFrame, Swing does not create a floating window, it simply paints the tooltip in the graphic context of the JFrame. This does not generate any flickering.
On the other hand, when a tooltip is outside the boundaries of the JFrame, it becomes heavyweight: a window is created to host the tooltip component. Flickering occurs when the tooltip window appears.
Maybe setting "-Dsun.awt.noerasebackground=true" would help because it prevents one step of background repainting of the hosting window.
I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().