【问题标题】:How to erase a shape drawn in an image?如何擦除图像中绘制的形状?
【发布时间】:2019-04-09 01:17:38
【问题描述】:

我有一张图片,当我点击左键时,它会绘制一个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);
    }

}

还有ImagePanelclass

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

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

提前致谢

【问题讨论】:

  • 如果你在图像上画画就可以了!它永远留在那里-您需要写入面板的实际 g
  • 重绘并仅绘制您想要的剩余部分。也不要覆盖paint,而是paintComponenet

标签: java image swing jpanel draw


【解决方案1】:

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

【讨论】:

  • 是的,现在我明白了。它现在工作正常,但每次我重新绘制时,我都必须从文件中加载图像。从长远来看,这可能不是最好的方式......感谢所有帮助过我的人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
相关资源
最近更新 更多