Java anti fillRect(填充所述矩形之外的所有内容)

时间:2014-08-25 02:47:55

标签: java graphics2d

在Java中,有Graphics2D.fillRect(x,y,width,height)函数。在我的计划中,我正在寻找类似但完全相反的东西。

我需要填充屏幕上的所有内容,除了这个特定的x,y,宽度,高度,类似于反fillRect。是否有我忽略的内置功能,或者你能指出我正确的方向创建一个吗?

不是必需的,但如果它可以与其他Java 2D形状一起使用,那将是一个很好的奖励。

1 个答案:

答案 0 :(得分:4)

有几种方法可以实现,最简单的方法是使用 java.awt.geom.Area这是一个高度通用的Shape实现,允许您在其中添加和减去Area,例如......

Clip

我将填充色略微透亮以证明这一点;)

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 java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CutExample {

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

    public CutExample() {
        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(...);
            } catch (IOException ex) {
                Logger.getLogger(CutExample.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @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();
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getWidth()) / 2;
                g2d.drawImage(img, x, y, this);

                Area outter = new Area(new Rectangle(0, 0, getWidth(), getHeight()));
                x = (getWidth() / 2) - 100;
                y = (getHeight() / 2) - 100;
                Rectangle inner = new Rectangle(x, y, 200, 200);
                outter.subtract(new Area(inner));

                g2d.setColor(new Color(0, 0, 0, 192));
                g2d.fill(outter);
            }
            g2d.dispose();
        }

    }

}

我还应该提一下,你可以使用你想要的任何其他Shape ......