How to make an overlay that overlays over other program

Hello, I was wondering if there was any way to make it so I could have a program, that if you switch to another program, it would still be on your screen and you could interact with the other program normally?
For example, a line that points from the middle of your screen to your mouse cursor.
Or maybe even effects

1 Like

Do you mean that you want to detect if your sketch is the “active” window?

No I want it to go ontop of other programs without affecting the programs in any way, for example if you clicked on the program it wouldnt switch between them

So you mean that the program only puts a graphic on the screen and doesn’t interfere with any of inputs “meant” for another program?


For example, Im playing a game, and I have a white line pointing from the middle of my screen to my mouse

1 Like

I understand… There maybe is some trickery to be done with plain Java but processing graphics aren’t intended for that purpose.

Okay, I’m sorry for wasting your time.

I just did some research on that and you can create overlays using Java (and therefore processing) take a look at this code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
JFrame drawingFrame=new JFrame("CustomWindow");
drawingFrame.setUndecorated(true);
drawingFrame.setBackground(new Color(0,0,0,0));
drawingFrame.setSize(640,640);
drawingFrame.getContentPane().setLayout(new BorderLayout());
drawingFrame.getContentPane().add(new JTextField(10),"North");
drawingFrame.setAlwaysOnTop(true);
drawingFrame.setVisible(true);
drawingFrame.getRootPane().putClientProperty("apply.awt.draggableWindowBackground",false);
drawingFrame.pack();

I could try if I can manage to force the normal processing window in such a state.

@5x9x7x2x7x9
Here is the program you described! I hope thats what you need!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Canvas canv=new ResultOutput();
JFrame drawingFrame=new JFrame("CustomWindow");
void setup() {
  size(50, 50);
  //creates an empty window
  drawingFrame.setUndecorated(true);
  drawingFrame.setBackground(new Color(0, 0, 0, 0));
  drawingFrame.add(canv);
  canv.setBackground(new Color(0, 0, 0, 0));
  drawingFrame.setSize(displayWidth, displayHeight);
  drawingFrame.setAlwaysOnTop(true);
  
  drawingFrame.setVisible(true);
  drawingFrame.getRootPane().putClientProperty("apply.awt.draggableWindowBackground", false);
  drawingFrame.pack();
  //hides the Processing window
  getSurface().setVisible(false);
}
Point p=MouseInfo.getPointerInfo().getLocation();
void draw() {
  //get the mousePosition
  p=MouseInfo.getPointerInfo().getLocation();
  canv.revalidate();
  //refresh the canvas
  SwingUtilities.updateComponentTreeUI(drawingFrame);
}
void mousePressed() {
}
public class ResultOutput extends Canvas {
  public void paint(Graphics g) {
    int strokeWidth=5;
    Graphics2D g2=(Graphics2D) g;
    //here you can paint the screen
    g2.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(strokeWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    g2.drawLine(displayWidth/2,displayHeight/2,p.x,p.y);
    //g2.drawOval(p.x,p.y,strokeWidth/2,strokeWidth/2);
    
    //This allows you to always click
    g2.clearRect(p.x,p.y,1,1);
  }
}

Edit: added a way to set a custom strokeWeight

4 Likes