前言本篇主要介绍用Java设计一种简单的画图工具步骤1创建窗体窗体中包含按钮可以添加想绘制的图形控制画笔颜色//头文件importjavax.imageio.stream.ImageInputStream;importjavax.swing.*;importjava.awt.*;publicclassDrawUI{//publicstaticvoidmain(String[]args){DrawUI dnewDrawUI();d.initUI();}publicvoidinitUI(){//创建窗体JFrame jfnewJFrame();jf.setSize(800,600);jf.setTitle(画图工具);jf.setLocationRelativeTo(null);jf.setDefaultCloseOperation(3);//设置流式布局FlowLayout flownewFlowLayout();jf.setLayout(flow);//设置并添加按钮JButton jbnewJButton(直线);jf.add(jb);JButton jb2newJButton(矩形);jf.add(jb2);JButton jb3newJButton(三角形);jf.add(jb3);JButton jb4newJButton(等腰三角形);jf.add(jb4);JButton jb5newJButton(椭圆);jf.add(jb5);JButton jb6newJButton(多边形);jf.add(jb6);JButton jb7newJButton(红色);jf.add(jb7);jf.setVisible(true);}}2给窗体和按钮添加监听器首先重写鼠标监听器和动作监听器中的抽象方法importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;publicclassDrawListenerimplementsMouseListener,ActionListener{//鼠标监听器中有五个抽象方法全部重写才能正常运行publicvoidmouseClicked(MouseEvent e){};publicvoidmousePressed(MouseEvent e){};publicvoidmouseReleased(MouseEvent e){};publicvoidmouseEntered(MouseEvent e){};publicvoidmouseExited(MouseEvent e){};//动作监听器抽象方法重写publicvoidactionPerformed(ActionEvent e){};想要在窗体上画图需要用到画笔因此先在监听器中创建画笔然后在窗体中获取画笔对象在监听器类中创建画笔importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;publicclassDrawListenerimplementsMouseListener,ActionListener{//创建画笔publicGraphics gr;publicvoidmouseClicked(MouseEvent e){};publicvoidmousePressed(MouseEvent e){};publicvoidmouseReleased(MouseEvent e){};publicvoidmouseEntered(MouseEvent e){};publicvoidmouseExited(MouseEvent e){};publicvoidactionPerformed(ActionEvent e){};在DrawUI类的显示界面方法中获取画笔并给窗体和按钮添加监听器Graphics gjf.getGraphics();DrawListener listenernewDrawListener();jf.addMouseListener(listener);jb.addActionListener(listener);jb2.addActionListener(listener);jb3.addActionListener(listener);jb4.addActionListener(listener);jb5.addActionListener(listener);jb6.addActionListener(listener);jb7.addActionListener(listener);listener.grg;3实现画图能力这一步骤主要在监听器中实现importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;publicclassDrawListenerimplementsMouseListener,ActionListener{publicGraphics gr;//设置全局变量publicint x1,y1,x2,y2,x3,y3;publicString name;//设置标记位控制代码执行顺序publicint flag1;publicvoidactionPerformed(ActionEvent e){//获取被点击按钮的名字namee.getActionCommand();//控制画笔颜色可以多设置几个其他颜色if(name.equals(红色)){gr.setColor(Color.red);}}publicvoidmouseClicked(MouseEvent e){x3e.getX();y3e.getY();//画三角形if(name.equals(三角形)){gr.drawLine(x1,y1,x3,y3);gr.drawLine(x2,y2,x3,y3);flag1;}//画多边形if(name.equals(多边形)){gr.drawLine(x2,y2,x3,y3);//点坐标传递x2x3;y2y3;//如果双击连接第一个点和最后一个点if(e.getClickCount()2){gr.drawLine(x1,y1,x3,y3);flag1;}}}publicvoidmousePressed(MouseEvent e){if(flag1){x1e.getX();y1e.getY();}}publicvoidmouseReleased(MouseEvent e){if(flag1){x2e.getX();y2e.getY();}//取最小值int xMath.min(x1,x2);int yMath.min(y1,y2);//取最大值intXMath.max(x1,x2);intYMath.max(y1,y2);//取绝对值int wMath.abs(x2-x1);int hMath.abs(y2-y1);//画直线if(name.equals(直线)){gr.drawLine(x1,y1,x2,y2);}//画矩形if(name.equals(矩形)){gr.drawRect(x,y,w,h);}//画三角形if(name.equals(三角形)){gr.drawLine(x1,y1,x2,y2);flag;}//画等腰三角形if(name.equals(等腰三角形)){gr.drawLine(x,Y,X,Y);gr.drawLine(x,Y,(x1x2)/2,y);gr.drawLine(X,Y,(x1x2)/2,y);}//画椭圆if(name.equals(椭圆)){gr.drawOval(x,y,w,h);}//画多边形if(name.equals(多边形)flag1){gr.drawLine(x1,y1,x2,y2);flag;}}publicvoidmouseEntered(MouseEvent e){}publicvoidmouseExited(MouseEvent e){}}4成果展示优化和补充如果想在画图工具中添加更多的按钮继续运用上面的方法会太过繁琐可以用数组进行优化JButton jbnewJButton(直线);jf.add(jb);JButton jb2newJButton(矩形);jf.add(jb2);JButton jb3newJButton(三角形);jf.add(jb3);JButton jb4newJButton(等腰三角形);jf.add(jb4);JButton jb5newJButton(椭圆);jf.add(jb5);JButton jb6newJButton(多边形);jf.add(jb6);JButton jb7newJButton(红色);jf.add(jb7);jf.setVisible(true);Graphics gjf.getGraphics();DrawListener listenernewDrawListener();jf.addMouseListener(listener);jb.addActionListener(listener);jb2.addActionListener(listener);jb3.addActionListener(listener);jb4.addActionListener(listener);jb5.addActionListener(listener);jb6.addActionListener(listener);jb7.addActionListener(listener);}}优化为DrawListener listenernewDrawListener();String[]name{橡皮,曲线,直线,矩形,三角形,等腰三角形,椭圆,多边形,红色,黑色};for(int i0;iname.length;i){JButton jbnewJButton(name[i]);jf.add(jb);jb.addActionListener(listener);}补充曲线和橡皮擦需要用到鼠标拖动监听器publicclassDrawListenerimplementsMouseMotionListener{publicvoidmouseDragged(MouseEvent e){if(name.equals(曲线)){gr.drawLine(x1,y1,e.getX(),e.getY());x1e.getX();y1e.getY();}if(name.equals(橡皮)){//g2.setStroke(newBasicStroke(10));gr.setColor(Color.WHITE);gr.drawLine(x1,y1,e.getX(),e.getY());x1e.getX();y1e.getY();}}publicvoidmouseMoved(MouseEvent e){};}实现画图区域与按钮区域的分开需要运用边框布局将DrawUI类中的显示界面方法稍加改动publicvoidinitUI(){//窗体默认边框布局JFrame jfnewJFrame()fan;jf.setSize(800,600);jf.setTitle(画图工具);jf.setLocationRelativeTo(null);jf.setDefaultCloseOperation(3);JPanel NPanelnewJPanel();//设置面板用于存放按钮NPanel.setBackground(Color.green);//设置背景颜色NPanel.setPreferredSize(newDimension(0,80));//设置heightjf.add(NPanel,BorderLayout.NORTH);//将面板设置在窗体上方DrawListener listenernewDrawListener();String[]name{粗,细,橡皮,曲线,直线,矩形,三角形,等腰三角形,椭圆,多边形,红色,黑色,蓝色};for(int i0;iname.length;i){JButton jbnewJButton(name[i]);NPanel.add(jb);//将按钮添加在面板上jb.addActionListener(listener);}JPanel drawPanelnewJPanel();//设置画图面板用于画图drawPanel.setBackground(Color.WHITE);//画图区域设置成白色与橡皮颜色保持一致jf.add(drawPanel,BorderLayout.CENTER);//画图面板设置在中心会自动填满剩下的区域jf.setVisible(true);Graphics gdrawPanel.getGraphics();//将画笔添加在画图面板上//将鼠标相关的监听器添加在画图面板上drawPanel.addMouseListener(listener);drawPanel.addMouseMotionListener(listener);listener.grg;}调整画笔粗细且不影响上一步选择的画图方式和画笔颜色publicclassDrawListenerimplementsMouseListener,ActionListener,MouseMotionListener{publicGraphics gr;publicint width1;publicGraphics2D g2;publicColor colorColor.BLACK;publicvoidactionPerformed(ActionEvent e){String stre.getActionCommand();//创建新变量str单独控制颜色与粗细if(str.equals(红色)){colorColor.RED;}elseif(str.equals(黑色)){colorColor.BLACK;}elseif(str.equals(蓝色)){colorColor.BLUE;}elseif(str.equals(粗)){width5;}elseif(str.equals(细)){width1;}else{namee.getActionCommand();}g2(Graphics2D)gr;//将gr强制转型调用Graphics2D的方法调整大小g2.setStroke(newBasicStroke(width));//设置画笔大小g2.setColor(color);}}