如何禁用java.awt.Graphics.fillRect(INT X,INT Y,INT宽度,高度INT)的影响? [英] How to disable java.awt.Graphics.fillRect(int x, int y, int width, int height)'s effect?

查看:371
本文介绍了如何禁用java.awt.Graphics.fillRect(INT X,INT Y,INT宽度,高度INT)的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是原始图像:

It's the original image:

我用java.awt.Graphics.fillRect(INT X,INT Y,INT宽度,高度INT),以在图像上添加一个彩色矩形。

I use java.awt.Graphics.fillRect(int x, int y, int width, int height) to add a coloured rectangle on the image.

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

所以图像已被颠倒,看起来像这样:

在那之后,我想部分清除黑色透明的矩形,并显示原始图像。
imageGraphics.clearRect(100,100,100,100);
但效果是这样的:

So the image has been inverted and looks like this: After that,I want to clear the black transparent rectangle partly and show the original image. imageGraphics.clearRect(100,100,100,100); But the effect is like this:

什么我的要求是:

What my requirement is:

我想知道为什么它不工作,是有去实现它任何其他方式?

I want to know why it doesn't work and is there any other way to realize it?

推荐答案

记住,绘画是破坏性的。这也许可以使用的AlphaComposite 来实现这一结果,但一个简单的解决方案可能是简单建设性的复合形状和油漆的替代。

Remember, painting is destructive. It might be possible to use AlphaComposite to achieve this result, but a simpler solution might be to simple constructive a compound shape and paint that instead.

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

The following example creates two Rectangles, one been the area we want to fill and one been the area we want to show, the second is then subtracted from the first (to create the window) and then the result is painted on top of the image

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 方法或以某种方式,它不影响原始图像,否则你已经基本被破坏的形象,直到你重新加载它

If I were doing some kind of paint program, I would do this all within the paintComponent method or in some way that it didn't effect the original image, otherwise you've basic destroyed the image until you re-load it

另一种解决方案可能是把你想保留后重绘其重回巅峰,原面积的副本,例如...

Another solution might be to take a copy of the original area you want to keep and repaint it back on top after, for example...

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

这篇关于如何禁用java.awt.Graphics.fillRect(INT X,INT Y,INT宽度,高度INT)的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆