资源描述
《 面向对象程序设计》
课程设计报告
题 目: 画图板设计与实现
院 (系): 信息科学与工程学院
专业班级: 计算机科学与技术1201班
学生姓名: 程伟
学 号: 1183011
指引教师: 吴奕
20 14 年 12 月 29 日至20 15 年 1 月 9 日
华中科技大学武昌分校制
面向对象程序设计 课程设计任务书
一、设计(调查报告/论文)题目
画图板设计与实现
二、设计(调查报告/论文)重要内容
内容:
完毕一种类似于windows画图板绘画程序。软件重要有菜单栏,工具栏,画图区构成,菜单栏具备新建,打开,保存等常用功能;工具栏可以选取顾客需要图形样式,设立参数,插入文本等操作。
基本功能与规定:
1 新建一种空图形文献:用于绘制新图形。
2 打开或保存一种图形文献,便于对图形文献管理。
3 绘制某些基本画图组件:直线,矩形,实心矩形,椭圆,实心椭圆,圆,实心圆,圆角矩形,实心圆角矩形等。
4 可以用鼠标在绘制画板上单击拖动进行随笔画。
5 橡皮擦:可以将已绘制好图形擦掉。
三、原始资料
1.《面向对象程序设计课程设计》指引书
2. 耿祥义.JAVA大学实用教程.北京:清华大学出版社,.
四、规定设计(调查/论文)成果
1.课程设计报告
2.课程设计软件
五、进程安排
布置任务,查找资料、需求分析1天
总体设计1天
详细设计1.5天
编制源程序实现3.5天
测试与修改1天
撰写课设报告2天
六、重要参照资料
[1] 张广彬. Java课程设计案例精编(第二版).北京:清华大学出版社,.
[2] 耿祥义. Java课程设计(第二版).北京:清华大学出版社,.
[3] 耿祥义.JAVA大学实用教程.北京:清华大学出版社,.
[4] 邹林达.陈国君 Java2程序设计基本.北京:清华大学出版社,.
[5] 邹林达.Java2程序设计基本实验指引.北京:清华大学,.
指引教师(签名):
20 年 月 日
目 录
1需求与总体设计……………………………………………………………………………1
1.1需求分析………………………………………………………………………1
1.2总体设计………………………………………………………………………1
2详细设计…………………………………………………………………………………3
2.1详细设计思路………………………………………………………………………3
2.2详细主代码………………………………………………………………………4
3编码实现…………………………………………………………………………………7
4系统运营与测试………………………………………………………………………………33
总结…………………………………………………………………………………37
1需求与总体设计
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 drawings[5000];
//用来存储基本图形数组
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) 华中科技大学武昌分校 ",
" 画图板程序阐明 ",
JOptionPane.INFORMATION_MESSAGE );
}
}
);
helpMenu.add(aboutItem);
bar.add(helpMenu);
items=new ImageIcon[names.length];
//创立各种基本图形按钮
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1();
//导入咱们需要图形图标,这些图标都存储在与源文献相似目录下面
for(int i=0;i<choices.length;i++)
{
//items[i]=new ImageIcon( MiniDrawPad.class.getResource(names[i] +".gif"));
//如果在jbuilder下运营本程序,则应当用这条语句导入图片
items[i]=new ImageIcon(names[i] + ".gif");
//默认在jdk或者jcreator下运营,用此语句导入图片
choices[i]=new JButton("",items[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
//将动作侦听器加入按钮里面
for(int i=3;i<choices.length-3;i++)
{
choices[i].addActionListener(handler);
}
choices[0].addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newFile();
}
}
);
choices[1].addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
loadFile();
}
}
);
choices[2].addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveFile();
}
}
);
choices[choices.length-3].addActionListener(handler1);
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
//字体风格选取
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
style1=styleNames[styles.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;j<choices.length-3;j++)
{
if(e.getSource()==choices[j])
{
currentChoice=j;
createNewItem();
repaint();}
}
}
}
//按钮侦听器ButtonHanler1类,用来侦听颜色选取、画笔粗细设立、文字输入按钮操作
public class ButtonHandler1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==choices[choices.length-3])
{
chooseColor();
}
if(e.getSource()==choices[choices.length-2])
{
setStroke();
}
if(e.getSource()==choices[choices.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() + "]");
//设立状态提示
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
//如果当前选取图形是随笔画或者橡皮擦,则进行下面操作
if(currentChoice==3||currentChoice==13)
{
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
index++;
createNewItem();
}
//如果当前选取图形式文字输入,则进行下面操作
if(currentChoice==14)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
String input;
input=JOptionPane.showInputDialog(
"Please input the text you want!");
itemList[index].s1=input;
itemList[index].x2=f1;
itemList[index].y2=f2;
itemList[index].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)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
}
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
index++;
createNewItem();
}
public void mouseEntered(MouseEvent e)
{
statusBar.setText("
展开阅读全文