画图板-Java课程设计.doc

上传人:jian****018 文档编号:9363183 上传时间:2020-04-05 格式:DOC 页数:41 大小:479KB
返回 下载 相关 举报
画图板-Java课程设计.doc_第1页
第1页 / 共41页
画图板-Java课程设计.doc_第2页
第2页 / 共41页
画图板-Java课程设计.doc_第3页
第3页 / 共41页
点击查看更多>>
资源描述
面向对象程序设计课程设计报告题 目: 画图板设计与实现 院 (系): 信息科学与工程学院 专业班级: 计算机科学与技术1201班 学生姓名: 程伟 学 号: 20121183011 指导教师: 吴奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日华中科技大学武昌分校制 面向对象程序设计 课程设计任务书一、设计(调查报告/论文)题目画图板设计与实现二、设计(调查报告/论文)主要内容内容:完成一个类似于windows画图板的绘画程序。软件主要有菜单栏,工具栏,画图区组成,菜单栏具有新建,打开,保存等常见的功能;工具栏可以选择用户需要的图形样式,设置参数,插入文本等操作。基本功能与要求:1 新建一个空的图形文件:用于绘制新的图形。2 打开或保存一个图形文件,便于对图形文件的管理。3 绘制一些基本的画图组件:直线,矩形,实心矩形,椭圆,实心椭圆,圆,实心圆,圆角矩形,实心圆角矩形等。4 可以用鼠标在绘制画板上单击拖动进行随笔画。5 橡皮擦:可以将已绘制好的图形擦掉。三、原始资料1.面向对象程序设计课程设计指导书2. 耿祥义JAVA大学实用教程北京:清华大学出版社,2009四、要求的设计(调查/论文)成果1.课程设计报告2.课程设计软件五、进程安排布置任务,查找资料、需求分析1天总体设计1天详细设计1.5天编制源程序实现3.5天测试与修改1天撰写课设报告2天六、主要参考资料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需求分析 平时生活中,我们会需要处理一些基本的图像,也有可能需要花一些简单的图像,平时所使用的图形处理工具有PhotoShop、Windows 画图工具等,其中PhotoShop 是一款非常强大的图形处理工具,windows 画图工具则是一款较为简单的画图工具,功能较为简单,用Windows画图工具,是一种比较简单与具有代表性的画图工具,虽然功能不够强大,但具有大多图片处理程序所必需的基本功能:铅笔画图、各种数学函数图形、填色、取色、橡皮擦等等。这次试验中我做的就是一个仿照windows画板的程序。 考虑到画图板的实际需要,画图板要实现打开,保存图片,用于打开本地图片或保存当前图片至本地;选择需要绘制的图形,用于选择需要绘制的图形样式,如直线,矩形,椭圆等;设置背景色,画笔色以及画笔大小,用于设置画板背景颜色,画笔颜色和当前画笔粗细;擦除错误操作;显示帮助信息等。1.2总体设计总体功能图 总体设计类图2详细设计2.1详细设计思路系统结构设计:菜单栏的设计及功能实现:菜单栏主要包括文件、编辑、设置、帮助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有撤销、恢复等功能;设置菜单含有对画笔颜色、画笔大小等的设置功能;帮助菜单提供画图板的帮助信息。画图区的设计:对图片的编辑处理区。工具栏的设计:根据实际需要选择不同的工具完成画图操作。其他功能设计。总体流程图2.2详细类设计MiniDrawPad :主类,扩展了JFrame类,用来生成主界面。Drawings类:是画图类,基本图形的单元。其各个子类用以实现画各种图形包括以下几个子类: 1.Line类:绘制直线;2. Rect类:绘制矩形;3. fillOval类:绘制实心椭圆;4. fillRect类:绘制实心矩形;5. Oval类:绘制椭圆;6. Cicle类:绘制圆;7. fillCicle类:绘制实心圆;8. RoundRect类:绘制圆角矩形;9. fillRoundRect类:绘制实心圆角矩形;10. Pecil类:自由笔;ButtonHandler类:按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作ButtonHandler1类:用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作checkBoxHandler类:选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中DrawPanel类:画图面板类,用来画图mouseA类:鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作mouseB类: 鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作 3编码实现源代码import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import javax.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, /保存 /*接下来是我们的画图板上面有的基本的几个绘图单元按钮*/ Pencil, /铅笔画,也就是用鼠标拖动着随意绘图 Line, /绘制直线 Rect, /绘制空心矩形 fRect, /绘制以指定颜色填充的实心矩形 Oval, /绘制空心椭圆 fOval, /绘制以指定颜色填充的实心椭圆 Circle, /绘制圆形 fCircle, /绘制以指定颜色填充的实心圆形 RoundRect, /绘制空心圆角矩形 frRect, /绘制以指定颜色填充的实心圆角矩形 Rubber, /橡皮擦,可用来擦去已经绘制好的图案 Color, /选择颜色按钮,可用来选择需要的颜色 Stroke, /选择线条粗细的按钮,输入需要的数值可以实现绘图线条粗细的变化 Word /输入文字按钮,可以在绘图板上实现文字输入 ; private String styleNames= 宋体 , 隶书 , 华文彩云 , 仿宋_GB2312 , 华文行楷 , 方正舒体 , Times New Roman , Serif , Monospaced , SonsSerif , Garamond ; /可供选择的字体项 /当然这里的灵活的结构可以让读者自己随意添加系统支持的字体 private Icon items; private String tipText= /这里是鼠标移动到相应按钮上面上停留时给出的提示说明条 /读者可以参照上面的按钮定义对照着理解 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, 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 ; JToolBar 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; /当前画笔颜色 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(); /定义菜单条 JMenu fileMenu=new JMenu(文件); fileMenu.setMnemonic(F); /新建文件菜单条 JMenuItem newItem=new JMenuItem(新建); newItem.setMnemonic(N); newItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) newFile(); /如果被触发,则调用新建文件函数段 ); fileMenu.add(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(打开); loadItem.setMnemonic(L); loadItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) loadFile(); /如果被触发,则调用打开文件函数段 ); fileMenu.add(loadItem); fileMenu.addSeparator(); /退出菜单项 JMenuItem exitItem=new JMenuItem(退出); exitItem.setMnemonic(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 colorItem=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(像素); 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); /设置提示菜单条 JMenu helpMenu=new JMenu(帮助); helpMenu.setMnemonic(H); /设置提示菜单项 JMenuItem aboutItem=new JMenuItem(关于); aboutItem.setMnemonic(A); aboutItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) JOptionPane.showMessageDialog(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 JToolBar( JToolBar.VERTICAL ) ; buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ; ButtonHandler handler=new ButtonHandler(); ButtonHandler1 handler1=new ButtonHandler1(); /导入我们需要的图形图标,这些图标都存放在与源文件相同的目录下面 for(int i=0;ichoices.length;i+) /itemsi=new ImageIcon( MiniDrawPad.class.getResource(namesi +.gif); /如果在jbuilder下运行本程序,则应该用这条语句导入图片 itemsi=new ImageIcon(namesi + .gif); /默认的在jdk或者jcreator下运行,用此语句导入图片 choicesi=new JButton(,itemsi); choicesi.setToolTipText(tipTexti); buttonPanel.add(choicesi); /将动作侦听器加入按钮里面 for(int i=3;ichoices.length-3;i+) choicesi.addActionListener(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 actionPerformed(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( new 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); 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.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 ActionListener public void actionPerformed(ActionEvent e) for(int j=3;jchoices.length-3;j+) if(e.getSource()=choicesj) currentChoice=j; createNewItem(); repaint(); /按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作public class ButtonHandler1 implements ActionListener public void actionPerformed(ActionEvent 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.INFORMATION_MESSAGE ); currentChoice=14; createNewItem(); repaint(); /鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作class mouseA extends MouseAdapter public void mousePressed(MouseEvent e) statusBar.setText( Mouse Pressed : + e.getX() + , + e.getY() + ); /设置状态提示 itemListindex.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=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.repaint(); 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(); repaint(); 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() + ); /鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作 class mouseB extends MouseMotionAdapter public void mouseDragged(MouseEvent e) statusBar.setText( + Mouse Dragged : + e.getX() + , + e.getY() + ); if(currentChoice=3|currentChoice=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 : + 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.getSource()=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(new mouseB(); public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; /定义画笔 int j=0; while (j0 ); stroke=Float.parseFloat(input); itemListindex.stroke=stroke; /保存图形文件程序段public void saveFile() JFileChooser fileChooser=new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result =fileChooser.showSaveDialog(this); if(result=JFileChooser.CANCEL_OPTION) return ; File fileName=fileChooser.getSelectedFile(); fileName.canWrite(); if (fileName=null|fileName.getName().equals() JOptionPane.showMessageDialog(fileChooser,Invalid File Name, Invalid File Name, JOptionPane.ERROR_MESSAGE); else try fileName.delete(); FileOutputStream fos=new FileOutputStream(fileName); output=new ObjectOutputStream(fos); drawings record; output.writeInt( index ); for(int i=0;i index ;i+) drawings p= itemList i
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 大学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!