學會了使用按鈕監聽以及鼠標監聽器之後,我們就可以制作一個屬於自己的畫圖板了,以下是我之前制作的一個簡易的畫圖板,功能方面尚還不足,不過已經可以完成鉛筆,橡皮,畫直線,圓等基本工作了,還可以完成顏色的自由選擇,大家不放自己也動手做做吧。
首先是界面類:
Java代碼
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; @SuppressWarnings("serial") public class Draw extends JFrame{ public Color color=Color.BLACK;//設置初始顏色 private Color colorselector; private JButton colorselectorbu[]=new JButton[6]; //創建對象時需要new一個對象,否則他的默認值就是NULL private JPanel colorjp=new JPanel(); public static void main(String args[]){ Draw dr=new Draw(); dr.showframe(); } public void showframe(){ JPanel north=new JPanel(); //上方區域,存放顏色按鈕以及顏色選擇器 JPanel west=new JPanel(); //左邊區域,存放圖形按鈕 JPanel center=new JPanel(); //中間區域,存放畫板 JPanel south=new JPanel(); //下面區域,用來顯示當前鼠標所在前位置以及所畫圖形的大小 //-----------------中間區域--------------- center.setLayout(new FlowLayout(FlowLayout.LEFT));//將中間區域的布局設置為左對齊 JPanel drawjp=new JPanel(); drawjp.setBackground(Color.white); drawjp.setPreferredSize(new Dimension(600,500)); center.add(drawjp); //-----------------上方區域---------------- JButton colorchooserbu=new JButton("更多顏色"); colorchooserbu.addActionListener(colorlistener); colorjp.setPreferredSize(new Dimension(120,60)); colorjp.setLayout(new GridLayout(3,6,0,0)); //創建一個顏色組 Color color[]=new Color[]{Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,Color.GREEN ,Color.LIGHT_GRAY,Color.GREEN,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.YELLOW}; Dimension d=new Dimension(20,20); for(int i=0;i<color.length;i++){ JButton colorbu=new JButton(); colorbu.setBackground(color[i]); colorbu.setPreferredSize(d); colorjp.add(colorbu); colorbu.addActionListener(listener); } //按鈕選擇器的自定義顏色組 for(int j=0;j<colorselectorbu.length;j++){ colorselectorbu[j]=new JButton(); colorselectorbu[j].setPreferredSize(d); colorjp.add(colorselectorbu[j]); colorselectorbu[j].addActionListener(listener); } north.add(colorjp); north.add(colorchooserbu); //-----------------左邊區域---------------- String img[]=new String[]{"Line", "oval", "rect", "roundrect", "eraser", "fill", "pencil", "select"}; String img1[]=new String[]{"Line1","oval1","rect1","roundrect1","eraser1","fill1", "pencil1","select1"}; ButtonGroup bg=new ButtonGroup(); for(int i=0;i<img.length;i++){ //創建Icon組 ImageIcon shapeicon=new ImageIcon("back/"+img[i]+".jpg"); ImageIcon shapeicon1=new ImageIcon("back/"+img1[i]+".jpg"); JRadioButton shapebu=new JRadioButton(shapeicon); bg.add(shapebu); shapebu.setPressedIcon(shapeicon1); //設置選中時的圖案 shapebu.setSelectedIcon(shapeicon1); shapebu.setActionCommand(img[i]); west.add(shapebu); } //---------------下方區域------------- JLabel la1=new JLabel(); JLabel la2=new JLabel(); JLabel la3=new JLabel("600x500像素"); JLabel la4=new JLabel(); south.add(la4); south.add(la1); south.add(la2); south.add(la3); south.setLayout(new GridLayout(1,4,20,20)); north.setPreferredSize(new Dimension(200,80)); west.setPreferredSize(new Dimension(80,200)); south.setPreferredSize(new Dimension(200,20)); north.setBackground(Color.GRAY); west.setBackground(Color.DARK_GRAY); center.setBackground(Color.LIGHT_GRAY); south.setBackground(Color.WHITE); this.setLayout(new BorderLayout()); this.add(north,BorderLayout.NORTH); this.add(west,BorderLayout.WEST); this.add(south,BorderLayout.SOUTH); this.add(center,BorderLayout.CENTER); this.setTitle("畫圖板復習"); this.setSize(900,700); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setVisible(true); //在畫板上獲取畫布權限 Graphics g=drawjp.getGraphics(); DrawListener listener=new DrawListener(g,bg,this,la1,la2); drawjp.addMouseListener(listener); drawjp.addMouseMotionListener(listener); } //添加按鈕監聽,獲取按鈕的背景顏色 ActionListener listener=new ActionListener(){ public void actionPerformed(ActionEvent e) { //獲取事件源,發生事件的對象 Object obj=e.getSource(); if(obj instanceof JButton){ color=((JButton) obj).getBackground(); } } }; ActionListener colorlistener=new ActionListener(){ //按鈕數組累加器 int i = 0; public void actionPerformed(ActionEvent e){ colorselector=JColorChooser.showDialog(null, "顏色選擇器", Color.BLACK); colorselectorbu[i].setBackground(colorselector); i++; if(i>=6){ //超出六個按鈕,返回0 i=0; } } }; }
下面是監聽類:
Java代碼
import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JLabel; public class DrawListener extends MouseAdapter{ private int x1,x2,y1,y2; private Graphics g; //將畫布對象傳過來 private ButtonGroup bg; //將按鈕組傳過來 private Draw dr; private JLabel la1; private JLabel la2; public DrawListener(Graphics g,ButtonGroup bg,Draw dr,JLabel la1,JLabel la2){ this.g=g; this.bg=bg; this.dr=dr; this.la1=la1; this.la2=la2; } //鼠標按下 public void mousePressed(MouseEvent e){ x1=e.getX(); y1=e.getY(); g.setColor(dr.color); } //鼠標釋放 public void mouseReleased(MouseEvent e){ x2=e.getX(); y2=e.getY(); ButtonModel bm=bg.getSelection(); String com=bm.getActionCommand(); if(com.equals("Line")){ g.drawLine(x1, y1, x2, y2); //改變la2中的數字 la2.setText(Math.abs(x1-x2)+"x"+Math.abs(y1-y2)); } else if(com.equals("oval")){ g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x1-x2),Math.abs(y1-y2)); la2.setText(Math.abs(x1-x2)+"x"+Math.abs(y1-y2)); } else if(com.equals("rect")){ g.drawRect(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2)); la2.setText(Math.abs(x1-x2)+"x"+Math.abs(y1-y2)); } else if(com.equals("roundrect")){ g.drawRoundRect(Math.min(x1,x2), Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2), Math.abs(x1-x2)/4,Math.abs(y1-y2)/4); la2.setText(Math.abs(x1-x2)+"x"+Math.abs(y1-y2)); } } // public void mouseMoved(MouseEvent e){ int X=e.getX(); int Y=e.getY(); la1.setText(X+","+Y); } public void mouseExited(MouseEvent e) { la1.setText(""); } //鼠標拖動 public void mouseDragged(MouseEvent e){ int X=e.getX(); int Y=e.getY(); la1.setText(X+","+Y); x2=e.getX(); y2=e.getY(); String com=bg.getSelection().getActionCommand(); if(com.equals("pencil")){ g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } else if(com.equals("eraser")){ g.setColor(Color.WHITE); for(int i=-8;i<8;i++){ g.drawLine(x1+i,y1+i,x2+i,y2+i); } x1=x2; y1=y2; } else if(com.equals("select")){ g.drawLine(x1,y1,x2,y2); } else if(com.equals("fill")){ Random ran=new Random(); int a=ran.nextInt(16)-8; g.drawLine(x1+a, y1+a, x2+a, y2+a); x1=x2; y1=y2; } } }
使用到的圖片是從WINDOWS中的畫圖板中截圖下來的,附件中已提供給大家,自己也動手試試吧
查看本欄目