-1

Is there a way to tell if the pixel of a JComponent (such as JEditorPane, JPanel, etc) is transparent?

I know that for top-level containers such as JFrame or JDialog, getContentPane().paintAll() can be used to paint the content of the container to a BufferedImage. We can then use getRGB(x,y) to get the color & transparency of the pixel at (x,y).

However, the approach doesn't tell me the color of the individual components. Is there a way to know the pixel colors of low-level containers?

More info:

My purpose for this is to have a JEditorPane behave like a transparent overlay. The JEditorPane can contain text, pictures, etc, but its background is transparent

I'd like the mouse events to be forwarded to the components under the JEditorPane if they happen over a transparent pixel of the JEditorPane, but dealt with differently if the pixel is opaque.

Guillaume F.
  • 1,010
  • 7
  • 21
  • 2
    That depends a lot on what you are trying to do (and why). Swing components are either fully opaque or fully transparent, so you could inspect the component hierarchy and check `isOpaque` and `getBackground` – MadProgrammer Mar 30 '20 at 04:30
  • @MadProgrammer not so. Most Swing components can be given a transparent background through `setBackground(new Color(0,0,0,0))`; once this is done, the Swing component will have both transparent and opaque pixels. – Guillaume F. Mar 30 '20 at 07:05
  • 1
    Actually, that won't work, because, as I said, Swing components are either fully opaque or full transparent, the only way to make a component transparent, with out generating a lot of random graphic glitches, is to use `setOpaque` ... and you won't believe how often that question gets posted . Translucent components need to faked by first making them non-opaque and then implementing the `paintComponent` method to fill the component with the desired color and alpha level – MadProgrammer Mar 30 '20 at 08:19
  • @MadProgrammer for many Swing components (including `JEditorPane`, try it yourself), setting `setOpaque(false)` will result in the background pixels that are transparent not getting painted, but everything will still get painted. – Guillaume F. Mar 30 '20 at 08:39
  • Yes, but's that different from using a alpha based color. You "can" override that functionality, you understand how the look and feel delegates work and you're also taking into consideration the `JScrollPane` - [for example](https://stackoverflow.com/questions/43531992/make-background-image-show-through-panels-on-top/43532093#43532093) – MadProgrammer Mar 30 '20 at 12:40

1 Answers1

2

You can simply try to modify my previous answer to fit your requirement

public static boolean isTransparent(Component c, Point p) {
    Rectangle rect = c.getBounds();
    BufferedImage img = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB);
    c.paintAll(img.createGraphics());
    return new Color(img.getRGB(p.x, p.y), true).getAlpha() == 0;
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48