JAVA课程设计题目:图形编辑系统*名:**学号:*********班级: 计算机学院09级1班日期:2012年6月8日目录JAVA课程设计 (1)一、设计思路 (1)二、具体实现 (1)三、运行调试与分析讨论 (4)四、设计体会与小结 (9)五、参考文献 (9)六、附录 (9)一、设计思路将主界面设置为当前Windows风格的,然后在一个Container里分为菜单功能区域,和绘图区域。
在菜单功能区域里包含了打开已有图形,保存图形,画笔,直线,圆形,矩形,画笔颜色预设,画笔大小预设,调色板,橡皮,橡皮大小预设,以及清除等功能,在菜单功能区域的下方就是绘图区域,绘图区域主要是通过记录下鼠标移动点的位置来实现绘图功能的。
二、具体实现在该系统中要实现以下各功能:1、能用拖橡皮筋的手法绘制各类图形(直线、椭圆、矩形、):可以在菜单区域选择直线、椭圆、矩形等button控件然后在绘图区域用托橡皮的手法绘制各类图形;2、支持画笔颜色、大小设置:在系统的功能菜单区域可以设置画笔的颜色、大小,根据自己的喜好来绘制图形;3、能将画面中的所有图形存储到文件中:在菜单区域有保存功能,可以将绘制好的图形保存到自己的文件夹中;4、能从文件读取图形进行增补:可以打开自己已有的文件,将文件读取出来,将文件图形进行增补等工作;5、采用对话框获取图形文件名:在读取的时候可以读取文件名称;6、删除:在系统的右上角设有清除和橡皮擦,如果画有不满意的图形时,可以用橡皮擦进行擦除,如果想要把整幅图片的都要删除的话,可以点击清楚按钮将整幅图片删除。
系统的工作原理以及该系统流程图如下:图 1 系统流程图图 2 打开文件程序流程图图 3 保存文件程序流程图三、运行调试与分析讨论系统的详细设计代码请查阅附带的代码。
以下仅对各个界面进行截图展示:图3系统主界面图 3 画笔工具绘图的界面图 4 直线工具画图的界面图 5 圆形工具画图的界面图 6 矩形工具画图的界面图7 画笔大小与画笔颜色设置界面图9 橡皮檫与橡皮檫大小工具的使用的界面图10 调色板工具的界面图11 打开图片的界面图12 保存图片的界面运行JA V A程序中的pb文件,在pb中实例化paintboard文件显示系统的主界面,如图三所示,主界面有进入其他子功能的按钮,选择相应的按钮便可进行相应的画笔、圆形、矩形、直线、橡皮擦等一些操作,画图工具是针对鼠标的一些点的操作来进行画图实现的,在其中定义了三个类,一个点point类,一个界面和操作类paintboard类,一个主函数类,在主函数中实例化paintboard类实现界面操作,又在paintboard类中实例化点类,可以在各个功能里面来进行对点的操作实现画图功能,主要的功能为打开图片,保存图片,画笔圆形,矩形,直线,橡皮擦以及调色板的操作。
四、设计体会与小结通过几周的上课时间,为了要做好自己所做的项目,自己通过查找资料、复习课本、编程调试,写实验报告等环节,进一步掌握了以前学到的知识,并且还对GUI组键的应用有了更深入的认识与掌握,另外还学到了一些新东西,比如JA V A 的SWING、AWT包,以前是没有接触过的,可是通过这次课程设计使得我们对这个包更了解。
通过实践的学习,我认到学好计算机要重视实践操作,不仅仅是学习java 语言,还是其它的语言,以及其它的计算机方面的知识都要重在实践,所以后在学习过程中,我会更加注视实践操作,使自己便好地学好计算机。
五、参考文献[1]朱福喜,尹为民等编著.Java语言与面向对象程序设计.武汉:武汉大学出版社,2002.12[2]冯军,程超等编著.JBuilder 9.0程序设计.北京:中国水利水电出版社,2004.5[3]丁振凡,黎章等编著.Java语言实用教程..北京:北京邮电大学出版社,2005.8[4]何桥,李肃义等编著.Java程序设计简明教程.北京:中国水利水电出版社,2004.9六、附录程序的主要代码://pb.javapackage pb;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import java.awt.geom.*;import java.io.*;public class pb{public static void main(String args[]){ paintboard pp=new paintboard("画图程序"); }}// paintboard.javapackage pb;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import java.awt.geom.*;import java.io.*;public class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener{int x = -1, y = -1;int con = 1;//画笔大小int Econ = 5;//橡皮大小int toolFlag = 0;//toolFlag:工具标记//toolFlag工具对应表://(0--画笔);(1--橡皮);(2--清除);//(3--直线);(4--圆);(5--矩形);Color c = new Color(0,0,0); //画笔颜色BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细Point cutflag = new Point(-1, -1, c, 6, con);//截断标志Vector paintInfo = null;//点信息向量组int n = 1;FileInputStream picIn = null;FileOutputStream picOut = null;ObjectInputStream VIn = null;ObjectOutputStream VOut = null;// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/ Panel toolPanel;Button eraser, drLine,drCircle,drRect;Button clear ,pen;Choice ColChoice,SizeChoice,EraserChoice;Button colchooser;Label 颜色,大小B,大小E;//保存功能Button openPic,savePic;FileDialog openPicture,savePicture;paintboard(String s){super(s);addMouseMotionListener(this);addMouseListener(this);paintInfo = new Vector();/*各工具按钮及选择项*///颜色选择ColChoice = new Choice();ColChoice.add("black");ColChoice.add("red");ColChoice.add("blue");ColChoice.add("green");ColChoice.addItemListener(this);//画笔大小选择SizeChoice = new Choice();SizeChoice.add("1");SizeChoice.add("3");SizeChoice.add("5");SizeChoice.add("7");SizeChoice.add("9");SizeChoice.addItemListener(this);//橡皮大小选择EraserChoice = new Choice();EraserChoice.add("5");EraserChoice.add("9");EraserChoice.add("13");EraserChoice.add("17");EraserChoice.addItemListener(this);////////////////////////////////////////////////////toolPanel = new Panel();clear = new Button("清除");eraser = new Button("橡皮");pen = new Button("画笔");drLine = new Button("直线");drCircle = new Button("圆形");drRect = new Button("矩形");openPic = new Button("打开图片");savePic = new Button("保存图片");colchooser = new Button("调色板");//各组件事件监听clear.addActionListener(this);eraser.addActionListener(this);pen.addActionListener(this);drLine.addActionListener(this);drCircle.addActionListener(this);drRect.addActionListener(this);openPic.addActionListener(this);savePic.addActionListener(this);colchooser.addActionListener(this);颜色= new Label("画笔颜色",Label.CENTER); 大小B = new Label("画笔大小",Label.CENTER); 大小E = new Label("橡皮大小",Label.CENTER); //面板添加组件toolPanel.add(openPic);toolPanel.add(savePic);toolPanel.add(pen);toolPanel.add(drLine);toolPanel.add(drCircle);toolPanel.add(drRect);toolPanel.add(颜色); toolPanel.add(ColChoice);toolPanel.add(大小B); toolPanel.add(SizeChoice);toolPanel.add(colchooser);toolPanel.add(eraser);toolPanel.add(大小E); toolPanel.add(EraserChoice);toolPanel.add(clear);//工具面板到APPLET面板add(toolPanel,BorderLayout.NORTH);setBounds(60,60,900,600); setVisible(true);validate();//dialog for save and loadopenPicture = new FileDialog(this,"打开图片",FileDialog.LOAD); openPicture.setVisible(false);savePicture = new FileDialog(this,"保存图片",FileDialog.SA VE); savePicture.setVisible(false);openPicture.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ openPicture.setVisible(false); }});savePicture.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ savePicture.setVisible(false); }});addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ System.exit(0);}});}public void paint(Graphics g)Graphics2D g2d = (Graphics2D)g;Point p1,p2;n = paintInfo.size();if(toolFlag==2)g.clearRect(0,0,getSize().width,getSize().height);//清除for(int i=0; i<n-1; i++){p1 = (Point)paintInfo.elementAt(i);p2 = (Point)paintInfo.elementAt(i+1);size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);g2d.setColor(p1.col);g2d.setStroke(size);if(p1.tool==p2.tool){switch(p1.tool){case 0://画笔Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);g2d.draw(line1);break;case 1://橡皮g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);break;case 3://直线Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);g2d.draw(line2);break;case 4://圆Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));g2d.draw(ellipse);break;case 5://矩形Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));g2d.draw(rect);break;case 6://截断,跳过i=i+1;break;default :}//end switch}//end if}//end for}public void itemStateChanged(ItemEvent e){if(e.getSource()==ColChoice)//预选颜色{String name = ColChoice.getSelectedItem();if(name=="black"){ c = new Color(0,0,0); }else if(name=="red"){ c = new Color(255,0,0); }else if(name=="green"){ c = new Color(0,255,0); }else if(name=="blue"){ c = new Color(0,0,255); }}else if(e.getSource()==SizeChoice)//画笔大小{String selected = SizeChoice.getSelectedItem();if(selected=="1"){con = 1;size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);}else if(selected=="3"){con = 3;size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);}else if(selected=="5"){ con = 5;size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);}else if(selected=="7"){ con = 7;size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);}else if(selected=="9"){ con = 9;size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);}}else if(e.getSource()==EraserChoice)//橡皮大小{String Esize = EraserChoice.getSelectedItem();if(Esize=="5"){ Econ = 5*2; }else if(Esize=="9"){ Econ = 9*2; }else if(Esize=="13"){ Econ = 13*2; }else if(Esize=="17"){ Econ = 17*3; }}}public void mouseDragged(MouseEvent e){Point p1 ;switch(toolFlag){case 0://画笔x = (int)e.getX();y = (int)e.getY();p1 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p1);repaint();break;case 1://橡皮x = (int)e.getX();y = (int)e.getY();p1 = new Point(x, y, null, toolFlag, Econ);paintInfo.addElement(p1);repaint();break;default :}}public void mouseMoved(MouseEvent e) {}public void update(Graphics g){paint(g);}public void mousePressed(MouseEvent e) {Point p2;switch(toolFlag){case 3://直线x = (int)e.getX();y = (int)e.getY();p2 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p2);break;case 4: //圆x = (int)e.getX();y = (int)e.getY();p2 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p2);break;case 5: //矩形x = (int)e.getX();y = (int)e.getY();p2 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p2);break;default :}}public void mouseReleased(MouseEvent e) {Point p3;switch(toolFlag){case 0: //画笔paintInfo.addElement(cutflag);break;case 1: //eraserpaintInfo.addElement(cutflag);break;case 3: //直线x = (int)e.getX();y = (int)e.getY();p3 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p3);paintInfo.addElement(cutflag);repaint();break;case 4: //圆x = (int)e.getX();y = (int)e.getY();p3 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p3);paintInfo.addElement(cutflag);repaint();break;case 5: //矩形x = (int)e.getX();y = (int)e.getY();p3 = new Point(x, y, c, toolFlag, con);paintInfo.addElement(p3);paintInfo.addElement(cutflag);repaint();break;default:}}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mouseClicked(MouseEvent e){}public void actionPerformed(ActionEvent e){if(e.getSource()==pen)//画笔{ toolFlag = 0; }if(e.getSource()==eraser)//橡皮{ toolFlag = 1; }if(e.getSource()==clear)//清除{toolFlag = 2;paintInfo.removeAllElements();repaint();}if(e.getSource()==drLine)//画线{ toolFlag = 3; }if(e.getSource()==drCircle)//画圆{ toolFlag = 4; }if(e.getSource()==drRect)//画矩形{ toolFlag = 5; }if(e.getSource()==colchooser)//调色板{Color newColor = JColorChooser.showDialog(this,"调色板",c);c = newColor;}if(e.getSource()==openPic)//打开图片{openPicture.setVisible(true);if(openPicture.getFile()!=null){int tempflag;tempflag = toolFlag;toolFlag = 2 ;repaint();try{paintInfo.removeAllElements();File filein = new File(openPicture.getDirectory(),openPicture.getFile());picIn = new FileInputStream(filein);VIn = new ObjectInputStream(picIn);paintInfo = (Vector)VIn.readObject();VIn.close();repaint();toolFlag = tempflag;}catch(ClassNotFoundException IOe2){repaint();toolFlag = tempflag;System.out.println("can not read object");}catch(IOException IOe){repaint();toolFlag = tempflag;System.out.println("can not read file");}}}if(e.getSource()==savePic)//保存图片{savePicture.setVisible(true);try{File fileout = new File(savePicture.getDirectory(),savePicture.getFile());picOut = new FileOutputStream(fileout);VOut = new ObjectOutputStream(picOut);VOut.writeObject(paintInfo);VOut.close();}catch(IOException IOe){System.out.println("can not write object");}}}}//end paintboard//point.javapackage pb;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import java.awt.geom.*;import java.io.*;public class Point implements Serializable{int x,y;Color col;int tool;int boarder;Point(int x, int y, Color col, int tool, int boarder){this.x = x;this.y = y;this.col = col;this.tool = tool;this.boarder = boarder;}}。