Drawing a bounding rectangle to select what area to record

后端 未结 3 1289
别跟我提以往
别跟我提以往 2020-11-29 09:05

\"bound\"

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

3条回答
  •  一向
    一向 (楼主)
    2020-11-29 09:31

    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) );
    }
    

提交回复
热议问题