Java:用鼠标画一个不规则图形 有没有鼠标点击第二个点,第一个点与第
Java:用鼠标画一个不规则图形有没有鼠标点击第二个点,第一个点与第二个点相连,点击第三个点时第二个点与第三个点相连,以此类推,直到第一个点与最后一个点重合就停止,就像这...
Java:用鼠标画一个不规则图形
有没有鼠标点击第二个点,第一个点与第二个点相连,点击第三个点时第二个点与第三个点相连,以此类推,直到第一个点与最后一个点重合就停止,就像这样。有木有源代码呀。我用mouseclicked获取点的时候没办法将这些点分开 展开
有没有鼠标点击第二个点,第一个点与第二个点相连,点击第三个点时第二个点与第三个点相连,以此类推,直到第一个点与最后一个点重合就停止,就像这样。有木有源代码呀。我用mouseclicked获取点的时候没办法将这些点分开 展开
1个回答
展开全部
package pos.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo extends JFrame implements MouseListener {
private JPanel drawPanel;
private Graphics2D g2;
private point uppoint;
private point firstpoint;
private int count = 0;
public Demo() {
setTitle("多边形绘图测试");
setBounds(200, 200, 600, 400);
drawPanel = new JPanel();
drawPanel.addMouseListener(this);
setContentPane(drawPanel);
setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
g2 = (Graphics2D) drawPanel.getGraphics();
if (firstpoint == null) {
firstpoint = new point(e.getX(), e.getY());
uppoint = firstpoint;
} else {
System.out.println(uppoint);
point nowpoint = new point(e.getX(), e.getY());
if (count > 2) {
if (nowpoint.getX() - firstpoint.getX() <= 10 && nowpoint.getY() - firstpoint.getY() <= 10) {
nowpoint = firstpoint;
firstpoint = null;
}
}
g2.setColor(Color.RED);
g2.drawLine(uppoint.getX(), uppoint.getY(), nowpoint.getX(), nowpoint.getY());
uppoint = nowpoint;
}
count++;
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
class point {
int x;
int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Point:[X:" + x + ";Y:" + y + "]";
}
}
public static void main(String[] args) {
new Demo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询