如何删除图像中绘制的形状?

时间:2019-04-09 01:17:38

标签: java image swing jpanel draw

我有一个图像,当我用鼠标左键单击时,它会绘制一个Rectangle。我将LinkedList放在每个绘制的形状上,因此我可以在需要时擦除(实际上是在我右键单击鼠标按钮时)。调用方法paint()repaint()时,应包含或删除形状。包括是可以的,但不能删除:(。

这是该问题的一小段时间

package classes;

import java.awt.HeadlessException;
import javax.swing.JFrame;
import views.ImagePanel;

/**
 *
 * @author Luis
 */
public class Test extends JFrame{

    private ImagePanel imgPanel;

    public Test() throws HeadlessException {

        imgPanel = new ImagePanel();
        imgPanel.addMouseListener(imgPanel);
        imgPanel.addMouseWheelListener(imgPanel);

        setTitle("Test frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        add(imgPanel);

    }

    public static void main(String[] args) {
        Test test = new Test();
        test.setVisible(true);
    }

}

还有ImagePanel class

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

/**
 *
 * @author Luis
 */
public class ImagePanel extends JPanel implements MouseListener,MouseWheelListener{

    private BufferedImage imagem = null; 

    private double txi=0;   //Series of variables to do some transformations
    private double tyi=0;   //
    private double txf=0;   //
    private double tyf=0;   //

    private double final_translx=0; //
    private double final_transly=0; //

    private double zoomFactor = 1;  //

    private LinkedList<Shape> shapes; //List of all shapes added

    private boolean zoomer = false; //

    public ImagePanel() {
        //Initializing the list and doing some config.
        shapes = new LinkedList<>();

        setBorder(new LineBorder(Color.BLACK,1));
        setLayout(new BorderLayout());
        setCursor(new java.awt.Cursor(java.awt.Cursor.MOVE_CURSOR));
        try {
            //This image has 981x651, but you can replace by any other
            imagem = ImageIO.read(getClass().getResource("/sources/image.png"));
        } catch (IOException ex) {
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g); //To change body of generated methods, choose Tools | Templates.

        //Doing the transformations of the jPanel inside the jFrame (possibly)
        Graphics2D panelGraphics = (Graphics2D) g;

        AffineTransform at = AffineTransform.getScaleInstance(zoomFactor, zoomFactor);
        panelGraphics.drawImage(imagem, at, this);

        //Adding shapes in the image
        Graphics2D imageGraphics = (Graphics2D)imagem.getGraphics();

        imageGraphics.setColor(Color.red);
        for (Shape s : shapes){
            //This is where i think it should overwrite all the shapes
            imageGraphics.draw(s);
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(SwingUtilities.isLeftMouseButton(e)){    
            //creating a new shape when i left click the mouse
            shapes.add(new Rectangle(Math.toIntExact((long) (e.getX()/zoomFactor-final_translx)),Math.toIntExact((long) (e.getY()/zoomFactor-final_transly)), 100, 100));
        }
        else{
            if(shapes.size()>0){
                //the part when it should remove;
                shapes.removeLast();
            }
        }
        repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //Initial point to calculate the translation
        if(SwingUtilities.isLeftMouseButton(e)){
            txi = e.getX()/zoomFactor;
            tyi = e.getY()/zoomFactor; 
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if(SwingUtilities.isLeftMouseButton(e)){
            txf = e.getX()/zoomFactor;
            tyf = e.getY()/zoomFactor;
            final_translx += txf-txi;
            final_transly += tyf-tyi;
            //final translation
            repaint();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //do nothing
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //do nothing
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        zoomer=true;
        zoomFactor += e.getWheelRotation()*0.01;
        repaint();
    }
}

这的最终目的是在将来添加气球并在用户想要的区域中插入信息! 关于如何正确删除这些形状的任何想法? 我也愿意接受任何其他建议!

预先感谢

1 个答案:

答案 0 :(得分:0)

最简单的方法可能是重新绘制(加载)原始图像,再次绘制所有形状并忽略要删除的形状。