“低配版”win10画图板的实现

下面总结一下画图板的实现!
![在这里插入图片描述](https://img-blog.csdn在这里插入图片描述
工程目录如图:
在这里插入图片描述
代码如下:

/**
 * 简易画板的实现
 * @author 杨飞
 *
 */
public class DrawFrame extends JFrame {

	private Shape[] shapeArray = new Shape[100000];
	private int time = 0;

	public void showUI() {
		this.setSize(1000, 700);// 大小
		this.setTitle("杨飞的画板");// 标题
		this.setDefaultCloseOperation(3);// 退出
		this.setLocationRelativeTo(null);// 居中
		this.setLayout(new FlowLayout());// 布局
		this.getContentPane().setBackground(Color.WHITE);// 设置画布背景为白色!

		DrawMouse mouse = new DrawMouse();

		String name[] = { "直线", "矩形", "三角形", "多边形", "椭圆", "曲线", "连线", "橡皮檫" };// 用数组的方法创建按钮
		for (int i = 0; i < name.length; i++) {
			JButton jbu = new JButton(name[i]);
			jbu.setPreferredSize(new Dimension(80, 30));
			this.add(jbu);
			jbu.addActionListener(mouse);
		}

		Color color[] = { Color.BLACK, Color.BLUE, Color.GREEN, Color.ORANGE, Color.RED, Color.YELLOW,
				new Color(200, 100, 100) };
		for (int i = 0; i < color.length; i++) {
			JButton jbu = new JButton();
			jbu.setBackground(color[i]);
			jbu.setPreferredSize(new Dimension(30, 30));
			this.add(jbu);
			jbu.addActionListener(mouse);
		}
		
		this.setVisible(true);// 可见
		
		Graphics g = this.getGraphics();// 取画笔一定要在窗体显示可见之后!!!

		this.addMouseListener(mouse);// 添加鼠标监听器
		this.addMouseMotionListener((MouseMotionListener) mouse);// 添加拖动监听器
		//传参
		mouse.setGr(g);
		mouse.setShapeArray(shapeArray);
	}
	/**
	 * 重绘
	 */
	public void paint(Graphics g) {
		super.paint(g);
		time++;
		System.out.println("重绘" + time);
		for (int i = 0; i < shapeArray.length; i++) {// 从shapeArray数组中取出shape重绘
			Shape shape = shapeArray[i];
			if (shape != null) {
				shape.drawShape(g);
			} 
		}
	}

	public static void main(String[] args) {
		DrawFrame frame = new DrawFrame();
		frame.showUI();
	}

}
/**
 * 监听器
 * @author 杨飞
 *
 */
public class DrawMouse implements MouseListener, ActionListener, MouseMotionListener {

	private Graphics gr;
	private int x1, y1, x2, y2, x3, y3, x4, y4, a, b;
	private String name = "直线";
	private int flag = 1, flag2 = 1, index = 0;
	private Color color = Color.BLACK;
	private Shape[] shapeArray;

	public void setGr(Graphics g) {
		gr = g;
	}

	public void setShapeArray(Shape[] shapeArray) {

		this.shapeArray = shapeArray;
	}

	public void mouseClicked(MouseEvent e) { // 点击
		// System.out.println("点击");
		if ("三角形".equals(name) && flag == 2) {
			x3 = e.getX();
			y3 = e.getY();
			gr.drawLine(x1, y1, x3, y3);
			Shape shape = new Shape(x1, y1, x3, y3, name, color);
			shapeArray[index++] = shape;
			gr.drawLine(x3, y3, x2, y2);
			Shape shape2 = new Shape(x3, y3, x2, y2, name, color);
			shapeArray[index++] = shape2;
			flag = 1;
		}
		if ("多边形".equals(name) && flag2 == 2) {
			gr.drawLine(x1, y1, x3, y3);
			Shape shape = new Shape(x1, y1, x3, y3, name, color);
			shapeArray[index++] = shape;
			x3 = e.getX();
			y3 = e.getY();
			flag2 = 2;
			if (e.getClickCount() == 2) {
				System.out.println("双击");
				gr.drawLine(a, b, x3, y3);
				Shape shape2 = new Shape(a, b, x3, y3, name, color);
				shapeArray[index++] = shape2;
				flag2 = 1;
			}
		}
		if("清屏".equals(name)){
			
		}
	}

	public void mousePressed(MouseEvent e) { // 按下
		// System.out.println("按下");
		if (flag == 1) {
			x1 = e.getX();
			y1 = e.getY();
		}
	}

	public void mouseReleased(MouseEvent e) { // 松开
//		System.out.println("松开");
		gr.setColor(color);
		if (flag == 1) {
			x2 = e.getX();
			y2 = e.getY();
		}
		if ("直线".equals(name)) {
			gr.drawLine(x1, y1, x2, y2);
			Shape shape = new Shape(x1, y1, x2, y2, name,color);
			shapeArray[index++] = shape;
		}

		if ("矩形".equals(name)) {
			gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y1 - y2));
            Shape shape=new Shape(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y1 - y2),name,color);
            shapeArray[index++] = shape;
		}
		if ("三角形".equals(name) && flag == 1) {
			gr.drawLine(x1, y1, x2, y2);
			Shape shape = new Shape(x1, y1, x2, y2, name,color);
			shapeArray[index++] = shape;
			flag++;
		}
		if ("多边形".equals(name) && flag2 == 1) {
			a = x2;
			b = y2;
			x3 = x2;
			y3 = y2;
			flag2++;
		}
		if ("连线".equals(name)) {
			Shape shape = new Shape(x1, y1, x4, y4, name,color);
			shapeArray[index++] = shape;
			Shape shape2 = new Shape(x1, y1, x4, y4, name,color);
			shapeArray[index++] = shape2;
		}
		if ("椭圆".equals(name)) {
		Shape shape = new Shape(Math.min(x1, x4), Math.min(y1, y4), Math.abs(x4 - x1), Math.abs(y4 - y1), name,color);
		shapeArray[index++] = shape;
		Shape shape2 = new Shape(Math.min(x1, x4), Math.min(y1, y4), Math.abs(x4 - x1), Math.abs(y4 - y1), name,color);
		shapeArray[index++] = shape2;
		}
	}

	public void actionPerformed(ActionEvent e) {

		if ("".equals(e.getActionCommand())) {
			JButton jbu = (JButton) e.getSource();
			color = jbu.getBackground();
			gr.setColor(color);
		} else
			name = e.getActionCommand();
	}

	public void mouseDragged(MouseEvent e) {// 拖拽
		// System.out.println("拖拽");
		if ("曲线".equals(name)) {
			x4 = e.getX();
			y4 = e.getY();
			gr.drawLine(x1, y1, x4, y4);
			Shape shape = new Shape(x1, y1, x4, y4, name, color);
			shapeArray[index++] = shape;
			x1 = x4;
			y1 = y4;
		}
		if ("连线".equals(name)) {
			gr.setColor(Color.WHITE);
			gr.drawLine(x1, y1, x4, y4);
			x4 = e.getX();
			y4 = e.getY();
			gr.setColor(color);
			gr.drawLine(x1, y1, x4, y4);
		}
		if ("椭圆".equals(name)) {
			gr.setColor(Color.WHITE);
			gr.drawOval(Math.min(x1, x4), Math.min(y1, y4), Math.abs(x4 - x1), Math.abs(y4 - y1));
			x4 = e.getX();
			y4 = e.getY();
			gr.setColor(color);
			gr.drawOval(Math.min(x1, x4), Math.min(y1, y4), Math.abs(x4 - x1), Math.abs(y4 - y1));
		}
		if ("橡皮檫".equals(name)) {
			x4 = e.getX();
			y4 = e.getY();
			gr.setColor(Color.WHITE);
			gr.fillRect(x4 - 15, y4 - 15, 30, 30);
			Shape shape = new Shape(x4 - 15, y4 - 15, 30, 30, name, color);
			shapeArray[index++] = shape;
		}
	}
	
	public void mouseEntered(MouseEvent e) { // 进入
		// System.out.println("进入");
	}

	public void mouseExited(MouseEvent e) { // 退出
		// System.out.println("退出");
	}

	public void mouseMoved(MouseEvent e) {// 移动
		// System.out.println("移动");
	}

}
/**
 * Shape类存点,目的是实现重绘
 * @author 杨飞
 *
 */
public class Shape {

	private int x1, y1, x2, y2;// 定义起始点和中止点,利用两个点画一条线段
	private String name;
	private Color color;

	public Shape(int x1, int y1, int x2, int y2, String name, Color color) {// 构造函数Shape
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.name = name;
		this.color = color;
	}
	
	public void reShape(Graphics g){
		this.x1 = 0;
		this.y1 = 0;
		this.x2 = 0;
		this.y2 = 0;
		this.name = "直线";
		this.color = Color.WHITE;
	}

	public void drawShape(Graphics g) {
		g.setColor(color);
		switch (name) {
		case "直线":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "三角形":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "多边形":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "曲线":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "矩形":
			g.drawRect(x1, y1, x2, y2);
			break;
		case "椭圆":
			g.drawOval(x1, y1, x2, y2);
			break;
		case "连线":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "橡皮檫":
			g.setColor(Color.WHITE);
			g.fillRect(x1, y1, x2, y2);
			break;
		}
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值