package com.lovo;
/**
* 点
* @author XWJ
*
*/
public class Point {
private int x; // 横坐标
private int y; // 纵坐标
public Point() {
this(0, 0);
}
public Point(int x, int y) {
this.moveTo(x, y);
}
public Point(final Point other) {
this.moveTo(other.x, other.y);
}
/**
* 移动到某个坐标
* @param x 指定的横坐标
* @param y 指定的纵坐标
*/
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
}
/**
* 移动指定的偏移量
* @param dx 横坐标的偏移量
* @param dy 纵坐标的偏移量
*/
public void moveBy(int dx, int dy) {
x += dx;
y += dy;
}
/**
* 计算与另一个点的距离
* @param other 另一个点
* @return 两点间的距离
*/
public double distance(final Point other) {
return Math.sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
package com.lovo;
/**
* 线段
* @author XWJ
*
*/
public class Line {
private Point start; // 起点
private Point end; // 终点
public Line() {
start = new Point();
end = new Point();
}
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
public Line(int x1, int y1, int x2, int y2) {
start = new Point(x1, y1);
end = new Point(x2, y2);
}
/**
* 获得线段长度
*/
public double length() {
return start.distance(end);
}
/**
* 判断是否与另一线段平行
* @param other 另一线段
* @return 平行返回true否则返回false
*/
public boolean isParallel(final Line other) {
if(start.getX() == end.getX()) {
return other.start.getX() == other.end.getX();
}
else {
double slope1 = (double)(start.getY() - end.getY()) / (start.getX() - end.getX());
double slope2 = (double)(other.start.getY() - other.end.getY()) / (other.start.getX() - other.end.getX());
return slope1 == slope2;
}
}
}
package com.lovo;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private List<Point> list = new ArrayList<Point>();
private boolean isOK = false;
public MyFrame() {
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
isOK = true;
}
});
okButton.setBounds(680, 500, 60, 30);
this.add(okButton);
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(isOK) {
isOK = false;
list.clear();
}
Point temp = new Point(e.getX(), e.getY());
list.add(temp);
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
for(int i = 0; i < list.size(); i++) {
Point p1 = list.get(i);
Point p2 = list.get((i + 1) % list.size()); // 判断下标及最后一个点与第一个点连接
g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new MyFrame().setVisible(true);
}
}