java fillrect,Java anti fillRect(填充矩形以外的所有内容)

In Java, there is the Graphics2D.fillRect(x, y, width, height) function. In my program, I am looking for something similar, yet completely opposite.

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?

Not required, but it would be a nice bonus if it could work with other Java 2D shapes.

解决方案

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 Areas from it, for example...

HhNBq.png

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();

}

}

}

I should also mention that you can use any other Shape you want...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值