To do this, I believe you need to use Glass Pane . Glass Pane sits on top of everything in JRootPane and fills the entire view. This particular position allows you to use two different possibilities:
- Interception of mouse and keyboard events
- Drawing the entire user interface
I believe that your question is being solved by the second opportunity. The following is an example implementation that you can later adapt to meet your own needs. Please note that I did not focus on Glass Pane , which you will need to explore on your own.
CODE
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.KeyAdapter; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class GlassPaneDemo { private static BufferedImage bi; public static void main(String[] args){ try { loadImages(); SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { createAndShowGUI(); } }); } catch (IOException e) {
OUTPUT

EXPLANATION
In this example, I clicked two arbitrary points in each JLabel and then drew a connecting line.
source share