如何删除在图像上绘制的形状

时间:2016-06-14 09:43:04

标签: java swing graphics2d eraser

我正在开发一个小型Paint工具。我能够在图像上加载和绘制线条或圆形和其他形状。我还有一个橡皮擦工具来擦除我绘制的形状 这是代码:

g.setColor(getColor().WHITE);
g.fillRect(getXAxis() - getThickness(), getYAxis() - getThickness(), getThickness() * 2, getThickness() * 2);

我的问题是,如果我已经加载了图像并在其上绘制了一些形状。然后,当我试图擦除形状时,图像也会被白色替换 有没有办法在使用fillRect()擦除形状时将图像设置为背景,这样我的图像就不会受到影响。

2 个答案:

答案 0 :(得分:2)

这是一个例子。要测试它,您需要用背景图像替换我的图像。

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/**
 * <code>PaintTryout</code>.
 *
 * @author smedvynskyy
 */
public class PaintPanel extends JPanel {

    private Image backgroundImage;

    private BufferedImage paintImage;

    public PaintPanel() {
        try {
            // replace this image with your image
            backgroundImage = ImageIO.read(new File("E:\\icons\\blackboard.png"));
            paintImage = new BufferedImage(backgroundImage.getWidth(this), 
                    backgroundImage.getHeight(this), BufferedImage.TYPE_INT_ARGB);
        } catch (final Exception e) {
            e.printStackTrace();
        }

    }

    public void fillRect() {
        final Graphics g = paintImage.createGraphics();
        g.setColor(Color.RED);
        g.fillRect(0, 0, 50, 50);
        g.dispose();
        repaint();
    }

    public void clearRect() {
        final Graphics2D g = paintImage.createGraphics();
        g.setColor(new Color(0, 0, 0, 0));
        g.setComposite(AlphaComposite.Clear); // overpaint
        g.fillRect(0, 0, 50, 50);
        g.dispose();
        repaint();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(backgroundImage, 0, 0, this);
        g.drawImage(paintImage, 0, 0, this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(backgroundImage.getWidth(this), 
                backgroundImage.getHeight(this));
    }

    public static void main(String[] args) {
        final JFrame frm = new JFrame("Tesp paint");
        final PaintPanel p = new PaintPanel();
        frm.add(p);
        final JPanel buttons = new JPanel();
        final JButton fill = new JButton("Fill Rect");
        fill.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                p.fillRect();
            }
        });
        final JButton clear = new JButton("Clear Rect");
        clear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                p.clearRect();
            }
        });
        buttons.add(fill);
        buttons.add(clear);
        frm.add(buttons, BorderLayout.SOUTH);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

答案 1 :(得分:0)

这样做的简单方法是在XOR模式下绘制线条。然后,要删除它们,您只需再次绘制它们。