版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课题介绍11.1课程设计目的11.2课程设计要求11.3课程设计主要知识点1设计3画板界面设计图3模块概述31文件操作3图形绘制43详细设计与实现6DrawGraphic3.1.1菜单63.1.2工具栏83.1.3画图区域93.2基本图形类12类Drawings123.2.2子类(只列出部分)124设计中的难点144.1多态与动态绑定144.2重绘的理解144.3文件的新建、打开和保存155运行测试166总结18 (3)可以对线条的颜色和粗细进行设置,也可以对填充图形的颜色进行设识点 (2)选择一个文件,用到组件JFileChooser; (3)选择颜色,用到组件JColorChooser; (4)弹出标准对话框,用到组件JOptionPane; (5)在单击绘图按钮时,所触发的动作通过方法addActionListener和 (7)在界面设计的时候结合BorderLayout和GridLayout两种布局格式; (8)在添加监听器的时候有用到部匿名类和部类,所以要熟悉它们的构造 (9)在图形绘制的时候,用到Graphics类,其揭示了所有图形系统几乎都面有一支画笔,即Graphics的一个对象,当窗口调用paint方法的时候,系统会2总体设计2.1画板界面设计图BorderLayout标签、主要的画图区域分别新新建打开保存用户向导关于文件菜单栏画图区域鼠标位置显示标签铅笔直线椭圆圆矩形颜色粗细橡皮擦文字.2模块概述2.1文件操作2.2.2图形绘制ifegetSource数,当事件源鼠标标的,所以,在画图的过程中,只要鼠标单击一下就获得(x1,y1),紧接着用if语句判断画的基本图形是哪一个,如果是铅笔或者橡皮擦,则获得(x2,y2),详细设计与实现框架类DrawGraphic3.1.1菜单 publicvoidnewFile(){index=0;currentChoice=3;rBLUEstroke=1.0f;createNewItem();repaint();} publicvoidopenFile(){JFileChooserfileChooser=newJFileChooser();//为用户选择文件提供了一种简单的机制fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);intresult=fileChooser.showOpenDialog(this);//弹出一个"OpenFile"文件选择器对话框,父组件if(result==JFileChooser.CANCEL_OPTIOreturn;FilefileName=fileChooser.getSelectedFile();fileName.canRead();//测试应用程序是否可以读取此抽象路径名表示的文件if(fileName==null||fileName.getName().equals("")){JOptionPane.showMessageDialofileChooser,"这个名字不可以用的","",}else{try{FileInputStreamfis=newFileInputStream(fileName);input=newObjectInputStream(fis);intcountNumber=0;countNumber=input.readInt();for(index=0;index<countNumber;index++){DrawingsinputRecord=(Drawings)input.readObject();itemList[index]=inputRecord;}createNewItem();input.close();repaint();}catch(EOFExceptionendofFileException){//当输入过程中意外到达文件或流的末尾时,抛出此异常JOptionPaneshowMessageDialothis"nomorerecordinfile",endoffileJOptionPaneERROR_MESSAG;}catch(ClassNotFoundExceptionclassNotFoundException){JOptionPaneshowMessageDialothis,"unabletocreateobject","classnotfound",JOptionPane.ERROR_MESSAG;}catch(IOExceptionioException){JOptionPane.showMessageDialothis,"errorduringreadfromfile",readerror,JOptionPane.ERROR_MESSAG;}}} publicvoidsaveFile(){JFileChooserfileChooser=newJFileChooser();fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//允许用户只选择文件intresult=fileChooser.showSaveDialog(this);//弹出一个"SaveFile"文件选择器对话框if(result==JFileChooser.CANCEL_OPTIOreturn;FilefileName=fileChooser.getSelectedFile();fileName.canWrite();if(fileName==null||fileName.getName().equals("")){JOptionPane.showMessageDialofileChooser,"这个名字不可以用的","}else{try{fileName.delete();FileOutputStreamfos=newFileOutputStream(fileName);output=newObjectOutputStream(fos);output.writeInt(index);//写入一个32位的int值for(inti=0;i<index;i++){DrawingsoutputRecord=itemList[i];output.writeObject(outputRecord);//将指定的对象写入ObjectOutputStreamefos.close();//此操作将写入所有已缓冲的输出字节,并将它们刷新到底层流中}}catch(IOExceptionioe){ioe.printStackTrace();}}}3.1.2工具栏 publicclassButtonHandler1implementsActionListener{publicvoidactionPerformed(ActionEvente){for(intj=3;j<choices.length-3;j++){if(e.getSource()==choices[j]){currentChoice=j;createNewItem();repaint();}}}} publicclassButtonHandler2implementsActionListener{publicvoidactionPerformed(ActionEvente){if(e.getSource()==choices[choices.length-3]){chooseColor();}if(e.getSource()==choices[choices.length-2]){setStroke();}icon=newImageIcon(getClass().getResource("/images/smile.png"));if(e.getSource()==choices[choices.length-1]){DialonullcurrentChoice=14;createNewItem();repaint();}}}3.1.3画图区域 (1)createNewItem()用来new各种基本图形,在函数的一开始,我设置了voidcreateNewItem(){if(currentChoice==14){drawingArea.setCursor(Cursor.getPredefinedCursoCursor.TEXT_CURSO);}else{drawingArea.setCursor(Cursor.getPredefinedCursoCursor.CROSSHAIR_CURSO);}switch(currentChoice){case3:itemList[index]=newPencil();break;case4:itemList[index]=newLine();break;case5:itemList[index]=newRect();break;case6:itemList[index]=newfillRect();break;case7:itemList[index]=newOval();break;case8:itemList[index]=newfillOval();break;case9:itemList[index]=newCircle();break;case10:itemList[index]=newfillCircle();break;case11:itemList[index]=newRoundRect();break;case12:itemList[index]=newfillRoundRect();break;case13:itemList[index]=newRubber();break;case14:itemList[index]=newWord();break;}itemList[index].type=currentChoice;itemList[index].R=R;itemList[index].G=G;itemList[index].B=B;itemList[index].stroke=stroke;} chooseColor码如下:publicvoidchooseColor(){color=JColorChooser.showDialog(null,"在这里选择自己喜欢的颜色",color);R=color.getRed();G=color.getGreen();B=color.getBlue();} setStroke代码如下:publicvoidsetStroke(){Stringinput;input=JOptionPane.showInputDialonull,"在这里重新输入画笔的粗细值(一个>0的实数)","1.0");stroke=Float.parseFloat(input);//将字符串参数转换为一个float值itemList[index].stroke=stroke;} (4)mouseA监听鼠标单击、释放、进入、离开四个事件,当鼠标单击的时e.getSource()先得到一个点的坐标初始化(x1,y1),再判断单击的是哪一个按钮,如果是绘制其它图形的按钮,那就光得到一个点(x1,y1);如果是铅笔或者橡皮擦,则继续初始化第二个点的坐标(x2,y2),此时x1=x2,y1=y2,本按钮,那就光得到(x2,y2),关键代码如下:classmouseAextendsMouseAdapter{publicvoidmousePressed(MouseEvente){statusBar.setText("MousePressed:["+e.getX()+","+e.getY()+"]");itemList[index].x1=e.getX();itemList[index].y1=e.getY();if(currentChoice==3||currentChoice==13){itemList[index].x2=e.getX();itemList[index].y2=e.getY();index++;createNewItem();repaint();}if(currentChoice==14){itemList[index].x1=e.getX();itemList[index].y1=e.getY();Stringinput;input=JOptionPane.showInputDialog("可以添加你想写的文字咯!");//可以输入容的对话框itemList[index].s1=input;itemList[index].x2=f1;itemList[index].y2=f2;itemList[index].s2=stylel;//f1,f2用来存放当前字体风格index++;currentChoice=14;createNewItem();drawingArearepaint);}}publicvoidmouseReleased(MouseEvente){statusBar.setText("MouseReleased:["+e.getX()+","+e.getY()+"]");if(currentChoice==3||currentChoice==13){itemList[index].x1=e.getX();itemList[index].y1=e.getY();}itemList[index].x2=e.getX();itemList[index].y2=e.getY();index++;createNewItem();repaint();}} (5)mouseB监听鼠标拖拽和移动,在鼠标拖拽的时候,由于鼠标的拖拽一在动态拖拽的过程中只需要,得到点(x2,y2)就好:classmouseBextendsMouseMotionAdapter{publicvoidmouseDragged(MouseEvente){statusBar.setText("MouseDragged:["+e.getX()+","+e.getY()+"]");if(currentChoice==3||currentChoice==13){itemList[index-1].x2=itemList[index].x2=itemList[index].x1=e.getX();itemList[index-1].y2=itemList[index].y2=itemList[index].y1=e.getY();index++;createNewItem();}else{itemList[index].x2=e.getX();itemList[index].y2=e.getY();}repaint();}publicvoidmouseMoved(MouseEvente){statusBar.setText("MouseMoved:["+e.getX()+","+e.getY()+"]");}}3.2基本图形类DrawingsclassDrawingsimplementsSerializable{intx1,y1,x2,y2;intR,G,B;floatstroke;inttype;Strings1,s2;voiddraw(Graphics2Dg2d){};}3.2.2子类(只列出部分) (1)铅笔:classPencilextendsDrawings{voiddraw(Graphics2Dg2d){g2d.setPaint(newColor(R,G,B));g2d.setStroke(newBasicStroke(stroke));g2d.drawLine(x1,y1,x2,y2);}} (2)空心圆classCircleextendsDrawings{voiddraw(Graphics2Dg2d){g2d.setPaint(newColor(R,G,B));g2d.setStroke(newBasicStroke(stroke));g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),Math.max(Math.abs(x1-x2),Math.abs(y1-y2)));}} (3)实心圆classfillCircleextendsDrawings{voiddraw(Graphics2Dg2d){g2d.setPaint(newColor(R,G,B));g2d.setStroke(newBasicStr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 旅游业服务规范与操作手册
- 企业合同审核与风险控制指南(标准版)
- 基础设施建设招投标规范
- 石油天然气勘探开发作业手册(标准版)
- 2025 七年级道德与法治上册社区服务前的准备与注意事项指导课件
- 困难气道管理临床操作流程
- 零食很忙长沙市区中秋节促销活动策划方案
- 2025年江西交投校招笔试真题及答案
- 2025年单证招聘英语翻译笔试及答案
- 2025年机场中控技术员面试题库及答案
- 2026年广东省事业单位集中公开招聘高校毕业生11066名笔试模拟试题及答案解析
- 司法鉴定资料专属保密协议
- 丝路基金招聘笔试题库2026
- 2022年7月23日广东省事业单位高校毕业生招聘考试《基本能力测试》真题试卷解析
- 中职生理学考试真题及解析
- 院感三管监测课件
- 2025年江西省从“五方面人员”中选拔乡镇领导班子成员考试历年参考题库含答案详解(5套)
- 2025年数据分析个人工作总结范文
- 新疆湿地公园管理办法
- 新能源有限公司商业计划书
- c2考驾照科目一试题及答案
评论
0/150
提交评论