![](/img/trans.png)
[英]Draw a triangle using mouse drag (how can I move the previous drawn triangle using mouse drag)
[英]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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.