鼠标定坐标连线画图(面向对象)

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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值