




免费预览已结束,剩余31页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
翁佳盛Java语言课程期末作业Java语言课程期末作业题 目 第1题:简易绘图程序 学 院 计算机学院 专 业 软件工程 班 别 学 号 姓 名 2015年12月6日36一、课程题目 绘图应用程序。利用学过的GUI 方法实现一个较为完整的绘图应用程序。用户可以选择要画的图形(如线,椭圆,圆,矩形,弧,圆角矩形,多边形,折线等等),选择画笔颜色和画笔粗度,用鼠标单击和拖拽等动作绘图,并能将绘图结果保存为jpg和bmp格式。二、题目分析与设计 1. 开发环境:Eclipse 4.5.0JDK 1.8.0_452. 题目需求分析:题目要求实现一个较为完整的绘图程序,最基本的要求是用户可以自由选择图形,颜色,线条粗细等属性,并用鼠标绘制图形,并将其保存为图片文件。首先,程序界面参考一般绘图软件,进行组件的布局。另外对于程序的功能实现,则将图形封装成类,通过多态实现多种不同图形的绘制,并用输入输出流保存或打开文件。除此之外根据实际操作的需要,添加一些必要或者实用的功能,如撤销操作,快捷键撤销或保存,关闭文件前弹窗提示保存等功能。(1). 菜单功能:通过菜单栏进行:一、 文件的读取与写入;二、 使用扩展功能;三、 程序的简单偏好设置;四、 程序信息提示。(2) . 绘图功能:在绘图过程通过工具栏进行形状、颜色等图形选择,在文本输入及扩展功能状态下会新增子工具栏,在子工具栏中对于字体或其它形状进行设置。然后在画板区域通过鼠标的点击与拖拽绘制图形。绘图过程应有保存与撤销功能。绘图范围为画板的显示的区域。(3). 其它功能:如快捷键Ctrl+z为撤销,Ctrl+s为保存。鼠标停在工具栏按钮上时提示功能。文件修改后窗口标题带上星号作为标记。第一次编辑的文件或修改过的文件关闭前自动提示保存。等等。(4). 功能架构图:简易绘图文件操作绘画图形补充功能打开文件新建文件保存文件撤销擦除图形绘制背景颜色更多图形作者信息3. 界面设计:本程序使用Swing图形编程。菜单:类似于常见程序,添加子菜单:File,Paint, Setting, Help。在File菜单下有new,open, save, exit等菜单项。Paint下主要是moreshape调出更多图形选择,Setting下的background菜单项可更改画板背景颜色。Help下可查看程序及作者的信息。主界面:采用默认的BordeLayout布局,添加工具栏于NORTH区,画板于CENTER区。打开程序时默认全屏。工具栏:由于有多个工具栏,所以使用JTabbedPane做出多标签。主工具栏一直存在,文本工具栏和扩展工具栏每次最多只有一个出现,若打开时另一个工具栏已存在,则会先将已存在的工具栏关闭。主工具栏与文本工具栏由于有复选框这一组件,添加后会自动填满多余空间,所以使用FlowLayout布局。扩展工具栏使用默认布局。主工具栏添加常用图形按钮、图形属性按钮、撤销按钮。文本工具栏添加字体属性按钮,撤销按钮及退出文本工具栏按钮。扩展工具栏添加多边形按钮及退出扩展工具栏按钮。每个工具栏的按钮各自存放在一个ArrayList中。画板:画板类从JPanel继承,重写paintComponent方法,使得画板每次刷新时可将之前的所有图形重新画出来。4. 逻辑实现:画图部分:程序将一个图形的所有信息封装为一个Drawings类,而具体的图形则从Drawings类继承,并实现自己的draw()方法用以画出图形。因此,可以创建一个Drawings类型的数组,用一个索引对数组进行操作,用户每画一个图形,就将该图形的所有信息存放到数组中索引指向的位置,索引向后移动一位。重写后的画板的drawComponent方法就会自动将数组中从0位置到索引位置的所有图形在画板上绘制出来。而撤销操作只需要将索引向前移动一位并重画即可。文件读写:保存文件时,先创建一个BufferedImage对象,然后将画板内容画到该对象上,并将该对象写入到指定文件中。打开文件时,则从指定文件中获得一个BufferedImage对象,将其作为一个从Drawings类继承的图形,在画板上画出来。三、测试分析1. 测试数据:(1). 新建文件,画图,保存为jpg文件;(2). 新建文件,画图,保存为bmp文件;(3). 打开(1)(2)中保存的文件;(4). 打开一个txt文件,弹窗报错;2. 测试情况:测试结果与预计结果一致;附录:源代码+图标import java.awt.AWTEvent;import java.awt.BasicStroke;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Toolkit;import java.awt.event.AWTEventListener;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.Serializable;import java.util.ArrayList;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTabbedPane;import javax.swing.JToggleButton;import javax.swing.JToolBar;import javax.swing.filechooser.FileFilter;public class MiniDrawPad extends JFrame implements MouseListener, MouseMotionListener /基本属性定义MainMenu mainMenu;/主菜单JTabbedPane tab;/多工具栏ToolBar toolBar;/主工具栏ArrayList button;/主工具栏的一排按钮String names = /主工具栏按钮名pencil, line, oval, rect, orect, tria, eraser, word, color, full, undo;JComboBox strokeitem;/线条粗细选择框DrawPanel board;/画图面板File fileName = null;/打开保存的文件名String oldName;/原窗口名标记Drawings pic = new Drawings5000;/图形单元存储数组int index=0;/当前图形存储位置int shape=0;/形状标记int stroke = 1;/线条粗细标记int lengthCount;/铅笔或橡皮擦图形的存储长度int bold, italic;/粗体与斜体int size=3;/文字大小String font;/文字字体Color color = Color.black;/颜色标记Color anticolor = Color.white;boolean fill = false;/填充标记int tag=0; /标记文件第一次打开/构造主面板public MiniDrawPad(String s) super(s);oldName = s;tab = new JTabbedPane();board = new DrawPanel();mainMenu = new MainMenu();toolBar = new ToolBar();board.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize();board.addMouseListener(this);board.addMouseMotionListener(this);this.setJMenuBar(mainMenu);tab.add(主工具栏, toolBar);this.add(tab, BorderLayout.NORTH);this.getContentPane().add(board, BorderLayout.CENTER);this.setExtendedState(JFrame.MAXIMIZED_BOTH);this.setBounds(350, 35, 555, 655);this.setVisible(true);initItem();/初始化当前图形单元this.addWindowListener(new WindowAdapter() Overridepublic void windowClosing(WindowEvent e) / TODO Auto-generated method stubif(getTitle().equals(oldName+*) /关闭前确定保存Object options = 确定, 取消 ; int choice = JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(board), 文件未保存,是否保存?, 提示, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options0);if(choice = 0) mainMenu.saveFile();System.exit(0););/注册全局监听Toolkit tk = Toolkit.getDefaultToolkit(); tk.addAWTEventListener(new ImplAWTEventListener(), AWTEvent.KEY_EVENT_MASK);/自定义键盘监听器class ImplAWTEventListener implements AWTEventListener Override public void eventDispatched(AWTEvent event) if (event.getClass() = KeyEvent.class) / 被处理的事件是键盘事件. KeyEvent keyEvent = (KeyEvent) event; if (keyEvent.getID() = KeyEvent.KEY_PRESSED) /按下时你要做的事情 keyPressed(keyEvent); private void keyPressed(KeyEvent event) if(event.isControlDown() & event.getKeyCode() = KeyEvent.VK_Z) undo(); if(event.isControlDown() & event.getKeyCode() = KeyEvent.VK_S) mainMenu.saveFile(); /菜单面板类class MainMenu extends JMenuBar /构造菜单面板public MainMenu() JMenu File, Paint, Setting, Help;JMenuItem newfile, open, save, exit, moreShape, background, tip;/创建菜单File = new JMenu(File);Paint = new JMenu(Paint);Setting = new JMenu(Setting);Help = new JMenu(Help);/File菜单下newfile = new JMenuItem(new);open = new JMenuItem(open);save = new JMenuItem(save);exit = new JMenuItem(exit);/Paint菜单下moreShape = new JMenuItem(moreShape);/Setting菜单下background = new JMenuItem(background);/Help菜单下tip = new JMenuItem(tip);this.add(File);File.add(newfile);File.addSeparator();File.add(open);File.addSeparator();File.add(save);File.addSeparator();File.add(exit);this.add(Paint);Paint.add(moreShape);this.add(Setting);Setting.add(background);this.add(Help);Help.add(tip);this.setBackground(Color.cyan);/各个菜单项添加监听器newfile.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubnewFile(););open.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubopenFile(););save.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubsaveFile(););exit.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(getTitle().equals(oldName+*) Object options = 确定, 取消 ; int choice = JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(board), 文件未保存,是否保存?, 提示, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options0);if(choice = 0) saveFile();System.exit(0););moreShape.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubaddTab(多边形工具栏););background.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubboard.setBackground(anticolor = JColorChooser.showDialog(JOptionPane.getFrameForComponent(board), Choose Color, MiniDrawPad.this.color);if(anticolor = Color.black)color = Color.WHITE;else color = Color.black;initItem(););tip.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubJOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(board), 本作品仅供学习交流n由于作者能力有限,本作品仍有许多不足之处,欢迎提出改进之处。nEmail:1368663435););/新建文件的实现public void newFile() if(getTitle().equals(oldName+*) Object options = 确定, 取消 ; int choice = JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(board), 文件未保存,是否保存?, 提示, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options0);if(choice = 0) saveFile();index = 0; shape = 0; color = Color.black; stroke = 1; strokeitem.setSelectedIndex(0); initItem(); board.repaint();/将有关值设置为初始状态,并且重画 isSave(true); fileName = null; /打开文件的实现public void openFile() if(getTitle().equals(oldName+*) Object options = 确定, 取消 ; int choice = JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(board), 文件未保存,是否保存?, 提示, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options0);if(choice = 0) saveFile();JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); JpgFilter jpg = new JpgFilter(); BmpFilter bmp = new BmpFilter(); fileChooser.addChoosableFileFilter(jpg); fileChooser.addChoosableFileFilter(bmp); fileChooser.setFileFilter(jpg); int result = fileChooser.showOpenDialog(MiniDrawPad.this); if (result = JFileChooser.CANCEL_OPTION) return; fileName = fileChooser.getSelectedFile(); if(!fileName.getName().endsWith(fileChooser.getFileFilter().getDescription() JOptionPane.showMessageDialog(MiniDrawPad.this, 文件格式错误!); return; fileName.canRead(); if (fileName = null | fileName.getName().equals() JOptionPane.showMessageDialog(fileChooser, Invalid File Name, Invalid File Name, JOptionPane.ERROR_MESSAGE); BufferedImage image;try tag = 1;index = 0;shape = -1;image = ImageIO.read(fileName);initItem();picindex.image = image;picindex.board = board;board.repaint();index+;shape = 0;initItem(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/保存文件的实现/保存图形文件程序段public void saveFile() if(fileName = null) JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); JpgFilter jpg = new JpgFilter(); BmpFilter bmp = new BmpFilter(); fileChooser.addChoosableFileFilter(jpg); fileChooser.addChoosableFileFilter(bmp); fileChooser.setFileFilter(jpg); int result = fileChooser.showSaveDialog(MiniDrawPad.this); if (result = JFileChooser.CANCEL_OPTION) return; fileName = fileChooser.getSelectedFile(); if(!fileName.getName().endsWith(fileChooser.getFileFilter().getDescription() String t = fileName.getPath() + fileChooser.getFileFilter().getDescription(); fileName = new File(t); fileName.canWrite(); if (fileName = null | fileName.getName().equals() JOptionPane.showMessageDialog(fileChooser, Invalid File Name, Invalid File Name, JOptionPane.ERROR_MESSAGE); BufferedImage image = createImage(board);try ImageIO.write(image, png, fileName); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();isSave(true);/创建image,由saveFile方法调用public BufferedImage createImage(DrawPanel panel) /int totalWidth = panel.getPreferredSize().width;/int totalHeight = panel.getPreferredSize().height;/BufferedImage panelImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);int width = MiniDrawPad.this.getWidth();int height = MiniDrawPad.this.getHeight();BufferedImage panelImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g2D = (Graphics2D) panelImage.createGraphics();g2D.setColor(Color.WHITE);g2D.fillRect(0, 0, width, height);g2D.translate(0, 0);panel.paint(g2D);/将画板内容画到panelImage上g2D.dispose();return panelImage;/文件过滤class JpgFilter extends FileFilter Overridepublic boolean accept(File f) / TODO Auto-generated method stubif(f.isDirectory() return true;return f.getName().endsWith(.jpg);Overridepublic String getDescription() / TODO Auto-generated method stubreturn .jpg;class BmpFilter extends FileFilter Overridepublic boolean accept(File f) / TODO Auto-generated method stubif(f.isDirectory() return true;return f.getName().endsWith(.bmp);Overridepublic String getDescription() / TODO Auto-generated method stubreturn .bmp;/主工具栏类class ToolBar extends JToolBar implements MouseListener, ItemListener /界面构造public ToolBar() this.setLayout(new FlowLayout(FlowLayout.LEFT);button = new ArrayList(names.length);for(int i=0; inames.length; i+) if(i = 10) strokeitem = new JComboBox();for(int j=1; j=10; j+) strokeitem.addItem( + j);this.add(strokeitem);this.addSeparator();strokeitem.setMaximumSize(getPreferredSize();strokeitem.addItemListener(this);button.add(i, new JButton(new ImageIcon(ClassLoader.getSystemResource(icon/ +namesi + .ico);this.add(button.get(i);if(i = 6 | i = 7 | i = 8 | i = 9 | i = 10) this.addSeparator();for(int j=0; jnames.length; j+)button.get(j).addMouseListener(this);/MouseListener监听器/实现按钮功能public void mousePressed(MouseEvent e) JButton b = (JButton)e.getSource();/createNewItem();switch(button.indexOf(b) case 0:shape = 0;break;case 1:shape = 1;break;case 2:shape = 2;break;case 3:shape = 3;break;case 4:shape = 4;break;case 5:shape = 5;break;case 6:shape = 6;break;case 7:shape = 7;addTab(文本工具栏);break;case 8:color = JColorChooser.showDialog(this, Choose Color, color);break;case 9:fill = !fill;break;case 10:undo();break;initItem();/提示按钮功能public void mouseEntered(MouseEvent e) JButton b = (JButton)e.getSource();switch(button.indexOf(b) case 0:b.setToolTipText(铅笔);break;case 1:b.setToolTipText(直线);break;case 2:b.setToolTipText(椭圆);break;case 3:b.setToolTipText(矩形);break;case 4:b.setToolTipText(圆角矩形);break;case 5:b.setToolTipText(三角形);break;case 6:b.setToolTipText(橡皮擦);break;case 7:b.setToolTipText(文字);break;case 8:b.setToolTipText(颜色);break;case 9:b.setToolTipText(填充);break;case 10:b.setToolTipText(撤销);break;/以下方法无需实现Overridepublic void mouseClicked(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseReleased(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseExited(MouseEvent e) / TODO Auto-generated method stub/ItemListener监听器Overridepublic void itemStateChanged(ItemEvent e) / TODO Auto-generated method stubstroke = strokeitem.getSelectedIndex();initItem();/文本工具栏类class WordToolBar extends JToolBar implements ItemListener, MouseListener /组件定义ArrayList al = new ArrayList();JToggleButton bold, italic;JComboBox font, size;JButton color, undo, exit;String fonts = , 宋体, 楷体, 微软雅黑, 黑体, Consolas;/界面构造public WordToolBar() font = new JComboBox();size = new JComboBox();color = new JButton(new ImageIcon(ClassLoader.getSystemResource(icon/ + color.ico);undo = new JButton(new ImageIcon(ClassLoader.getSystemResource(icon/ + undo.ico);exit = new JB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 急救护理中的生命体征监测标准
- 2025年广东省税务系统遴选面试真题带答案详解
- 2025年度七年级地理下册期末模拟试卷及答案(一)
- 差班家长会课件
- 工笔画介绍教学课件
- 工程项目管理第三版课件
- 工程质量标准化管理课件
- 2025版酒店行业临时客房服务员派遣服务合同
- 二零二五年度定制化班车租赁合同模板
- 采购合同详细事项条款
- 代加工合作合同书范例
- 研发分布式光伏智慧运维全过程管控平台
- 胆管恶性肿瘤
- 2024财务共享发展趋势洞察报告
- 青少年健康饮食推广活动方案
- 略阳县金子山尾矿库闭库工程施工组织设计
- DB34∕T 3251-2018 芜湖铁画锻制技术规程
- ISO5001能源管理体系法律法规标准清单
- 《胃癌腹膜转移诊治中国专家共识(2023版)》解读
- 2024年江苏省生态环境监测专业技术人员大比武理论试题库(含答案)
- 适老化改造项目施工方案
评论
0/150
提交评论