如何禁用java.awt.Graphics.fillRect(int x,int y,int width,int height)的效果?

时间:2014-05-17 08:07:54

标签: java swing awt bufferedimage

原始图片: enter image description here

我使用java.awt.Graphics.fillRect(int x,int y,int width,int height)在图像上添加彩色矩形。

Graphics imageGraphics = image.createGraphics();
Color color = new Color(0,0,0,100);
imageGraphics.setColor(color);
imageGraphics.fillRect(0, 0, 800, 600);

因此图像已被反转,看起来像这样: enter image description here 之后,我想部分清除黑色透明矩形并显示原始图像。 imageGraphics.clearRect(100,100,100,100); 但效果是这样的:

enter image description here

我的要求是: enter image description here

我想知道为什么它不起作用,有没有其他方法可以实现它?

3 个答案:

答案 0 :(得分:4)

请记住,绘画具有破坏性。有可能使用AlphaComposite来实现这个结果,但更简单的解决方案可能是简单的建设性复合形状和涂料。

以下示例创建两个Rectangles,一个是我们要填充的区域,一个是我们要显示的区域,然后从第一个区域中减去第二个(创建窗口)然后结果被绘在图像的顶部

Clipped

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ShowArea {

    public static void main(String[] args) {
        new ShowArea();
    }

    public ShowArea() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            try {
                img = ImageIO.read(new File("sample.png"));
                Rectangle bounds = new Rectangle(0, 0, img.getWidth(), img.getHeight());
                Rectangle clip = new Rectangle(150, 10, 100, 100);

                Area area = new Area(bounds);
                area.subtract(new Area(clip));

                Graphics2D g2d = img.createGraphics();
                g2d.setColor(Color.BLACK);
                g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
                g2d.fill(area);
                g2d.dispose();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) :  new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - img.getWidth()) / 2;
            int y = (getHeight() - img.getHeight()) / 2;
            g2d.drawImage(img, x, y, this);
            g2d.dispose();
        }

    }

}

如果我正在进行某种绘画程序,我会在paintComponent方法中执行此操作,或以某种方式执行此操作,使其不影响原始图像,否则您基本会将图像破坏,直到您重新加载

另一个解决方案可能是复制一份您想要保留的原始区域,然后将其重新打印回来,例如......

img = ImageIO.read(new File("sample.png"));
// This is the portion of the image we want to save...
BufferedImage cutout = img.getSubimage(150, 10, 100, 100);
// This is the area we want to paint over...
Rectangle bounds = new Rectangle(0, 0, img.getWidth(), img.getHeight());

Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.BLACK);
// Save the current Composite so we can reset it...
Composite comp = g2d.getComposite();
// Apply the composite and fill the area...
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.fill(area);
// Reset the composite 
g2d.setComposite(comp);
// Draw the part of the image we saved previously...
g2d.drawImage(cutout, 150, 10, this);
g2d.dispose();

答案 1 :(得分:1)

你无法按照自己的方式去做。

可以通过创建一个填充了Color(0,0,0,200)的BufferedImage来完成,然后在该图像中绘制颜色为Color(0,0,0,200)的矩形,然后将其应用于图像。 请记住,绘制filledRectange不是"撤消操作" - 它写在像素上,无法撤消。

修改

        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

答案 2 :(得分:1)

是的,这很容易实现。问题是任何绘制操作都是破坏性的,你所看到的就是你得到的,所绘制的信息就会丢失。

存储子图像的地方,执行绘制操作然后将子图像重新绘制在顶部。

public class Q23709070 {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Panel p = new Panel();

        frame.getContentPane().add(p);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class Panel extends JPanel {
        int w = 400;
        int h = 400;
        int x = 100;
        int y = 100;
        BufferedImage img;
        BufferedImage subImg;
        BufferedImage save;

        public Panel() {
            try {
                img = ImageIO.read(getClass().getResourceAsStream("0qzCf.jpg"));
            } catch (IOException e) {
            }
            subImg = img.getSubimage(x, y, w, h);

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            Color g2dColor = g2d.getColor();
            Color fillColor = new Color(0, 0, 0, 100);

            g2d.drawImage(img, 0, 0, null);
            g2d.setColor(fillColor);
            g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
            g2d.drawImage(subImg, x, y, null);
            g2d.setColor(g2dColor);

            if (save == null) {
                save = new BufferedImage(img.getWidth(), img.getHeight(),
                        img.getType());
                this.paint(save.getGraphics());
                try {
                    ImageIO.write(save, "jpg", new File("save.jpg"));
                } catch (IOException e) {
                }
            }

        }

        @Override
        @Transient
        public Dimension getPreferredSize() {
            return new Dimension(img.getWidth(), img.getHeight());
        }
    }
}

渲染enter image description here