如何触发多个 MouseEvent 事件

How to trigger multiple MouseEvent events

我有一个 class,点击它会绘制一个点,再次单击它会绘制另一个点并在它们之间画一条线。

public class SlopeComponent extends JComponent
{
private static final long serialVersionUID = 1L;
public SlopeComponent()
{
    point1 = null;
    point2 = null;
    class MouseSpy extends MouseAdapter
    {  
        public void mousePressed(MouseEvent event)
        {
            double x1 = rxPixel(event.getX());
            double y1 = ryPixel(event.getY()); 
            point1 = new Point2D.Double(x1, y1);

            double x2 = rxPixel(event.getX());
            double y2 = ryPixel(event.getY()); 
            point2 = new Point2D.Double(x2, y2);

            repaint();
        }
    } 
    MouseSpy listener = new MouseSpy ();
    addMouseListener(listener);
}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    Axes axes = new Axes(xPixel(XMIN), xPixel(XMAX), yPixel(YMIN), yPixel(YMAX),
            xPixel(0), yPixel(0), sWidth(1), sHeight(1));
    axes.drawAxes(g2);          //draw the axes
    axes.drawTicks(g2);

    if(point1 != null || point2 != null)
    {
        plotPoint(g2, point1);
        plotPoint(g2, point2);
        double x1 = point1.getX();
        double y1 = point1.getY();
        double x2 = point2.getX();
        double y2 = point2.getY();
        drawSlope(g2, x1, y1, x2, y2);
    }
}
public void drawSlope(Graphics2D g2, double x1, double y1, double x2, double y2)    //draw the lines
{
    Point2D.Double p1 = new Point2D.Double(xPixel(x1), yPixel(y1));
    Point2D.Double p2 = new Point2D.Double(xPixel(x2), yPixel(y2));
    Line2D.Double line = new Line2D.Double(p1, p2);
    g2.draw(line);
}
public void plotPoint(Graphics2D g2, Point2D.Double p)                  //plot the point
{
    double x = p.getX();
    double y = p.getY();
    double radius = 5 * (XMAX - XMIN) / getWidth();
    Ellipse2D.Double point = new Ellipse2D.Double(xPixel(x - radius), yPixel(y + radius),
            sWidth(2 * radius), sHeight(2 * radius));
    g2.fill(point);
    double xR = Rounding.round(x, 1);
    double yR = Rounding.round(y, 1);
    double gap = 6 * (XMAX - XMIN) / getWidth();
    g2.drawString("(" + xR + ", " + yR + ")", (float)xPixel(x + gap), (float)yPixel(y + gap));  
}
public double rxPixel(double x)
{
    return x * (XMAX - XMIN) / (getWidth() - 1) + XMIN;
}
public double ryPixel(double y)
{
    return y * (YMIN - YMAX) / (getHeight() - 1) + YMAX;
}
public double xPixel(double xuser)
{
    return (xuser - XMIN) * (getWidth() - 1) / (XMAX - XMIN);
}
public double yPixel(double yuser)
{
    return (yuser - YMAX) * (getHeight() - 1) / (YMIN - YMAX);
}
public double sHeight(double yuser)
{
    return yuser * (getHeight() - 1) / (YMAX - YMIN);
}
public double sWidth(double xuser)
{
    return xuser * (getWidth() - 1) / (XMAX - XMIN);
}
private static final double XMIN = -10;
private static final double XMAX = 10;
private static final double YMIN = -10;
private static final double YMAX = 10;
private Point2D.Double point1;
private Point2D.Double point2;
}

但是,当我尝试 运行 时,它会在彼此上方绘制两个点,然后在其上方绘制一条线。我知道在我的构造函数中我在同一事件上调用 getX() 和 getY() 以便 point1 和 point2 具有坐标。我该如何调用多个事件,以免发生这种情况。

你可以统计点击次数,你可以这样做

int count = 0;
public void mousePressed(MouseEvent event)
    {
      if(count == 0){
        double x1 = rxPixel(event.getX());
        double y1 = ryPixel(event.getY()); 
        point1 = new Point2D.Double(x1, y1);
        count++;
      }
      if(count == 1){
        double x2 = rxPixel(event.getX());
        double y2 = ryPixel(event.getY()); 
        point2 = new Point2D.Double(x2, y2);
        repaint();
      }
    }