Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:15:58+00:00 2026-06-16T04:15:58+00:00

How do I draw that semi-transparent rectangle on the screen? That cannot be a

  • 0

bound

How do I draw that semi-transparent rectangle on the screen?
That cannot be a JFrame because JFrames have the usual close, minimize, maximize options in top right.
if it is indeed a swing competent, How is it drawn in thin air? Without inserting it in a JFrame whatsoever?
Please tell me what it is and how I can implement it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:16:00+00:00Added an answer on June 16, 2026 at 4:16 am

    The immediate idea that comes to mind is to use java.awt.Robot to capture a screen shot, paint that to frameless window. From there you can simply draw a rectangle on it

    Updated with example

    … Took some time …

    enter image description here

    public class SelectionRectangle {
    
        public static void main(String[] args) {
            new SelectionRectangle();
        }
    
        public SelectionRectangle() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setUndecorated(true);
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new BackgroundPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class BackgroundPane extends JPanel {
    
            private BufferedImage background;
            private Point mouseAnchor;
            private Point dragPoint;
    
            private SelectionPane selectionPane;
    
            public BackgroundPane() {
                selectionPane = new SelectionPane();
                try {
                    Robot bot = new Robot();
                    background = bot.createScreenCapture(getScreenViewableBounds());
                } catch (AWTException ex) {
                    Logger.getLogger(SelectionRectangle.class.getName()).log(Level.SEVERE, null, ex);
                }
    
                selectionPane = new SelectionPane();
                setLayout(null);
                add(selectionPane);
    
                MouseAdapter adapter = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        mouseAnchor = e.getPoint();
                        dragPoint = null;
                        selectionPane.setLocation(mouseAnchor);
                        selectionPane.setSize(0, 0);
                    }
    
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        dragPoint = e.getPoint();
                        int width = dragPoint.x - mouseAnchor.x;
                        int height = dragPoint.y - mouseAnchor.y;
    
                        int x = mouseAnchor.x;
                        int y = mouseAnchor.y;
    
                        if (width < 0) {
                            x = dragPoint.x;
                            width *= -1;
                        }
                        if (height < 0) {
                            y = dragPoint.y;
                            height *= -1;
                        }
                        selectionPane.setBounds(x, y, width, height);
                        selectionPane.revalidate();
                        repaint();
                    }
    
                };
                addMouseListener(adapter);
                addMouseMotionListener(adapter);
    
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(background, 0, 0, this);
                g2d.dispose();
            }
    
        }
    
        public class SelectionPane extends JPanel {
    
            private JButton button;
            private JLabel label;
    
            public SelectionPane() {
                button = new JButton("Close");
                setOpaque(false);
    
                label = new JLabel("Rectangle");
                label.setOpaque(true);
                label.setBorder(new EmptyBorder(4, 4, 4, 4));
                label.setBackground(Color.GRAY);
                label.setForeground(Color.WHITE);
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(label, gbc);
    
                gbc.gridy++;
                add(button, gbc);
    
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SwingUtilities.getWindowAncestor(SelectionPane.this).dispose();
                    }
                });
    
                addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        label.setText("Rectangle " + getX() + "x" + getY() + "x" + getWidth() + "x" + getHeight());
                    }
                });
    
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(new Color(128, 128, 128, 64));
                g2d.fillRect(0, 0, getWidth(), getHeight());
    
                float dash1[] = {10.0f};
                BasicStroke dashed =
                                new BasicStroke(3.0f,
                                BasicStroke.CAP_BUTT,
                                BasicStroke.JOIN_MITER,
                                10.0f, dash1, 0.0f);
                g2d.setColor(Color.BLACK);
                g2d.setStroke(dashed);
                g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
                g2d.dispose();
            }
    
        }
    
        public static Rectangle getScreenViewableBounds() {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
    
            return getScreenViewableBounds(gd);
        }
    
        public static Rectangle getScreenViewableBounds(GraphicsDevice gd) {
            Rectangle bounds = new Rectangle(0, 0, 0, 0);
            if (gd != null) {
                GraphicsConfiguration gc = gd.getDefaultConfiguration();
                bounds = gc.getBounds();
    
                Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
    
                bounds.x += insets.left;
                bounds.y += insets.top;
                bounds.width -= (insets.left + insets.right);
                bounds.height -= (insets.top + insets.bottom);
            }
            return bounds;
        }
    }
    

    Update with SnipIt Example

    Some people have suggested using a transparent window laid over the top of the screen, this actually won’t work, as transparent windows don’t actually respond to mouse clicks UNLESS they have something to be painted on them that will allow the mouse event to be trapped.

    It’s also been suggested that you use a Window as the selection mechanism, this is a valid answer, however, I would (personally) find that to be an unsuitable solution, as you want the user to simply click and drag the selection rectangle (IMHO).

    Another approach is use something like SnipIt.

    enter image description here

    public class SnipIt {
    
        public static void main(String[] args) {
            new SnipIt();
        }
    
        public SnipIt() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setUndecorated(true);
                    // This works differently under Java 6
                    frame.setBackground(new Color(0, 0, 0, 0));
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new SnipItPane());
                    frame.setBounds(getVirtualBounds());
                    frame.setVisible(true);
                }
            });
        }
    
        public class SnipItPane extends JPanel {
    
            private Point mouseAnchor;
            private Point dragPoint;
    
            private SelectionPane selectionPane;
    
            public SnipItPane() {
                setOpaque(false);
                setLayout(null);
                selectionPane = new SelectionPane();
                add(selectionPane);
                MouseAdapter adapter = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        mouseAnchor = e.getPoint();
                        dragPoint = null;
                        selectionPane.setLocation(mouseAnchor);
                        selectionPane.setSize(0, 0);
                    }
    
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        dragPoint = e.getPoint();
                        int width = dragPoint.x - mouseAnchor.x;
                        int height = dragPoint.y - mouseAnchor.y;
    
                        int x = mouseAnchor.x;
                        int y = mouseAnchor.y;
    
                        if (width < 0) {
                            x = dragPoint.x;
                            width *= -1;
                        }
                        if (height < 0) {
                            y = dragPoint.y;
                            height *= -1;
                        }
                        selectionPane.setBounds(x, y, width, height);
                        selectionPane.revalidate();
                        repaint();
                    }
                };
                addMouseListener(adapter);
                addMouseMotionListener(adapter);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                Graphics2D g2d = (Graphics2D) g.create();
    
                Rectangle bounds = new Rectangle(0, 0, getWidth(), getHeight());
                Area area = new Area(bounds);
                area.subtract(new Area(selectionPane.getBounds()));
    
                g2d.setColor(new Color(192, 192, 192, 64));
                g2d.fill(area);
    
            }
        }
    
        public class SelectionPane extends JPanel {
    
            private JButton button;
            private JLabel label;
    
            public SelectionPane() {
                button = new JButton("Close");
                setOpaque(false);
    
                label = new JLabel("Rectangle");
                label.setOpaque(true);
                label.setBorder(new EmptyBorder(4, 4, 4, 4));
                label.setBackground(Color.GRAY);
                label.setForeground(Color.WHITE);
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(label, gbc);
    
                gbc.gridy++;
                add(button, gbc);
    
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SwingUtilities.getWindowAncestor(SelectionPane.this).dispose();
                    }
                });
    
                addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        label.setText("Rectangle " + getX() + "x" + getY() + "x" + getWidth() + "x" + getHeight());
                    }
                });
    
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                // I've chosen NOT to fill this selection rectangle, so that
                // it now appears as if you're "cutting" away the selection
    //            g2d.setColor(new Color(128, 128, 128, 64));
    //            g2d.fillRect(0, 0, getWidth(), getHeight());
    
                float dash1[] = {10.0f};
                BasicStroke dashed =
                        new BasicStroke(3.0f,
                        BasicStroke.CAP_BUTT,
                        BasicStroke.JOIN_MITER,
                        10.0f, dash1, 0.0f);
                g2d.setColor(Color.BLACK);
                g2d.setStroke(dashed);
                g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
                g2d.dispose();
            }
        }
    
        public static Rectangle getVirtualBounds() {
    
            Rectangle bounds = new Rectangle(0, 0, 0, 0);
    
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice lstGDs[] = ge.getScreenDevices();
            for (GraphicsDevice gd : lstGDs) {
    
                bounds.add(gd.getDefaultConfiguration().getBounds());
    
            }
    
            return bounds;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I draw a rectangle that have a black border and white background?
Is it possible to draw an arrow that has semi-transparent border around it just
I'm trying to render to a texture, then draw that texture to the screen
I want to draw rectangle that is only specified percent high of panel im
I have the code that draw a point inside a polygon. Each time I
I have a function that draw an image on graphics: private void DrawSmallImage(Graphics g)
I have my program that can draw rectangles. I have two problems I can't
I have an application that uses the mapview-overlay-manager code to draw map markers on
is it possible to draw a line semi-transparent with image magick? i want to
i'm trying to draw a rectangle with semi-circles at either end. i'm also trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.