如何用鼠标绘制多边形(三角形和五边形)?

How to draw a Polygon (triangle and pentagon) with a mouse?

如果我想用鼠标绘制多边形,使用 getX() 和 getY() 如何找到 xpoints[] 和 ypoints[]?

我现在的代码是:

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


public class Poligonos extends Figura{
    public void Poligonos (int[] xPoints, int[] yPoints, int nPoints){
        //private int[] xPoints = {(x1/2), x1, (x1+(x1/2))}  // {(getX()/2), getX(), (getX()+(getX()/2))};
        //private int[] yPoints = {( y1 + y1 ), y1 ,( y1 + y1 )};

    }
    @Override   
    public void desenha(Graphics g) {
        g.setColor(cor);
        g.drawPolygon(  xPoints, yPoints, 3);
    }
    @Override
    public void setCoordenadas(int x1, int y1, int x2, int y2) {
        p.x = Math.min(x1, x2);
        p.y = Math.min(y1, y2);

        int xPoints[] = {(p.x /2), p.x , ( p.x +( p.x /2))};  // {(getX()/2), getX(), (getX()+(getX()/2))};
        int yPoints[] = {( p.y + p.y ), p.y ,( p.y + p.y )};
    }
}

getX() 和 getY() 部分是:

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

       @Override
       public void mouseDragged(MouseEvent e) {
           x2 = e.getX();
           y2 = e.getY();
           r.setCoordenadas(x1, y1, x2, y2);
           pEdicao.repaint();
       }

我怎样才能完成这项工作?我只想用鼠标画一个五边形和一个三角形

感谢您的宝贵时间。

你可以做一个设置,你可以做类似的事情

int numCoordinates = /*(get the number of coordinates by getting number of clicks after a certain action, etc.)*/
int[] xCords = new int[numCoordinates];
int[] yCords = new int[numCoordinates];


xCords[index] = e.getX();
yCords[index] = e.getY();
index++;

if(index > numCoordinates){
    index = 0;
    pEdicao.repaint();
}