Drawing a bounding rectangle to select what area to record

后端 未结 3 1297
别跟我提以往
别跟我提以往 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:16

    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.

提交回复
热议问题