版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 面向对象程序设计课程设计报告题 目: 画图板设计与实现 院 (系): 信息科学与工程学院 专业班级: 计算机科学与技术1201班 学生姓名: 程伟 学 号: 20121183011 指导教师: 吴奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日华中科技大学武昌分校制 面向对象程序设计 课程设计任务书一、设计(调查报告/论文)题目画图板设计与实现二、设计(调查报告/论文)主要内容内容:完成一个类似于windows画图板的绘画程序。软件主要有菜单栏,工具栏,画图区组成,菜单栏具有新建,打开,保存等常见的功能;工具栏可以选择用户需要的图形样式,设置参数,插入文本等操作。基本功
2、能与要求:1 新建一个空的图形文件:用于绘制新的图形。2 打开或保存一个图形文件,便于对图形文件的管理。3 绘制一些基本的画图组件:直线,矩形,实心矩形,椭圆,实心椭圆,圆,实心圆,圆角矩形,实心圆角矩形等。4 可以用鼠标在绘制画板上单击拖动进行随笔画。5 橡皮擦:可以将已绘制好的图形擦掉。三、原始资料1.面向对象程序设计课程设计指导书2. 耿祥义JAVA大学实用教程北京:清华大学出版社,2009四、要求的设计(调查/论文)成果1.课程设计报告2.课程设计软件五、进程安排布置任务,查找资料、需求分析1天总体设计1天详细设计1.5天编制源程序实现3.5天测试与修改1天撰写课设报告2天六、主要参考
3、资料1 张广彬. Java课程设计案例精编(第二版).北京:清华大学出版社, 2011. 2 耿祥义. Java课程设计(第二版).北京:清华大学出版社, 2008.3 耿祥义JAVA大学实用教程北京:清华大学出版社,20094 邹林达陈国君 Java2程序设计基础北京:清华大学出版社,20105 邹林达Java2程序设计基础实验指导北京:清华大学,2010指导教师(签名): 20 年 月 日目 录1需求与总体设计11.1需求分析11.2总体设计12详细设计32.1详细设计思路32.2详细主代码43编码实现74系统运行与测试33总结371需求与总体设计1.1需求分析 平时生活中,我们会需要处理
4、一些基本的图像,也有可能需要花一些简单的图像,平时所使用的图形处理工具有PhotoShop、Windows 画图工具等,其中PhotoShop 是一款非常强大的图形处理工具,windows 画图工具则是一款较为简单的画图工具,功能较为简单,用Windows画图工具,是一种比较简单与具有代表性的画图工具,虽然功能不够强大,但具有大多图片处理程序所必需的基本功能:铅笔画图、各种数学函数图形、填色、取色、橡皮擦等等。这次试验中我做的就是一个仿照windows画板的程序。 考虑到画图板的实际需要,画图板要实现打开,保存图片,用于打开本地图片或保存当前图片至本地;选择需要绘制的图形,用于选择需要绘制的图
5、形样式,如直线,矩形,椭圆等;设置背景色,画笔色以及画笔大小,用于设置画板背景颜色,画笔颜色和当前画笔粗细;擦除错误操作;显示帮助信息等。1.2总体设计总体功能图 总体设计类图2详细设计2.1详细设计思路系统结构设计:菜单栏的设计及功能实现:菜单栏主要包括文件、编辑、设置、帮助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有撤销、恢复等功能;设置菜单含有对画笔颜色、画笔大小等的设置功能;帮助菜单提供画图板的帮助信息。画图区的设计:对图片的编辑处理区。工具栏的设计:根据实际需要选择不同的工具完成画图操作。其他功能设计。总体流程图2.2详细类设计MiniDrawPad :
6、主类,扩展了JFrame类,用来生成主界面。Drawings类:是画图类,基本图形的单元。其各个子类用以实现画各种图形包括以下几个子类: 1.Line类:绘制直线;2. Rect类:绘制矩形;3. fillOval类:绘制实心椭圆;4. fillRect类:绘制实心矩形;5. Oval类:绘制椭圆;6. Cicle类:绘制圆;7. fillCicle类:绘制实心圆;8. RoundRect类:绘制圆角矩形;9. fillRoundRect类:绘制实心圆角矩形;10. Pecil类:自由笔;ButtonHandler类:按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作Butt
7、onHandler1类:用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作checkBoxHandler类:选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中DrawPanel类:画图面板类,用来画图mouseA类:鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作mouseB类: 鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作 3编码实现源代码import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import javax
8、.swing.*;import java.io.*;import java.util.*;public class MiniDrawPad extends JFrame private ObjectInputStream input; private ObjectOutputStream output; /定义输入输出流,用来调用和保存图像文件 private JButton choices; /按钮数组,存放以下名称的功能按钮 private String names= "New", /新建 "Open", /打开 "Save",
9、/"保存" /*接下来是我们的画图板上面有的基本的几个绘图单元按钮*/ "Pencil", /铅笔画,也就是用鼠标拖动着随意绘图 "Line", /绘制直线 "Rect", /绘制空心矩形 "fRect", /绘制以指定颜色填充的实心矩形 "Oval", /绘制空心椭圆 "fOval", /绘制以指定颜色填充的实心椭圆 "Circle", /绘制圆形 "fCircle", /绘制以指定颜色填充的实心圆形 "
10、RoundRect", /绘制空心圆角矩形 "frRect", /绘制以指定颜色填充的实心圆角矩形 "Rubber", /橡皮擦,可用来擦去已经绘制好的图案 "Color", /选择颜色按钮,可用来选择需要的颜色 "Stroke", /选择线条粗细的按钮,输入需要的数值可以实现绘图线条粗细的变化 "Word" /输入文字按钮,可以在绘图板上实现文字输入 ; private String styleNames= " 宋体 " , " 隶书 " ,
11、" 华文彩云 " , " 仿宋_GB2312 " , " 华文行楷 " , " 方正舒体 " , " Times New Roman " , " Serif " , " Monospaced " , " SonsSerif " , " Garamond " ; /可供选择的字体项 /当然这里的灵活的结构可以让读者自己随意添加系统支持的字体 private Icon items; private String tipT
12、ext= /这里是鼠标移动到相应按钮上面上停留时给出的提示说明条 /读者可以参照上面的按钮定义对照着理解 "Draw a new picture", "Open a saved picture", "Save current drawing", "Draw at will", "Draw a straight line", "Draw a rectangle", "Fill a ractangle", "Draw an oval", &
13、quot;Fill an oval", "Draw a circle", "Fill a circle", "Draw a round rectangle", "Fill a round rectangle", "Erase at will", "Choose current drawing color", "Set current drawing stroke", "Write down what u want" ; JTo
14、olBar buttonPanel ; /定义按钮面板 private JLabel statusBar; /显示鼠标状态的提示条 private DrawPanel drawingArea; /画图区域 private int width=1000,height=600; /定义画图区域初始大小 drawings itemList=new drawings5000; /用来存放基本图形的数组 private int currentChoice=3; /设置默认画图状态为随笔画 int index=0; /当前已经绘制的图形数目 private Color color=Color.black;
15、 /当前画笔颜色 int R,G,B; /用来存放当前色彩值 int f1,f2; /用来存放当前字体风格 String style1; /用来存放当前字体 private float stroke=1.0f; /设置画笔粗细,默认值为1.0f JCheckBox bold,italic; /定义字体风格选择框 /bold为粗体,italic为斜体,二者可以同时使用 JComboBox styles; public MiniDrawPad() /构造函数 super("Drawing Pad"); JMenuBar bar=new JMenuBar(); /定义菜单条 JM
16、enu fileMenu=new JMenu("文件"); fileMenu.setMnemonic('F'); /新建文件菜单条 JMenuItem newItem=new JMenuItem("新建"); newItem.setMnemonic('N'); newItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) newFile(); /如果被触发,则调用新建文件函数段 ); fileMenu.a
17、dd(newItem); /保存文件菜单项 JMenuItem saveItem=new JMenuItem("保存"); saveItem.setMnemonic('S'); saveItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) saveFile(); /如果被触发,则调用保存文件函数段 ); fileMenu.add(saveItem); /打开文件菜单项 JMenuItem loadItem=new JMenuItem(&q
18、uot;打开"); loadItem.setMnemonic('L'); loadItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) loadFile(); /如果被触发,则调用打开文件函数段 ); fileMenu.add(loadItem); fileMenu.addSeparator(); /退出菜单项 JMenuItem exitItem=new JMenuItem("退出"); exitItem.setMnemoni
19、c('X'); exitItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) System.exit(0); /如果被触发,则退出画图板程序 ); fileMenu.add(exitItem); bar.add(fileMenu); /设置颜色菜单条 JMenu colorMenu=new JMenu("颜色"); colorMenu.setMnemonic('C'); /选择颜色菜单项 JMenuItem colorIt
20、em=new JMenuItem("选择颜色"); colorItem.setMnemonic('O'); colorItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) chooseColor(); /如果被触发,则调用选择颜色函数段 ); colorMenu.add(colorItem); bar.add(colorMenu); /设置线条粗细菜单条 JMenu strokeMenu=new JMenu("像素")
21、; strokeMenu.setMnemonic('S'); /设置线条粗细菜单项 JMenuItem strokeItem=new JMenuItem("设置像素"); strokeItem.setMnemonic('K'); strokeItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) setStroke(); ); strokeMenu.add(strokeItem); bar.add(strokeMenu);
22、/设置提示菜单条 JMenu helpMenu=new JMenu("帮助"); helpMenu.setMnemonic('H'); /设置提示菜单项 JMenuItem aboutItem=new JMenuItem("关于"); aboutItem.setMnemonic('A'); aboutItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) JOptionPane.showMessageDi
23、alog(null, "这是一个迷你画图板!nCopyright (c) 2014 华中科技大学武昌分校 ", " 画图板程序说明 ", JOptionPane.INFORMATION_MESSAGE ); ); helpMenu.add(aboutItem); bar.add(helpMenu); items=new ImageIconnames.length; /创建各种基本图形的按钮 drawingArea=new DrawPanel(); choices=new JButtonnames.length; buttonPanel = new JTo
24、olBar( JToolBar.VERTICAL ) ; buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ; ButtonHandler handler=new ButtonHandler(); ButtonHandler1 handler1=new ButtonHandler1(); /导入我们需要的图形图标,这些图标都存放在与源文件相同的目录下面 for(int i=0;i<choices.length;i+) /itemsi=new ImageIcon( MiniDrawPad.class.getResource(namesi +&
25、quot;.gif"); /如果在jbuilder下运行本程序,则应该用这条语句导入图片 itemsi=new ImageIcon(namesi + ".gif"); /默认的在jdk或者jcreator下运行,用此语句导入图片 choicesi=new JButton("",itemsi); choicesi.setToolTipText(tipTexti); buttonPanel.add(choicesi); /将动作侦听器加入按钮里面 for(int i=3;i<choices.length-3;i+) choicesi.addA
26、ctionListener(handler); choices0.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) newFile(); ); choices1.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) loadFile(); ); choices2.addActionListener( new ActionListener() public void ac
27、tionPerformed(ActionEvent e) saveFile(); ); choiceschoices.length-3.addActionListener(handler1); choiceschoices.length-2.addActionListener(handler1); choiceschoices.length-1.addActionListener(handler1); /字体风格选择 styles=new JComboBox(styleNames); styles.setMaximumRowCount(8); styles.addItemListener( n
28、ew ItemListener() public void itemStateChanged(ItemEvent e) style1=styleNamesstyles.getSelectedIndex(); ); /字体选择 bold=new JCheckBox("黑体字"); italic=new JCheckBox("斜体字"); checkBoxHandler cHandler=new checkBoxHandler(); bold.addItemListener(cHandler); italic.addItemListener(cHandler
29、); JPanel wordPanel=new JPanel(); buttonPanel.add(bold); buttonPanel.add(italic); buttonPanel.add(styles); styles.setMinimumSize( new Dimension ( 500, 200 ) ); styles.setMaximumSize(new Dimension ( 1000, 200 ) ); Container c=getContentPane(); super.setJMenuBar( bar ); c.add(buttonPanel,BorderLayout.
30、NORTH); c.add(drawingArea,BorderLayout.CENTER); statusBar=new JLabel(); c.add(statusBar,BorderLayout.SOUTH); statusBar.setText(" To The Little Drawing Pad! :)"); createNewItem(); setSize(width,height); show(); /按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作 public class ButtonHandler implements Action
31、Listener public void actionPerformed(ActionEvent e) for(int j=3;j<choices.length-3;j+) if(e.getSource()=choicesj) currentChoice=j; createNewItem(); repaint(); /按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作public class ButtonHandler1 implements ActionListener public void actionPerformed(ActionEven
32、t e) if(e.getSource()=choiceschoices.length-3) chooseColor(); if(e.getSource()=choiceschoices.length-2) setStroke(); if(e.getSource()=choiceschoices.length-1) JOptionPane.showMessageDialog(null, "Please hit the drawing pad to choose the word input position", "Hint",JOptionPane.IN
33、FORMATION_MESSAGE ); currentChoice=14; createNewItem(); repaint(); /鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作class mouseA extends MouseAdapter public void mousePressed(MouseEvent e) statusBar.setText(" Mouse Pressed :" + e.getX() + ", " + e.getY() + ""); /设置状态提示 itemList
34、index.x1=itemListindex.x2=e.getX(); itemListindex.y1=itemListindex.y2=e.getY(); /如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作 if(currentChoice=3|currentChoice=13) itemListindex.x1=itemListindex.x2=e.getX(); itemListindex.y1=itemListindex.y2=e.getY(); index+; createNewItem(); /如果当前选择的图形式文字输入,则进行下面操作 if(currentChoice=
35、14) itemListindex.x1=e.getX(); itemListindex.y1=e.getY(); String input; input=JOptionPane.showInputDialog( "Please input the text you want!"); itemListindex.s1=input; itemListindex.x2=f1; itemListindex.y2=f2; itemListindex.s2=style1; index+; currentChoice=14; createNewItem(); drawingArea.r
36、epaint(); public void mouseReleased(MouseEvent e) statusBar.setText(" Mouse Released :" + e.getX() + ", " + e.getY() + ""); if(currentChoice=3|currentChoice=13) itemListindex.x1=e.getX(); itemListindex.y1=e.getY(); itemListindex.x2=e.getX(); itemListindex.y2=e.getY(); r
37、epaint(); index+; createNewItem(); public void mouseEntered(MouseEvent e) statusBar.setText(" Mouse Entered :" + e.getX() + ", " + e.getY() + ""); public void mouseExited(MouseEvent e) statusBar.setText(" Mouse Exited :" + e.getX() + ", " + e.getY()
38、+ ""); /鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作 class mouseB extends MouseMotionAdapter public void mouseDragged(MouseEvent e) statusBar.setText(" " + " Mouse Dragged :" + e.getX() + ", " + e.getY() + ""); if(currentChoice=3|currentChoic
39、e=13) itemListindex-1.x1=itemListindex.x2=itemListindex.x1=e.getX(); itemListindex-1.y1=itemListindex.y2=itemListindex.y1=e.getY(); index+; createNewItem(); else itemListindex.x2=e.getX(); itemListindex.y2=e.getY(); repaint(); public void mouseMoved(MouseEvent e) statusBar.setText(" Mouse Moved
40、 :" + e.getX() + ", " + e.getY() + ""); /选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中private class checkBoxHandler implements ItemListener public void itemStateChanged(ItemEvent e) if(e.getSource()=bold) if(e.getStateChange()=ItemEvent.SELECTED) f1=Font.BOLD; else f1=Font.PLAIN; if(e.getS
41、ource()=italic) if(e.getStateChange()=ItemEvent.SELECTED) f2=Font.ITALIC; else f2=Font.PLAIN; /画图面板类,用来画图 class DrawPanel extends JPanel public DrawPanel() setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); setBackground(Color.white); addMouseListener(new mouseA(); addMouseMotionListener
42、(new mouseB(); public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; /定义画笔 int j=0; while (j<=index) draw(g2d,itemListj); j+; void draw(Graphics2D g2d,drawings i) i.draw(g2d); /将画笔传入到各个子类中,用来完成各自的绘图 /新建一个画图基本单元对象的程序段 void createNewItem() if(currentChoice=14
43、) /进行相应的游标设 drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR); else drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); switch (currentChoice) case 3: itemListindex=new Pencil(); break; case 4: itemListindex=new Line(); break; case 5: itemListindex=new Rect(); break; case 6: itemListindex=new fillRect(); break; case 7: itemListindex=new Oval(); break; case 8: itemListindex=new fillOval(); break; case 9: itemListindex=new Circle(); break; case 10: itemListindex=new fillCi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2027届广东省广州市华南师范大附属中学九年级物理第一学期期末达标检测模拟试题含解析
- 2027届湖南省长沙市田家炳实验中学物理九年级第一学期期末调研试题含解析
- 2027届山东省潍坊诸城市第七中学化学九上期中质量检测模拟试题含解析
- 2027届广东省中山市名校物理九年级第一学期期末联考试题含解析
- 2026中国医药中间体行业发展趋势全面调研及企业竞争格局发展预测与投资效益研究文档
- 七级第二期新世纪外国语学校2027届物理九上期末质量跟踪监视模拟试题含解析
- 2026中国印刷机械制造业市场供需分析及投资评估规划分析研究报告
- 年产1500万套新能源汽车热管理系统传感磁环组件项目可行性研究报告模板-申批备案
- 2027届山东省滨州市九上化学期中检测模拟试题含解析
- 2026人工智能机器视觉技术创新应用与市场前景分析报告
- 《2025东南亚数字经济报告》英文版
- 2025年大学《精神医学-儿童少年精神病学》考试参考题库及答案解析
- 食堂安全消防培训
- 初中物理八年级上册专题突破讲义(人教版)专题01 机械运动计算题分析(原卷版)
- PIVAS感控知识培训课件
- 中储粮员工知识培训内容课件
- 2024年天津市工业和信息化研究院招聘笔试真题
- Unit1MeandmyclassLesson5课件冀教版八年级英语上册
- DB23∕T 3368-2022 沥青路面冷再生设计与施工技术规程
- 二建继续教育bim考试试题及答案
- 肺癌手术麻醉管理
评论
0/150
提交评论