JLabel over transparent painted Rectangle? - java

I set the background of a JWindow completely transparent. Then I painted a rounded Rectangle (RGB: 0,0,0,100) in it's paint-Method and added a JLabel to the JWindows ContentPane. But when I try to repaint the JWindow to update the JLabel, it doesn't remove the old Rectangle and the old value of the JLabel. So the result is that my custom tooltip box (what it should be) gets less transparent and you cannot read the JLabels contents, because it overlays the old contens.
Is there any way to solve this problem?
BTW, if I don't repaint, it doesn't get less transparent, but the new contents of my JLabel overlays the old Contents, like it is, when I repaint.

First you should override paintComponent instead of paint and call super.paintComponent(g). You should leave JWindow opaque, because the component on the rearmost layer will clear the old contents. If all the layers are transparent you will end up with screen garbage.
See painting with Swing. Perhaps you really wanted to create translucent windows?

Related

Is it possible to make a selection tool for an ImageIcon in a JLabel?

I want to be able to select part of an image in an ImageIcon on JLabel and fill it with color.
Is this possible I have become a little confused as I have read that an ImageIcon is not selectable but I am not sure if that means I have to find another way of displaying the image?
Possible, yes, difficult, yes.
You need to start with a BufferedImage, which you can then wrap in a ImageIcon and apply to a JLabel.
You would then need to register a MouseMotionListener and MouseListener to the label to detect the area which was selected, you would then modify the BufferedImage accordingly and repaint everything.
Having said that, I wouldn't use a JLabel, as you can't accurately calculate the location that the label renders the icon, instead, I'd make myself a custom component, extending from JPanel and encapsulate the functionality within it and use custom painting to paint the image (and the selection area)
Start by having a look at How to Write a Mouse Listener, Performing Custom Painting, 2D Graphics and possibly Reading/Loading an Image, Writing/Saving an Image

How disable JFrame Background

I've made a JFrame with the next Properties:
setLayout(null)
setUndecorated(true)
setResizable(false)
within i've put a JLabel with one Icon(PNG image) in netbeans, and im looking for some soluction that disable the backgroud(full Transparent, and unactive) when the mouse moves THROUGH JFrame's Image(or someother component) BUT the mouse works different outside of the JLabel because JLabel Icon avoid any mouse action over the JLabel. But there's a default Gray background that doesnt exactly what i want.
We can see that mouse doesnt do anything on the JLabel(unless there were some component in the Frame)
Green = JFrame size.
And here the mouse change when moves through of the web page
im looking for some soluction that disable the backgroud(full Transparent, and unactive) when the mouse moves
Don't use full transparency.
If the pixels are not 100% transparent, then the MouseEvents will be handled by the frame.

Transparent JPanel not working correctly

I want my JPanel to be semi transparent. This panel is animated and has the motion of a dropdown list. I have used the Color(r,g,b,a) constructor to achieve the transparency, however the transparency only takes effect as the panel returns to its original position. For example, as it is moving downwards it is not transparent at all, however when moves up, it spontaneously becomes transparent.
How do I fix this problem?
detailPanel.setBackground(new Color(50,50,50,220));
detailPanel.setLayout(null);
detailPanel.setBounds(0,posY,1200,750);
Check out Backgrounds With Transparency for the cause of the problem (you can't use a transparent Color on an opaque background) and a couple of solutions to fix the problem:
make the component non-opaque and paint the background yourself
use the AlphaContainer class so it can do the painting of the background for you

Swing: Can't set colour of jtextfield inside a panel with image

I have a JPanel which has a background image. To this panel, I add a JTextField, and set the background color to that that text field.
I expected the background to get the colour, but it is being overriden by the background image of panel.
Is this expected behavior? If not, how to get around it?
Check that your textfield is opaque.
Swing behaviour depends on many properties and layouts. You can try to debug your swing components with this tool and make some experiments with properties(background, opaque, ...).

JPanel custom paint

I am having an issue painting a JPanel. It paints correctly, however when I resize the parent container, the JPanel gets painted at the top left corner of the window, as well as in the correct position.
The JPanel has a custom paint method, that paints a set of components that don't belong to any JPanel (including it). These are added to the JPanel immediately before their paint method is called, and is removed immediately after:
paintOnto.setIgnoreRepaint(true);
paintOnto.add(getPaintableComponent());
getPaintableComponent().paint(g);
paintOnto.remove(getPaintableComponent());
paintOnto.setIgnoreRepaint(false);
paintOnto is a reference to the JPanel, getPaintableComponent() returns a JComponent that is to be drawn. g is the Graphics object passed through from the JPanel paint method.
The add/remove code was not required in mac at all, but under windows without it the components didnt paint at all.
This issue is only present in windows and ubuntu, not mac. Other systems are untested.
Any ideas what could be causing it? I have checked the position of the JPanel whenever it is painted, and it never is positioned at the top left corner.
Thanks
TRy to save AffineTransform of the graphics before and restore it after painting.

Categories

Resources