9

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...

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 1
    This tutorial might will help you, [how to make translucent windows](http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html) – nIcE cOw Dec 19 '12 at 09:17

3 Answers3

10

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;

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah, `robot` can be used to **capture** the screen. and then `ImageIO` can be used to write it to the disk but the question; what is that mystery rectangle? – An SO User Dec 19 '12 at 08:26
  • Simple paint onto the image using standard Graphics2D – MadProgrammer Dec 19 '12 at 08:28
  • I believe the question is referring to the "capture area border" on the screen itself, rather than the resulting captured image (or can `Robot` actually be used to draw beyond JVM windows?) – FThompson Dec 19 '12 at 08:34
  • I doubt for such a simple task, such a heavy work is needed to capture screen and do stuff, when simple `setOpacity()` method can be used, or else as explained in Java Docs, [how to create translucent windows](http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html). – nIcE cOw Dec 19 '12 at 09:20
  • The Example works not with Java6 ?! And the Frame is only on my 2nd Monitor. – oliholz Dec 19 '12 at 09:23
  • @GagandeepBali these guys have been following my non-stop question for the past 2 days. I am creating a screen recorder and these guys are helping me out – An SO User Dec 19 '12 at 09:26
  • @GagandeepBali that's a solution. The problem I have with it is managing the mouse events for resizing the window. Not to say its not a solution and it also depends on what the requirements are. I was more focused on the graphics rendering then the actual means – MadProgrammer Dec 19 '12 at 09:43
  • @oliholz the example will work for Java 6, simply catch the try-catch block around the UiManager to handle a single exception instead. Also, this example defaults to default screen. It wouldn't be hard to modify it for different screens – MadProgrammer Dec 19 '12 at 09:47
  • @GagandeepBali (sorry) the other issue I have with the suggestion is that you can just start "dragging", per say, you would have to start with an area - not to say you can't, it's just my "feeling" – MadProgrammer Dec 19 '12 at 09:51
  • +1 for `Stroke`. The example cited [here](http://stackoverflow.com/a/11944233/230513) may offer a useful comparison. – trashgod Dec 19 '12 at 10:51
  • @MadProgrammer your code is slightly unstructured. hard to follow :( Not saying that I cant understang, btw :) – An SO User Dec 19 '12 at 11:39
  • @littlechild Really? I think its relatively basic, you should try having a look at some of the questions that asked around here – MadProgrammer Dec 19 '12 at 19:24
  • @MadProgrammer yes, took me some time but got it :) Still working on my `JPopup` issue :) – An SO User Dec 19 '12 at 19:34
  • Programming in the 21st century is quite some fun. You don't have to be able to do anything but googling. – phil294 Mar 30 '15 at 15:46
7

Update Multi Monitor Support to the Example Answer from @MadProgrammer.

Without ExtendedState(JFrame.MAXIMIZED_BOTH) and pack()

enter image description here

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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new BackgroundPane());

            frame.setResizable( false );
            frame.setBounds( getScreenViewableBounds() );

            frame.setVisible(true);
        }

    });
}

public static Rectangle getScreenViewableBounds() {
    GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    int minx = Integer.MAX_VALUE;
    int miny = Integer.MAX_VALUE;
    int maxx = Integer.MIN_VALUE;
    int maxy = Integer.MIN_VALUE;
    for( GraphicsDevice device : devices ) {
        for( GraphicsConfiguration config : device.getConfigurations() ) {
            Rectangle bounds = config.getBounds();
            minx = Math.min( minx, bounds.x );
            miny = Math.min( miny, bounds.y );
            maxx = Math.max( maxx, bounds.x + bounds.width );
            maxy = Math.max( maxy, bounds.y + bounds.height );
        }
    }
    return new Rectangle( new Point(minx, miny), new Dimension(maxx - minx, maxy - miny) );
}
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • That's an interesting approach. I would use an Area in the getScreenViewableBounds, basically adding each of the devices bounds/rectangles together, that's just me though – MadProgrammer Dec 19 '12 at 19:25
3

You could use a transparent, undecorated frame in order to create a basic border.

public class ScreenRectangle extends JFrame {

    public ScreenRectangle() {
        this.setUndecorated(true);
        this.setBackground(new Color(0, 0, 0, 0.25F));
        // opacity ranges 0.0-1.0 and is the fourth paramater
        this.add(new DrawPanel());
    }

    private class DrawPanel extends JPanel {

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawRect(0, 0, this.getWidth(), this.getHeight());
            // any other drawing
        } 
    }
}

The frame may also need to be setOpaque, or the panel size may need to be handled, but this is the general idea of it.

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • Please call `super.paintComponent` ;) – MadProgrammer Dec 19 '12 at 08:35
  • @MadProgrammer Oops! Fixed that, thanks. (Although, would it actually matter in this case, because nothing else will be drawn on the JPanel? Maybe I'll test this later.) – FThompson Dec 19 '12 at 08:39
  • @Vulcan and there is no extra code required to create the changeable size right? `JFrame` will handle it itself? – An SO User Dec 19 '12 at 08:45
  • @Vulcan `paintComponent` is responsible for clearing the `Graphics` context, if you don't you could end up with paint artifacts from previously painting components – MadProgrammer Dec 19 '12 at 08:50