Java anti fillRect(填充所述矩形之外的所有内容) [英] Java anti fillRect (fill everything outside of said rectangle)
问题描述
在Java中,有Graphics2D.fillRect(x,y,width,height)函数.在我的程序中,我正在寻找相似但完全相反的东西.
In Java, there is the Graphics2D.fillRect(x, y, width, height) function. In my program, I am looking for something similar, yet completely opposite.
我需要填充屏幕上的所有内容,除了某些x,y,宽度,高度之类的东西(类似于anti-FillRect).有没有我可以忽略的内置函数,或者您可以指出正确的方向来创建函数吗?
I need to fill everything on the screen except this certain x, y, width, height, sort of like an anti-fillRect. Is there a built in function that I am overlooking, or can you point me in the right direction to create one?
不是必需的,但是如果它可以与其他Java 2D形状一起使用,那将是一个不错的奖励.
Not required, but it would be a nice bonus if it could work with other Java 2D shapes.
推荐答案
有几种方法可以实现,最简单的方法就是使用
java.awt.geom.Area
是用途广泛的Shape
实现,例如,您可以从中添加和减去Area
...
There are a few ways that might be achieved, the easiest might be to use
java.awt.geom.Area
which is a highly versatile Shape
implementation, which allows you to add and subtract Area
s from it, for example...
我使填充色略透明,以证明这一点;)
I've left the fill color slightly translucent to prove the point ;)
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
...
I should also mention that you can use any other Shape
you want...
这篇关于Java anti fillRect(填充所述矩形之外的所有内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!