1

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

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

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

4

2 回答 2

3

这是示例。要对其进行测试,您需要将我的图像替换为您的背景图像。

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);
    }
}
于 2016-06-14T11:19:07.077 回答
0

执行此操作的简单方法是在 XOR 模式下绘制线条。然后,要擦除它们,您只需再次绘制它们。

于 2016-06-14T11:40:39.113 回答