Java 如何在第二次单击后在画布上绘制形状

Java 如何在第二次单击后在画布上绘制形状,java,Java,我想如果我按住鼠标的同时拖动鼠标来绘制形状,但是我需要能够在画布上单击以设置第一个点,然后在画布上单击第二次以设置第二个坐标,然后它将绘制形状 Canvas类扩展了java.awt.Canvas实现了MouseListener、MouseMotionListener{ //形状常数 公共静态最终整数圆=1; 公共静态最终整数矩形=2 // Coordinates of points to draw private int x1, y1, x2, y2; // shape to draw pri

我想如果我按住鼠标的同时拖动鼠标来绘制形状,但是我需要能够在画布上单击以设置第一个点,然后在画布上单击第二次以设置第二个坐标,然后它将绘制形状

Canvas类扩展了java.awt.Canvas实现了MouseListener、MouseMotionListener{ //形状常数 公共静态最终整数圆=1; 公共静态最终整数矩形=2

// Coordinates of points to draw
private int x1, y1, x2, y2;

// shape to draw
private int shape = CIRCLE;
private final int PAINT = 0; // do we need to draw current component
private final int NOPAINT = 1; // we dont need to draw current componet
private int paintOrRepaint = NOPAINT;// at the beginning we dont need to draw

public void setShape(int shape) {
    this.shape = shape;
}

// filled color
private Color filledColor = null;


public void setFilledColor(Color color) {
    filledColor = color;
}


public Canvas() {
    addMouseListener(this);
    addMouseMotionListener(this);
} // end of constructor


@Override
public void paint(Graphics g) {
    // the drawing area
    int x, y, width, height;

    // determine the upper-left corner of bounding rectangle
    x = Math.min(x1, x2);
    y = Math.min(y1, y2);

    // determine the width and height of bounding rectangle
    width = Math.abs(x1 - x2);
    height = Math.abs(y1 - y2);

    // current shape(cur_shape) : by default it is a line after using a switch we update this to
    // circle of rectangle
    Shape cur_shape = new Line2D.Double(x1, y1, x2, y2);
    switch (shape) {
        case RECTANGLE :
            cur_shape = new Rectangle2D.Double(x, y, width, height);
            break;
        case CIRCLE :
            int diameter = Math.max(width, height);
            cur_shape = new Ellipse2D.Double(x,y, diameter, diameter);
            break;
    }


    if(paintOrRepaint == PAINT){
        Figure sh;
        if(shape == RECTANGLE) {
            sh = new Rectangle(cur_shape,filledColor, shape);
            GUI.shapes.add(sh);
        }
        else if(shape == CIRCLE) {
            sh = new Circle(cur_shape,filledColor, shape);
            GUI.shapes.add(sh);
        }
    }

    Graphics2D graphics = (Graphics2D) g;
    for (Figure s : GUI.shapes) {

        if(s.fill != null)
            g.setColor(s.fill);
        else
            g.setColor(Color.BLACK);
        if(s.fill != null)
            graphics.fill(s.shape);
        else
            graphics.draw(s.shape);
    }
    GUI.updateShapes();
    setPaintOrRepaint(NOPAINT);


    GUI.selectColor = false;
    GUI.selectShape = false;
}


@Override
public void mousePressed(MouseEvent event) {
    x1 = event.getX();
    y1 = event.getY();

}

@Override
public void mouseReleased(MouseEvent event) {
    x2 = event.getX();
    y2 = event.getY();

    setPaintOrRepaint(PAINT);
    repaint();

}

@Override
public void mouseClicked(MouseEvent evt) { }
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}

@Override
public void mouseDragged(MouseEvent event) {
    x2 = event.getX();
    y2 = event.getY();
}

@Override
public void mouseMoved(MouseEvent e) {}

private void setPaintOrRepaint(int option) {
    paintOrRepaint = option;
}

}您的点需要存储在数组或列表中。因此,您要做的是使用
MouseListener
MouseMotionListener
(使用
MouseAdapter

对于第一点和其他点,请在
mouseClicked()
中进行设置。单击鼠标时,将该点存储在选择的数据结构中

如果你拖动鼠标,你会得到一个点流,所以我不知道这有多有用(除非你想画线并保存坐标)

如果使用内部类并扩展
MouseAdapter
,则可以摆脱独立实现侦听器的混乱。由于
MouseAdapter
使用空方法实现两个侦听器,因此您只需重写所需的侦听器,然后使用自己的
MouseListener
实例

类MyMouseListener扩展了MouseAdapter{
//您覆盖的方法在这里。
//你只需要覆盖你需要的。
}
然后

MyMouseListener ml=new MyMouseListener();
addMouseListener(ml);
addMouseMotionListener(ml);
我不确定您是如何绘制的,但通常是在
JPanel
上完成的,它包含在
JFrame
中。在
JPanel
中,您将覆盖
paintComponent(g)
,如下所示

公共组件(图形g){
超级组件(g);
//你的东西在这里
}

您的点需要存储在数组或列表中。因此,您要做的是使用
MouseListener
MouseMotionListener
(使用
MouseAdapter

对于第一点和其他点,请在
mouseClicked()
中进行设置。单击鼠标时,将该点存储在选择的数据结构中

如果你拖动鼠标,你会得到一个点流,所以我不知道这有多有用(除非你想画线并保存坐标)

如果使用内部类并扩展
MouseAdapter
,则可以摆脱独立实现侦听器的混乱。由于
MouseAdapter
使用空方法实现两个侦听器,因此您只需重写所需的侦听器,然后使用自己的
MouseListener
实例

类MyMouseListener扩展了MouseAdapter{
//您覆盖的方法在这里。
//你只需要覆盖你需要的。
}
然后

MyMouseListener ml=new MyMouseListener();
addMouseListener(ml);
addMouseMotionListener(ml);
我不确定您是如何绘制的,但通常是在
JPanel
上完成的,它包含在
JFrame
中。在
JPanel
中,您将覆盖
paintComponent(g)
,如下所示

公共组件(图形g){
超级组件(g);
//你的东西在这里
}