java——简易绘图板
簡易繪圖板
簡易繪圖板的構(gòu)建,用到了窗體的構(gòu)建,監(jiān)聽器的加入,Graphics類使用的相關(guān)知識,主要思路是在創(chuàng)建了窗體之后完善窗體結(jié)構(gòu),得到窗體對象的Graphics類之后,向窗體加入監(jiān)聽器,對Graphcis對象進(jìn)行操作從而實(shí)現(xiàn)簡易繪圖板的功能。
繪圖板基本功能:
1.選擇繪制直線、矩形、圓、三角形和多邊形這些不同的圖形
2.選擇不同的顏色進(jìn)行繪圖
3.能顯示當(dāng)前選取的顏色
4.保存當(dāng)前已經(jīng)繪制的圖形,不因拖動窗口和最小化等原因消失
建立三個類:
1.DrawUI類,構(gòu)建窗體對象,加入各種按鈕,提供Graphics對象以操作。
2.DrawListener類,作為動作監(jiān)聽器和鼠標(biāo)監(jiān)聽器,完成對于Graphics對象的繪圖以及繪圖選項(xiàng)轉(zhuǎn)換,顏色轉(zhuǎn)換等功能。
3.Shape類,用于保存已經(jīng)畫出的圖形對象使重新產(chǎn)生Graphics對象時能夠出現(xiàn)原有的圖形
監(jiān)聽器:
監(jiān)聽器的功能是用于監(jiān)聽事件源中產(chǎn)生的事件
DrawUI代碼:
public class DrawUI extends JFrame{public static void main(String arg[]) {DrawUI d = new DrawUI();d.initUI();}Shape[] arrayShape;public void initUI() {JFrame jf = new JFrame();this.setTitle("繪圖");this.setSize(1000,800);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);//流式布局FlowLayout flow =new FlowLayout();this.setLayout(flow);//建立監(jiān)聽器DrawListener dl = new DrawListener();this.addMouseListener(dl);String[] btnstrs = {"直線","矩形","圓","三角形","多邊形"};Color[] colors = {Color.white,Color.BLACK,Color.gray,Color.blue,Color.red,Color.green,Color.yellow};//當(dāng)前顏色按鈕JButton bucolor = new JButton();bucolor.setBackground(Color.BLACK);bucolor.setPreferredSize(new Dimension(80,50));this.add(bucolor);//畫圖按鈕for(int i=0;i<btnstrs.length;i++) {addButton(btnstrs[i],jf,dl);}//顏色按鈕for(int i=0;i<colors.length;i++) {addButton(colors[i],jf,dl);}//讓窗體可視化并傳窗體的圖形到監(jiān)聽器中this.setVisible(true);Graphics g = this.getGraphics();dl.g = g;dl.bucolor = bucolor;arrayShape = dl.getarrayShape();}public void addButton(String btnstr,JFrame jf,ActionListener al) {JButton bu = new JButton(btnstr);bu.setPreferredSize(new Dimension(80,50));this.add(bu);bu.addActionListener(al);}public void addButton(Color co,JFrame jf,ActionListener al) {JButton bu =new JButton();bu.setBackground(co);bu.setPreferredSize(new Dimension(50,30));this.add(bu);bu.addActionListener(al);}public void paint(Graphics g) {super.paint(g);for(int i=0;i<arrayShape.length;i++) {Shape tempShape = arrayShape[i];tempShape.drawShape(g);} // System.out.println("1");} }監(jiān)聽器DrawListener代碼:
public class DrawListener implements MouseListener,ActionListener{int x1,x2,y1,y2,x3,y3,x4,y4,x5,y5;Graphics g;String str = "";int count = 0;int[] px = new int[100];int[] py = new int[100];int pcount = 0;JButton bucolor = new JButton();private Shape arrayShape[] = new Shape[100];private int index = 0;Color color;public Shape[] getarrayShape() {return arrayShape;}public void actionPerformed(ActionEvent e) {String temp = e.getActionCommand(); //獲取按鈕上的文字if (temp.equals("")) { //若無文字JButton tempbu = (JButton)e.getSource(); //強(qiáng)制向下轉(zhuǎn)型Color tempco = tempbu.getBackground(); //獲取按鈕的顏色g.setColor(tempco); //改變顏色bucolor.setBackground(tempco); //改變當(dāng)前顏色顯示color = tempco; // color}else { //若有文字str = temp;}if (str.equals("三角形") || str.equals("多邊形")) {count = 0;}}/*** Invoked when the mouse button has been clicked (pressed* and released) on a component.*/public void mouseClicked(MouseEvent e) {if (str.equals("三角形")) {if (count==0) {x3 = e.getX();y3 = e.getY();g.fillOval(x3-5, y3-5, 10, 10);count++;}else if (count==1) {x4 = e.getX();y4 = e.getY();g.fillOval(x4-5, y4-5, 10, 10);g.drawLine(x3, y3, x4, y4);count++;}else if (count==2) {x5 = e.getX();y5 = e.getY();g.fillOval(x5-5, y5-5, 10, 10);g.drawLine(x3, y3, x5, y5);g.drawLine(x4, y4, x5, y5);count=0;Shape tempShape = new Shape("三角形",x3,y3,x4,y4,x5,y5,color);arrayShape[index++] = tempShape;}}else if (str.equals("多邊形")) {if (count==0) {x3 = e.getX();y3 = e.getY();g.fillOval(x3-5, y3-5, 10, 10);count++;px[pcount] = e.getX();py[pcount] = e.getY();pcount++;}else if (count==1) {x4 = e.getX();y4 = e.getY();g.fillOval(x4-5, y4-5, 10, 10);g.drawLine(x3, y3, x4, y4);count++;px[pcount] = e.getX();py[pcount] = e.getY();pcount++;}else if (e.getButton()==3) { //點(diǎn)擊右鍵時結(jié)束x5 = e.getX();y5 = e.getY();g.fillOval(x5-5, y5-5, 10, 10);g.drawLine(x4, y4, x5, y5);g.drawLine(x3, y3, x5, y5);count=0;px[pcount] = e.getX();py[pcount] = e.getY();int[] tpx = new int[px.length];int[] tpy = new int[py.length];System.arraycopy(px,0,tpx,0,px.length);System.arraycopy(py,0,tpy,0,py.length);Shape tempShape = new Shape("多邊形",tpx,tpy,pcount,color);arrayShape[index++] = tempShape;pcount=0;}else {x5 = e.getX();y5 = e.getY();g.fillOval(x5-5, y5-5, 10, 10);g.drawLine(x4, y4, x5, y5);x4 = x5;y4 = y5;count++;px[pcount] = e.getX();py[pcount] = e.getY();pcount++;}}}/*** Invoked when a mouse button has been pressed on a component.*/public void mousePressed(MouseEvent e) {x1 = e.getX();y1 = e.getY();}/*** Invoked when a mouse button has been released on a component.*/public void mouseReleased(MouseEvent e) {x2 = e.getX();y2 = e.getY();if (str.equals("直線")) { //畫線g.drawLine(x1,y1,x2,y2);Shape tempShape = new Shape("直線",x1,y1,x2,y2,color);arrayShape[index++] = tempShape;}else if (str.equals("矩形")) { //畫矩形g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));Shape tempShape = new Shape("矩形",x1,y1,x2,y2,color);arrayShape[index++] = tempShape;}else if (str.equals("圓")) { //畫圓g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));Shape tempShape = new Shape("圓",x1,y1,x2,y2,color);arrayShape[index++] = tempShape;}}/*** Invoked when the mouse enters a component.*/public void mouseEntered(MouseEvent e) {}/*** Invoked when the mouse exits a component.*/public void mouseExited(MouseEvent e) {} }Shape類代碼:
public class Shape {private int x1,y1,x2,y2,x3,y3;private String name;private int[] px;private int[] py;private int pcount;private Color color;//構(gòu)造直線、矩形、圓public Shape(String name,int x1,int y1,int x2,int y2,Color color) {this.x1 = x1;this.x2 = x2;this.y1 = y1;this.y2 = y2;this.name = name;this.color = color;}//構(gòu)造多邊形public Shape(String name,int[] px,int[] py,int pcount,Color color) {this.name = name;this.px = px;this.py = py;this.pcount = pcount;this.color = color;}//構(gòu)造三角形public Shape(String name,int x1,int y1,int x2,int y2,int x3,int y3,Color color) {this.x1 = x1;this.x2 = x2;this.y1 = y1;this.y2 = y2;this.x3 = x3;this.y3 = y3;this.name = name;this.color = color;}public void drawShape(Graphics g) {g.setColor(color);switch (name) {case ("直線"):{g.drawLine(x1, y1, x2, y2);break;}case("矩形"):{g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));break;}case("圓"):{g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));break;}case("三角形"):{g.fillOval(x1-4, y1-4, 8, 8);g.fillOval(x2-4, y2-4, 8, 8);g.fillOval(x3-4, y3-4, 8, 8);g.drawLine(x1, y1, x2, y2);g.drawLine(x1, y1, x3, y3);g.drawLine(x2, y2, x3, y3);}case("多邊形"):{for(int i=1;i<=pcount;i++) {g.fillOval(px[i-1]-4, py[i-1]-4, 8, 8);g.drawLine(px[i-1], py[i-1], px[i], py[i]);if (i == pcount) {g.drawLine(px[0], py[0], px[i], py[i]);g.fillOval(px[i]-4, py[i]-4, 8, 8);}}}}} }總結(jié)
以上是生活随笔為你收集整理的java——简易绘图板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: batch_prob_classify
- 下一篇: 2022长安杯赛后复现