Java 如何将paintComponent(Graphics g)与MouseListener和MouseMotionListener相结合

Java 如何将paintComponent(Graphics g)与MouseListener和MouseMotionListener相结合,java,swing,jpanel,paintcomponent,mouselistener,Java,Swing,Jpanel,Paintcomponent,Mouselistener,因此,我尝试将paintComponent()的使用与MouseListener和MouseActionListener的使用结合起来,但运行它时会出现很多错误,如果我想要的话,它就不起作用了。具体来说,在这段代码中,我希望程序在按下、拖动并释放按钮时,获得按下的坐标,然后是释放的坐标,然后测量形状有多大,并绘制JComboBox指定的形状。我还有一个颜色选择器,按钮位于JFrame的底部。我想知道如何在不自动运行paintComponent()方法的情况下运行它,这样我就可以在它绘制之前给出它

因此,我尝试将paintComponent()的使用与MouseListener和MouseActionListener的使用结合起来,但运行它时会出现很多错误,如果我想要的话,它就不起作用了。具体来说,在这段代码中,我希望程序在按下、拖动并释放按钮时,获得按下的坐标,然后是释放的坐标,然后测量形状有多大,并绘制JComboBox指定的形状。我还有一个颜色选择器,按钮位于JFrame的底部。我想知道如何在不自动运行paintComponent()方法的情况下运行它,这样我就可以在它绘制之前给出它的规范,并让它按需绘制。另外,我想知道是否有其他方法可以做到这一点,我完全错误地认为我是如何做到这一点的。 我希望解释不要太混乱:)。任何帮助都很好,谢谢

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings({"unchecked", "rawtypes"})
public class GraphicGUI extends JPanel implements ActionListener  {

    HandlerClass handler = new HandlerClass();

    public int x1, x2, y1, y2, width, height;
    public String event;
    public JButton colorChooserButton;
    public Color color = (Color.WHITE);
    public JComboBox shapeBox;
    public JLabel eventLabel;


    public GraphicGUI(){
        shapeBox = new JComboBox();
        eventLabel = new JLabel();
        colorChooserButton = new JButton("Choose a color");
        colorChooserButton.addActionListener(this);

        shapeBox.addItem("Oval");
        shapeBox.addItem("Rectangle");
        shapeBox.addItem("Line");

        super.addMouseListener(handler);
        super.addMouseMotionListener(handler);
    }

    public void actionPerformed(ActionEvent arg0){
        if(arg0.getSource().equals(colorChooserButton)){
            color = JColorChooser.showDialog(null, "Pick Your Color", color);
            if(color==null){
                color = (Color.BLACK);
            }
        }
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.white);
        if(shapeBox.getSelectedItem() == "Oval")
            if(event.equals("released")){
                width = x1-x2;
                height = y1-y2;
                g.setColor(color);
                g.fillOval(x1, y1, width, height);
            }
    }

    private class HandlerClass implements MouseListener, MouseMotionListener{

            //Mouse Events
            public void mouseClicked(MouseEvent arg0){
                event = "click";
            }
            public void mousePressed(MouseEvent arg0){
                event = "pressed";
                x1 = arg0.getX();
                y1 = arg0.getY();
                eventLabel.setText(String.format("Mouse pressed at %d, %d", x1, y1));
            }
            public void mouseReleased(MouseEvent arg0){
                event = "released";
                x2 = arg0.getX();
                y2 = arg0.getY();
                eventLabel.setText(String.format("Mouse released at %d, %d", x2, y2));
            }
            public void mouseEntered(MouseEvent arg0){
            }
            public void mouseExited(MouseEvent arg0){
            }

            //Mouse Motion Events
            public void mouseDragged(MouseEvent arg0){
            }
            public void mouseMoved(MouseEvent arg0){
            }

    }
}

您应该开始绘制形状

public void paintComponent(Graphics g){
    //Draw the oval
    g.setColor(color);
    g.fillOval(x1, y1, width, height);
}

public void mouseReleased(MouseEvent arg0){
    event = "released";
    x2 = arg0.getX();
    y2 = arg0.getY();       
    if( x2 > x1 ) {
        width = x2-x1;
    } else {
        width = x1-x2;
    }       
    if( y2 > y1 ) {
        height = y2-y1;
    } else {
        height = y1-y2;
    }
    eventLabel.setText(String.format("Mouse released at %d, %d", x2, y2));  
}
然后(如果你想添加更多的形状),你需要一种添加形状的方法(duh),这样你就可以有一个形状列表并在该列表中迭代

你真的应该(必须)初始化你所有的变量


询问是否需要更多详细信息。:)

是的,问题是,我如何调用paintComponent方法,因为当我尝试执行g.SetColor()或g.Fillova()时,g会给我一个错误,因为它是一个空变量(不存在),所以我不能这样调用它:/I做了另一次编辑。我认为你遇到的问题是因为你有时有负维。这在理论上是可行的,但是,当你运行程序时,它将无法绘制椭圆,因为paintComponent()方法在MouseEvent之前运行,而事件不会触发它运行,所以,这就是我遇到的问题:/尝试添加“repaint()”在mousererelease()中,您应该更准确地说明您的代码现在正在做什么,即它是否崩溃,是否绘制了某些内容,等等。