简体   繁体   English

更改 Graphics2D 形状

[英]Change Graphics2D Shape

Hey I want to create program where u can draw multishape like Circle,Rectagle,Line.When i clicked circle button and drew Circle(1) and then when i Clicked Rectangle it should draw only Rectangle, not Circle and Rectangle(2),When i Clicked Line its should draw only Line not Circle and Rectangle and Line(3).嘿,我想创建一个程序,您可以在其中绘制圆形,矩形,线条等多形。当我单击圆形按钮并绘制圆形(1)然后当我单击矩形时,它应该只绘制矩形,而不是圆形和矩形(2),当我点击了 Line 它应该只绘制 Line 而不是 Circle 和 Rectangle 和 Line(3)。

在此处输入图像描述

public class Images extends JComponent {


    private Image image;

    private Graphics2D g2;

    private int currentX, currentY, oldX, oldY;

    public Images() {
        setDoubleBuffered(false);
    }

    protected void paintComponent(Graphics g) {
        if (image == null) {

            image = createImage(getSize().width, getSize().height);
            g2 = (Graphics2D) image.getGraphics();

            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            clear();
        }
        g.drawImage(image, 0, 0, null);
    }


    public void line() {
        currentX = 0;
        currentY = 0;
        oldX = 0;
        oldY = 0;

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                oldX = e.getX();
                oldY = e.getY();
            }
        });

        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();
                g2.draw(new Line2D.Double(oldX, oldY, currentX, currentY));
                repaint();
            }
        });
    }

    public void rectangle() {
        currentX = 0;
        currentY = 0;
        oldX = 0;
        oldY = 0;
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                oldX = e.getX();
                oldY = e.getY();
            }
        });
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();
                g2.draw(new Rectangle2D.Double(oldX, oldY, currentX - oldX, currentY - oldY));
                repaint();
            }
        });
    }
    public void circle() {
        currentX = 0;
        currentY = 0;
        oldX = 0;
        oldY = 0;
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                oldX = e.getX();
                oldY = e.getY();
            }
        });
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();
                g2.draw(new Ellipse2D.Double(oldX, oldY, (currentX - oldX), (currentX - oldX)));
                repaint();
            }});}}

You are getting all your previously selected shapes because their mousedown/mouseup events are still bound.您将获得所有之前选择的形状,因为它们的 mousedown/mouseup 事件仍然是绑定的。

A straightforward way to solve this would be to keep track of your MouseAdapters (add them to a list when you bind them) and remove them (subsequently clearing the list) when you switch tools.解决此问题的一种直接方法是跟踪您的 MouseAdapter(绑定它们时将它们添加到列表中)并在切换工具时删除它们(随后清除列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM