简单画图板绘制直线

绘制直线

首先会用到的东西有

  • ArrayList
  • Frame

Paint.java

public class Paint extends Frame{
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private ArrayList list = new ArrayList();

    public Paint(String title) {
        //创建一个画图窗体
        super(title);
        this.setSize(600,600);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        this.addOwnMouseListener();
        this.addOwnWindowListener();
    }

    public void addOwnMouseListener() {
         this.addMouseListener(new MouseListener() {
				
				@Override
				public void mouseReleased(MouseEvent e) {	//鼠标释放
					Graphics graphics = Paint.this.getGraphics();		//特殊写法,表示使用当前窗体对象调用产生Graphics对象
					x2 = e.getX();
					y2 = e.getY();
					graphics.drawLine(x1, y1, x2, y2);
					Line line = new Line(x1, y1, x2, y2);
					list.add(line);							//将每次绘制的数据封装到自定义的Line类中保存
				}
				
				@Override
				public void mousePressed(MouseEvent e) {	//鼠标按下
					x1 = e.getX();
					y1 = e.getY();
				}
				
				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
			});
    }

    public void addOwnWindowListener() {
        this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);					//退出JVM结束运行,关闭窗口
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
    }

    //每当窗口被操作之后,都会重新绘制图形,调用的是paint方法
	//覆写重绘方法
    @Override
    public void paint(Graphics g) {
        for(Object obj : list) {
            Line line = (Line)obj;
            g.drawLine(line.getX1(), line.getY1(), line.getY1(), line.getY2());
        }
    }
}

Line.java


public class Line {
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	
	public Line() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Line(int x1, int y1, int x2, int y2) {
		super();
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}
	public int getX1() {
		return x1;
	}
	public void setX1(int x1) {
		this.x1 = x1;
	}
	public int getY1() {
		return y1;
	}
	public void setY1(int y1) {
		this.y1 = y1;
	}
	public int getX2() {
		return x2;
	}
	public void setX2(int x2) {
		this.x2 = x2;
	}
	public int getY2() {
		return y2;
	}
	public void setY2(int y2) {
		this.y2 = y2;
	}
	
}

Test.java


public class Test{
	public static void main(String[] args) {
		new Paint("画图");
	}
}

效果图:


经过这么一个小小的实现,我了解到了一些比较特殊的代码写法,更加深入的理解了代码的执行流程以及在什么时候什么地点应该使用一些类的封装来实现数据的存储,以及传递。

继续努力,勇往直前!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值