java入门(3)——简易绘图板
簡易繪圖板
簡易繪圖板的實(shí)現(xiàn),主要用到了java.awt和javax.swing包中的制作界面的相關(guān)知識,其次是Graphics類用于繪圖,最后用監(jiān)聽器將兩者聯(lián)系起來。
監(jiān)聽器
監(jiān)聽器用于監(jiān)聽事件,比如:鼠標(biāo)點(diǎn)擊、鍵盤輸入等等。他們被定義在java.awt.event這個(gè)包中。
安裝監(jiān)聽器主要有三個(gè)步驟:
1、尋找事件源,即當(dāng)前動(dòng)作發(fā)生的組件。
2、建立事件處理類,將監(jiān)聽器的抽象方法完善。
3、將事件源和事件處理類綁定。
在簡易繪圖板中,我們主要用到了動(dòng)作監(jiān)聽器和鼠標(biāo)監(jiān)聽器。動(dòng)作監(jiān)聽器用于監(jiān)聽鼠標(biāo)點(diǎn)擊和鍵盤輸入空格等動(dòng)作。鼠標(biāo)監(jiān)聽器用于監(jiān)聽鼠標(biāo)的動(dòng)作,如:按下、釋放、進(jìn)入等等。
Graphics類
Graphics類是java.awt包下的,主要用于繪圖。有繪制圓形、直線,調(diào)整畫筆顏色等各種功能。
java.awt與javax.swing包
java.awt與javax.swing包中的一部分類用于基本界面的制作,而我們知道如果想要繪圖必須要有窗體,因此我們需要掌握界面制作的基本知識,包括:窗體、按鈕、布局、文本框等等。而這些組件都在java.awt或者javax.swing包下面。
簡易繪圖板實(shí)現(xiàn)
功能介紹
1、可以畫出直線、圓、矩形、三角形、多邊形。
2、可以選取不同的顏色。
3、可以顯示畫筆當(dāng)前顏色。
4、可以保存畫下的圖形,不會因?yàn)槔齑翱诙А?#xff08;通過每次打開再重新繪制一遍實(shí)現(xiàn))
實(shí)現(xiàn)步驟
1、建立三個(gè)類:
DrawUI(用于繪圖)
DrawListener(用于寫事件監(jiān)聽器)
Shape(用于保存畫下的圖形)。
2、在DrawUI(事先繼承JFrame窗體類)中創(chuàng)建窗體、事件監(jiān)聽器、Graphics的繪圖范圍獲取、并將鼠標(biāo)監(jiān)聽器和Graphics繪圖范圍相連。
this.setTitle("簡易繪圖板");this.setSize(1000, 800);this.setDefaultCloseOperation(3);FlowLayout fl=new FlowLayout();this.setLayout(fl);DrawListener dl=new DrawListener();this.setVisible(true);//開始在顯卡下顯示//獲取繪圖板范圍,注意傳參的是Graphics而不是JFrame;Graphics g=this.getGraphics();//一定要先讓窗體顯示,才可以獲得它的圖形,進(jìn)而設(shè)置了繪圖范圍。//給窗體加上鼠標(biāo)監(jiān)聽器this.addMouseListener(dl);//讓鼠標(biāo)監(jiān)聽器活動(dòng)的范圍設(shè)置成畫圖的范圍。dl.g=g;3、DrawListener類和MouseListener、ActionListener兩個(gè)接口相連,同時(shí)完善相關(guān)抽象方法。
public class DrawListener implements MouseListener,ActionListener{//用于存儲圖形很計(jì)數(shù)Shape[] arrayShape=new Shape[100];int countShape=0;//繪圖用到的坐標(biāo)int x1,x2,y1,y2,x3,y3,x4,y4,x5,y5;//多邊形用動(dòng)態(tài)數(shù)組存儲int []x=new int[100];int []y=new int[100];int count_duobianxingX=0;int count_duobianxingY=0;//count用于三角形的三個(gè)坐標(biāo)的計(jì)步int count=0;//畫筆Graphics g;//按鈕,此按鈕用于顯畫筆當(dāng)前顏色JButton jbuc;//str記錄按鈕上的文字,從而判斷要求畫的是哪種圖形String str="123";//將DrawUI中的按鈕傳到DrawListener中public void setJBu(JButton jbu){jbuc=jbu;}//重寫ActionListener的抽象方法public void actionPerformed(ActionEvent e){//如果按鈕上沒有字體,那么是改變顏色的按鈕。if(e.getActionCommand().equals("")){JButton jb1= (JButton)e.getSource();g.setColor(jb1.getBackground());jbuc.setBackground(jb1.getBackground());}else str=e.getActionCommand();}//重寫MouseListener的抽象方法public void mouseClicked(MouseEvent e){if(str.equals("三角形")){if(count==0){x3=e.getX();y3=e.getY();g.drawOval(x3-4, y3-4, 8, 8);count++;}else if(count==1){x4=e.getX();y4=e.getY();g.drawOval(x4-4, y4-4, 8, 8);g.drawLine(x3, y3, x4, y4);count++;}else if(count==2){x5=e.getX();y5=e.getY();g.drawOval(x5-4, y5-4, 8, 8);g.drawLine(x3, y3, x5, y5);g.drawLine(x4, y4, x5, y5);//畫完之后一定要立刻存儲,下同。arrayShape[countShape++]=new Shape(x3,x4,x5,y3,y4,y5,str,g.getColor());count=0;}}if(str.equals("多邊形")){if(e.getButton()==1){x[count_duobianxingX++]=e.getX();y[count_duobianxingY++]=e.getY();g.drawOval(e.getX()-4 ,e.getY()-4, 8, 8);if(count_duobianxingX!=1){g.drawLine(e.getX(), e.getY(), x[count_duobianxingX-2], y[count_duobianxingY-2]);}}//以右擊鼠標(biāo)代表多邊形最后一個(gè)頂點(diǎn)else if(e.getButton()==3){ x[count_duobianxingX++]=e.getX();y[count_duobianxingY++]=e.getY();g.drawOval(e.getX()-4 ,e.getY()-4, 8, 8);g.drawLine(e.getX(), e.getY(), x[count_duobianxingX-2], y[count_duobianxingY-2]);g.drawLine(x[0], y[0], e.getX(), e.getY());arrayShape[countShape++]=new Shape(x,y,str,g.getColor(),count_duobianxingX);count_duobianxingX=0;count_duobianxingY=0;}} }public void mousePressed(MouseEvent e){x1=e.getX();y1=e.getY();} //直線、矩形、圓都是釋放鼠標(biāo)代表開始畫public void mouseReleased(MouseEvent e){x2=e.getX();y2=e.getY();if(str.equals("直線")){g.drawLine(x1, y1, x2, y2);arrayShape[countShape++]=new Shape(x1,x2,y1,y2,str,g.getColor());}if(str.equals("矩形")){g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));arrayShape[countShape++]=new Shape(x1,x2,y1,y2,str,g.getColor());}if(str.equals("圓")){g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));arrayShape[countShape++]=new Shape(x1,x2,y1,y2,str,g.getColor());}}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){} }4、在DrawUI中添加按鈕,并將按鈕和DrawListenr相連。
//三個(gè)添加按鈕的重載函數(shù)public JButton addButton(JFrame jf,DrawListener dl){JButton jbu=new JButton();jbu.setBackground(Color.BLACK);jbu.setPreferredSize(new Dimension(30,100));this.add(jbu);jbu.addActionListener(dl);return jbu;}public void addButton(String str,JFrame jf,DrawListener dl){JButton jbu=new JButton(str);this.add(jbu);jbu.addActionListener(dl);}public void addButton(Color col,JFrame jf,DrawListener dl){JButton jbu=new JButton();jbu.setBackground(col);jbu.setPreferredSize(new Dimension(30,30));//組件都要用Setpreferredsize來設(shè)置大小this.add(jbu);jbu.addActionListener(dl);} //圖形選項(xiàng)按鈕String[] jbustr= {"直線","圓","矩形","三角形","多邊形"};for(int i=0;i<jbustr.length;i++){addButton(jbustr[i],this,dl);}//將顯示當(dāng)前顏色的按鈕傳到監(jiān)聽器中,在監(jiān)聽器中改變它的顏色JButton jbuc=addButton(this,dl);dl.setJBu(jbuc);// 顏色選項(xiàng)按鈕Color[] jbucol= {Color.black,Color.BLUE,Color.cyan,Color.darkGray,Color.green,Color.orange,Color.PINK,Color.red};for(int i=0;i<jbucol.length;i++){addButton(jbucol[i],this,dl);}5、完善Shape類。
public class Shape {//存儲圖形需要的坐標(biāo)private int x1,x2,y1,y2,x3,y3,x4,y4,x5,y5; //存儲多邊形需要的數(shù)組和計(jì)數(shù)private int[] x=new int[100];private int[] y=new int[100];private int count_duobianxing; //圖形類型和顏色private String name;private Color col; //通過構(gòu)造函數(shù)重載實(shí)現(xiàn)保存不同的圖形public Shape(int x1,int x2,int y1,int y2,String name,Color col){this.col=col;this.name=name;this.x1=x1;this.y1=y1;this.x2=x2;this.y2=y2;}public Shape(int x1,int x2,int x3,int y1,int y2,int y3,String name,Color col){this.col=col;this.name=name;this.x1=x1;this.x2=x2;this.x3=x3;this.y1=y1;this.y2=y2;this.y3=y3;}public Shape(int[] x,int[] y,String name,Color col,int c){this.col=col;this.name=name;System.arraycopy(x, 0, this.x, 0, c);System.arraycopy(y, 0, this.y, 0, c);this.count_duobianxing=c;} //注意繪圖時(shí)參數(shù)時(shí)畫筆gpublic void Draw(Graphics g){switch(this.name){case("直線"):{g.setColor(col);g.drawLine(x1, y1, x2, y2);break;}case("矩形"):{g.setColor(col);g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));break;}case("圓"):{g.setColor(col);g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));break;}case("三角形"):{g.setColor(col);g.drawOval(x1-4, y1-4, 8, 8);g.drawOval(x2-4, y2-4, 8, 8);g.drawOval(x3-4, y3-4, 8, 8);g.drawLine(x1, y1, x2, y2);g.drawLine(x2, y2, x3, y3);g.drawLine(x1, y1, x3, y3);}case("多邊形"):{g.setColor(col);for(int i=0;i<count_duobianxing;i++){g.drawOval(x[i]-4,y[i]-4,8,8);if(i!=count_duobianxing-1)g.drawLine(x[i], y[i], x[i+1], y[i+1]);elseg.drawLine(x[0], y[0],x[count_duobianxing-1],y[count_duobianxing-1]);} }}} }6、在DrawUI中建立Shape數(shù)組,并將DrawListener中的Shape數(shù)組傳過來,以便在每次窗體初始化調(diào)用paint函數(shù)時(shí)顯示。
private Shape[] arrayShape=new Shape[100]; arrayShape=dl.arrayShape; public void paint(Graphics g){super.paint(g);for(int i=0;i<this.arrayShape.length;i++){arrayShape[i].Draw(g);}}效果
總結(jié)
以上是生活随笔為你收集整理的java入门(3)——简易绘图板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【XR806开发板试用】TCP通信测试
- 下一篇: SpringMVC---(2)