How to disable java.awt.Graphics.fillRect(int x, int y, int width, int height)'s effect?

前端 未结 3 789
一生所求
一生所求 2020-12-21 18:13

It\'s the original image: \"enter

I use java.awt.Graphics.fillRect(int x, int y, int

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 18:52

    You cant do that the way you're trying to.

    It can be done by creating a BufferedImage filled with Color(0,0,0,200), then in that image draw rectangle with color Color(0,0,0,200) and then apply it on image. Remember, that drawing filledRectange is not a "undo operation"- it is written on pixels and it can't be undone.

    EDIT

            Icon imageIcon = new javax.swing.ImageIcon("image.jpg");
            BufferedImage mask = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            BufferedImage image = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            imageIcon.paintIcon(null, image.getGraphics(), 0, 0);
            Graphics maskGraphics = mask.getGraphics();
            //drawing grey background
            maskGraphics.setColor(new Color(0, 0, 0, 120));
            maskGraphics.fillRect(0, 0, mask.getWidth(), mask.getHeight());
            //drawing black frame
            maskGraphics.setColor(new Color(0, 0, 0, 255));
            maskGraphics.drawRect(99, 99, 301, 301);
            //drawing original image window
            maskGraphics.drawImage(image, 100, 100, 400, 400, 100, 100, 400, 400, null);
            //apply mask on image
            new ImageIcon(mask).paintIcon(null, image.getGraphics(), 0, 0);
            //result presentation
            label.setIcon(new ImageIcon(image));
    

    Result

提交回复
热议问题