S
S
stepango2011-08-05 09:26:01
Java
stepango, 2011-08-05 09:26:01

Semi-transparent java window that ignores mouse clicks?

The task was to create an application, one of the components of which will be a translucent window that is always in the foreground, while behaving as if it does not exist. That is, if there is an application behind this horse, then only this application will respond to mouse clicks. Is it possible to do this with java? It's just a question of ignoring clicks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mgarin, 2011-08-11
@mgarin

1. I had an idea that a similar option might work:

public class NonResponsiveWindow extends JFrame
{
  public NonResponsiveWindow ()
  {
    super ();
    setLayout ( new BorderLayout () );
    add ( new JPanel()
    {
      {
        setOpaque ( false );
      }
      public boolean contains ( int x, int y )
      {
        return false;
      }
      public boolean contains ( Point p )
      {
        return false;
      }
      protected void paintComponent ( Graphics g )
      {
        super.paintComponent ( g );
        Graphics2D g2d = ( Graphics2D ) g;
        g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON );
        g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_OVER, 0.5f ) );
        g2d.setPaint ( Color.GRAY );
        Area area = new Area ( new Ellipse2D.Double ( 0, 0, getWidth (), getHeight () ) );
        area.subtract ( new Area (
            new Ellipse2D.Double ( 25, 25, getWidth () - 50, getHeight () - 50 ) ) );
        g2d.fill ( area );
      }
      public Dimension getPreferredSize ()
      {
        return new Dimension ( 300, 300 );
      }
    } );
    setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    setAlwaysOnTop ( true );
    setUndecorated ( true );
    AWTUtilities.setWindowOpaque ( this, false );
    pack ();
  }
  public boolean contains ( int x, int y )
  {
    return false;
  }
  public boolean contains ( Point p )
  {
    return false;
  }
  public static void main ( String[] args )
  {
    new NonResponsiveWindow ().setVisible ( true );
  }
}

(At the time of JDK 1.6.0_26 - this code works fine only under Win / Mac)
But, as you can see, the semi-transparent area still intercepts all events on the window, even though the panel "supposedly" does not fall into the area visible for events through overridden contains(...) methods.
You probably need to redefine it also in the JRootPane that is used on the window (that is, replace the standard RootPane with your own implementation) - but not the fact that it will help. I have a hunch that something else is checking to see if the window is really over the mouse, or if it's possible to pass events "through" it.
2. Speaking of other options… You asked above:
And if, for example, after receiving the signal, play it but for a different window?
This should be quite feasible using standard tools (you just need to correctly transfer the coordinates to the underlying window).
3. I have a counter question - do you really need a window hanging above your main one (for example, because the hanging one can go beyond the borders)?
If the hanging window (ignoring any events) is always within the boundaries of the main one, I can offer another, more efficient and simple implementation option.

A
Anatoly, 2011-08-05
@taliban

It seems to me that it is unrealistic to do this in any language, except to draw the necessary information on the screen, if there is a window, then it receives signals, and will accordingly respond to the mouse ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question