簡單畫圖板繪製直線

繪製直線

首先會用到的東西有

  • 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("畫圖");
	}
}

效果圖:


經過這麼一個小小的實現,我瞭解到了一些比較特殊的代碼寫法,更加深入的理解了代碼的執行流程以及在什麼時候什麼地點應該使用一些類的封裝來實現數據的存儲,以及傳遞。

繼續努力,勇往直前!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章