Java画图板课程设计报告参考模板

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

目录
1 引言 (1)
1.1课程设计选题 (1)
1.2课程设计的目的 (1)
1.3本选题的设计背景 (1)
2 需求分析 (1)
2.1 功能需求 (1)
2.2 软件开发运行环境 (1)
3 总体设计 (2)
3.1 软件结构设计 (2)
3.1.1 结构设计 (2)
3.2软件功能模块及主要类设计 (2)
3.2.1功能描述 (2)
3.2.2总体结构图 (3)
3.2.3主要类设计 (3)
4 详细设计与实现 (4)
4.1 主界面 (4)
4.1.1主界面功能设计 (4)
4.1.2主界面设计 (4)
4.1.3主界面主要代码 (5)
4.2 菜单栏 (6)
4.2.1菜单栏功能设计 (6)
4.2.2菜单栏界面设计 (6)
4.2.3菜单栏主要代码 (9)
4.3 工具栏 (10)
4.3.1工具栏功能设计 (10)
4.3.2工具栏界面设计 (10)
4.3.3工具栏主要代码 (10)
4.4 画图区 (11)
4.4.1画图区功能设计 (11)
4.4.2画图区界面设计 (12)
4.4.3画图区主要代码 (12)
4.5 状态栏 (12)
4.5.1状态栏功能设计 (12)
4.5.2状态栏设计 (12)
4.5.3状态栏代码 (12)
5 小结和展望 (13)
参考文献 (14)
附录 (15)
使用说明书 (15)
源程序清单 (15)
1引言
1.1课程设计选题
《画图板软件开发和设计》
1.2课程设计的目的
巩固和加深以Java 语言为基础的面向对象编程技术理论知识的理解,提高实际动手编程能力的培养,掌握以Java为核心的应用软件开发方案,达到能独立阅读、编制和调试一定规模的Java程序的水平。

1.3本选题的设计背景
日常生活、学习中我们经常需要画图,简单的画图我们可以手工画图,但往往还是不能满足需求。

因此,借助计算机准确而快速的画图就十分有必要了。

好的画图工具能够很好的完成日常生活中的各种画图工作。

学习了Java语言后设计开发一个画图软件是对Java语言学习的巩固以及提高。

能够在实践中更深的理解面向对象语言,同时提高了解决问题的能力。

2需求分析
2.1 功能需求
考虑到画图板的实际需要,画图板要实现打开、保存图片,用于打开本地图片或保存当前图片至本地;选择需要绘制的图形,用于选择需要绘制的图形样式,如直线、矩形、椭圆等;设置背景色、画笔色以及画笔大小,用于设置画板背景颜色、画笔颜色和当前画笔粗细;擦除错误操作;显示帮助信息等。

2.2 软件开发运行环境
本软件开发平台: Java
本软件集成开发环境: MyEclipse
本软件运行环境: Windows xp 或以上版本
3总体设计
3.1软件结构设计
3.1.1 结构设计
①菜单栏的设计及功能实现:菜单栏主要包括文件、帮助等常见菜单。

其中文件菜单有
新建、打开、保存、另存为等功能;帮助菜单提供关于画图板的信息。

②画图区的设计:对图片的编辑处理区。

③工具栏的设计:根据实际需要选择不同的工具完成画图操作。

④其他功能设计。

3.2软件功能模块及主要类设计
3.2.1功能描述
本软件可以绘制日常生活、学习中的一般图形。

软件主要有菜单栏、工具栏、画图区组成,菜单栏具有新建、打开、保存等常见的功能;工具栏可以选择用户需要的图形样式、设置参数、插入文本等操作;画图区就是简单绘图区域,用户在画图区可自由绘制图形。

还可以根据不同的需要设置背景色,画笔大小、画笔颜色。

能够在任意点添加几何图形等。

3.2.2总体结构图
图 3.2.1 画图板总体结构图
3.2.3主要类设计
(1) Paint类
Pai n t类为主类,定义main方法;
(2) MyPaint类
MyPaint类继承JFrame类,用于实现画图板的总体构架。

包括菜单栏设计、工具栏设计、画图区创建等等。

画图板的基本功能都靠MyPaint类来实现。

(3) DrawPanel类
DrawPanel来是MyPaint类的内部类,是用来画图的。

(4) drawings类
drawings类是画图类,基本图形的单元。

其各个子类用以实现画各种图形包括以下几个子类:
1.Line类:绘制直线;
2. Rect类:绘制矩形;
3.fillRect:绘制实心矩形;
4.Oval类:绘制椭圆;
5.fillOval类:绘制实心椭圆;
6.Circle类:绘制圆;
7.fillCircle类:绘制实心圆;
8.RoundRect:绘制圆角矩形;
9.fillRoundRect类:绘制实心圆角矩形;
10.Pencil类:自由笔;
11. Rect3D 类:绘制3D矩形;
12.fillRect3D类:绘制实现3D矩形;
13.Cube类:绘制立方体;
14.Rubber类:橡皮类;
15. Word类:文本类;
4详细设计与实现
4.1 主界面
4.1.1主界面功能设计
画图板的主界面分为菜单栏、工具栏、画图区、状态栏四部分。

其中,菜单栏实现了图片的打开、新建、保存等操作;工具栏实现了画笔的各种操作;画图区实现了图片的操作面板;状态栏实现了显示鼠标当前状态。

4.1.2主界面设计
画图板的主界面如图所示,在主类中加载了系统风格,使其界面更为美观;为工具栏和工具栏按钮添加了颜色,使画图板颜色不单调;为画图板得主窗口设置图标,使画图板更有个性。

在实现画图功能的基础上使其外观更为人性化。

图 4.1.1 主界面图
4.1.3主界面主要代码
①设置窗体图标:
Toolkit kit=Toolkit.getDefaultToolkit(); //获取Toolkit实例
Image image=kit.getImage("Icons/Title.jpg"); //获取图片
setIconImage(image); //设置窗体图标
②加载系统风格:
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} //加载系统界面风格
catch (Exception e){
}
③设置鼠标样式:
setCursor(new Cursor(Cursor.HAND_CURSOR)); //设置画图板鼠标样式
④背景色:
setBackground(new Color(0,255,0));
4.2 菜单栏
4.2.1菜单栏功能设计
菜单栏包括文件、编辑、设置、帮助四部分。

其中,文件菜单实现了新建、打开、保存、退出功能;编辑菜单实现了撤销和恢复功能;设置菜单实现了画笔颜色、画笔大小和立方体宽度的设置;帮助菜单实现了关于画图板的帮助信息;
4.2.2菜单栏界面设计
1.文件菜单:
图 4.2.1 文件菜单
2.编辑菜单:
图 4.2.3 编辑菜单
3.设置菜单:
图 4.2.3 设置菜单4.帮助菜单:
图 4.2.4 帮助菜单5.打开文件窗口:
图 4.2.5 打开窗口6.撤销菜单(此功能尚不完善):
图 4.2.6 撤销操作7.画笔颜色选择:
图 4.2.7 画笔颜色选择8.画笔大小设置:
图 4.2.8 设置画笔大小9.立方体宽度设置:
图4.2.8 设置立方体宽度
10.“关于画图板”帮助:
图4.2.10 关于画图板
11.“关于作者”帮助
图4.2.11关于作者4.2.3菜单栏主要代码
菜单栏代码较多,详细代码见附录。

4.3 工具栏
4.3.1工具栏功能设计
工具栏是一系列快捷操作的按钮,分别是:新建、打开、保存、自由画笔、直线、空心矩形、实心矩形、空心椭圆、实心椭圆、圆、实心圆、空心圆角矩形、实心圆角矩形、3D矩形、3D矩形、3D长方体、橡皮、设置背景色、画笔颜色、画笔粗细、添加文字,用来实现相应的功能。

另外还为工具栏设置了按钮图片和鼠标右击事件,右击鼠标可以设置工具栏是否可拖动。

4.3.2工具栏界面设计
图4.3.1 工具栏界面
4.3.3工具栏主要代码
①工具栏按钮图标:
items=new ImageIcon[names.length];
//创建各种基本图形的按钮
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL);
buttonPanel = new JToolBar( JToolBar.HORIZONTAL);
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1();
buttonPanel.setBackground(new Color(0,255,0)); //工具栏背景色设置
//导入图形图标,图标存放在项目文件夹下的Icons目录内
for(int i=0;i<choices.length;i++){
items[i]=new ImageIcon("Icons/"+names[i]+".gif");
choices[i]=new JButton(items[i]);
choices[i].setToolTipText(tipText[i]);
choices[i].setBackground(new Color(0,255,0)); //按钮背景色设置
buttonPanel.add(choices[i]);
}
②鼠标右击事件:
// 工具栏右击菜单,设置工具栏是否可拖动
void ToolMenu() {
final JPopupMenu ToolMenu;
ToolMenu = new JPopupMenu();
final JCheckBox move = new JCheckBox("工具栏是否可拖动");
move.setBackground(new Color(0, 255, 0));
ToolMenu.add(move);
buttonPanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK)
ToolMenu.show(buttonPanel, e.getX(), e.getY());
}
});
move.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (move.isSelected()) {
buttonPanel.setFloatable(true);
} else {
buttonPanel.setFloatable(false);
}
}
});
}
4.4 画图区
4.4.1画图区功能设计
画图区可以绘制各种图形,可以进行擦除操作,还可以根据实际需要添加文本。

4.4.2画图区界面设计
图4.4.1 画图界面
4.4.3画图区主要代码
详细代码见附录。

4.5 状态栏
4.5.1状态栏功能设计
状态栏是显示鼠标的状态的信息栏,随时反馈鼠标信息。

4.5.2状态栏设计
图4.5.1 状态栏显示
4.5.3状态栏代码
部分代码如下,详细代码见附录:
public void mouseReleased(MouseEvent e){
statusBar.setText("鼠标松开:["+e.getX()+","+e.getY()+"]");
if(currentChoice==3||currentChoice==16){
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
}
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
index++;
createNewItem();
}
5小结和展望
三周的时间很快,作为Java初学者的我在三周的时间内完成画图板软件的开发、设计不免有很多漏洞。

但是重要的不是结果,而是从中学到了什么!从写字板到画图板,相同的地方不少,但不同的地方更多。

写字板是文本处理,从文本区定义到读写文件都相对容易些。

而画图板就不同了,画图面板要自己定义,还要设置各种参数。

一开始就觉得挺难入手,但是作为Java语言的入门级开发产品,不管多少困难也要坚持到最后。

通过查阅书籍、上网搜索资料以及同学间的交流,最终还是比较成功的完成了画图板软件的设计。

画图板软件的开发,使我收获了更多的知识。

而这些知识是实践中才能学得到的。

想起一句话:纸上得来终觉浅,绝知此事要躬行。

没错,学习不仅仅是大脑的思考,更要有实践的配合。

现在觉得自己掌握的Java 知识比课堂上学到的多很多,同时对其他编程语言的学习也有了一定的帮助。

软件设计向来不是一路顺风的,在开发过程中总会突发奇想的实现某些其他功能,但总是失败,比如像文本处理的撤销与恢复操作,觉得这个功能很有必要,但是没有实现。

如果画错了就只能重画了,因此解决这一问题意义很大,期待以后能够解决这一问题。

参考文献
[1] 耿祥义, 张跃平. Java大学实用教程 [M]. 北京:电子工业出版社, 2011.
[2] 陈嵩等. 新手学Java [M]. 北京:北京希望电子出版社, 2010.
附录
使用说明书
本软件由MyEclipse9.1开发,运行文件后直接进入窗口。

菜单栏有新建、打开、保存、另存为、退出功能。

可用键盘快捷键实现相应功能。

打开文件只可打开本软件生成的图片文件,再次提示用户注意。

编辑菜单尚不完善,后续版本将会解决,望用户包涵。

设置菜单可设置画笔颜色、画笔大小和立方体宽度,如有需要可在此进行设置。

帮助菜单显示本软件信息与作者信息,如有需要,请您按照相关联系方式进行联系。

由于作者水平有限,本软件不免有很多错误的地方,欢迎广大用户提出意见!
源程序清单
Paint.java:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.*;
public class Paint{
public static void main(String[] args){
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} //加载系统界面风格
catch (Exception e){
}
new MyPaint();
}
}
class MyPaint extends JFrame{
JMenuBar jmenuBar;
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", //实心圆角矩形
"3DRect", //3D矩形
"f3DRect", //实心3D矩形
"Cube", //立方体
"Rubber", //橡皮擦
"bgColor", //背景色
"Color", //画笔颜色
"Stroke", //画笔大小
"Word"//文本输入
};
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String styleNames[] = ge.getAvailableFontFamilyNames(); //加载系统字体
private Icon items[];
private String tipText[]={"新建","打开","保存","自由画笔","直线","空心矩形",
"实心矩形","空心椭圆","实心椭圆","圆","实心圆","空心圆角矩形",
"实心圆角矩形","3D矩形","3D矩形","3D长方体","橡皮","设置背景色","画笔颜色",
"画笔粗细","添加文字"}; //按钮提示说明
JToolBar buttonPanel ; //定义按钮面板
private JLabel statusBar; //显示鼠标状态的提示条
private DrawPanel drawingArea; //定义画图区域
private int width=850,height=550; //画图区域初始大小
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; //设置画笔粗细
static int thickness=10; //立方体宽度
JCheckBox bold,italic; //定义字体风格选择框
MyUndoManager myUndo;
JComboBox styles; //字体选择框
Toolkit kit=Toolkit.getDefaultToolkit(); //获取Toolkit实例
Image image=kit.getImage("Icons/Title.jpg"); //获取图片
public MyPaint(){
setTitle("画图板 1.0开发者预览版");
setLocation(200,100); //画图板窗口起始位置
setSize(850,550); //画图板大小
setVisible(true);
setIconImage(image); //设置窗体图标
setCursor(new Cursor(Cursor.HAND_CURSOR)); //设置画图板鼠标样式
drawingArea=new DrawPanel();
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(new Frame(),"你确定退出吗?","退出提示",
JOptionPane.WARNING_MESSAGE) ==
JOptionPane.OK_OPTION)
System.exit(0);
}
}); //关闭确认提示
getJMenuBar(); //获取菜单栏
myUndo = new MyUndoManager();
items=new ImageIcon[names.length];
//创建各种基本图形的按钮
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL);
buttonPanel = new JToolBar( JToolBar.HORIZONTAL);
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1();
buttonPanel.setBackground(new Color(0,255,0)); //工具栏背景色设置
//导入图形图标,图标存放在项目文件夹下的Icons目录内
for(int i=0;i<choices.length;i++){
items[i]=new ImageIcon("Icons/"+names[i]+".gif");
choices[i]=new JButton(items[i]);
choices[i].setToolTipText(tipText[i]);
choices[i].setBackground(new Color(0,255,0)); //按钮背景色设置
buttonPanel.add(choices[i]);
}
ToolMenu(); //工具栏右击事件调用
//将动作侦听器加入按钮里面
for(int i=3;i<choices.length-4;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-4].addActionListener(handler1);
choices[choices.length-3].addActionListener(handler1);
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
//字体风格选择
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(10);
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);
bold.setBackground(new Color(0,255,0));
italic.setBackground(new Color(0,255,0));
buttonPanel.add(bold);
buttonPanel.add(italic);
buttonPanel.addSeparator();
buttonPanel.add(new JLabel("字体:"));
buttonPanel.add(styles);
buttonPanel.setFloatable(false);
styles.setMinimumSize(new Dimension(100,20)); //字体选框大小设置
styles.setMaximumSize(new Dimension(120,20));
Container c=getContentPane();
c.add(buttonPanel,BorderLayout.NORTH);
c.add(drawingArea,BorderLayout.CENTER);
statusBar=new JLabel();
c.add(statusBar,BorderLayout.SOUTH);
createNewItem();
setSize(width,height);
show();
}
//按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作
public class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){
for(int j=3;j<choices.length-4;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-4]){
SetbgColor();
}
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,"在鼠标点击处添加文本",
"添加文本",RMATION_MESSAGE );
currentChoice=17;
createNewItem();
repaint();
}
}
}
//鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作
class mouseA extends MouseAdapter{
public void mousePressed(MouseEvent e){
statusBar.setText("鼠标点击:["+e.getX()+","+e.getY()+"]"); //设置状态提示
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
//如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作
if(currentChoice==3||currentChoice==16){
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
index++;
createNewItem();
}
//如果当前选择的图形式文字输入,则进行下面操作
if(currentChoice==17){
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
String input;
input=JOptionPane.showInputDialog("输入要添加的文本内容");
itemList[index].s1=input;
itemList[index].x2=f1;
itemList[index].y2=f2;
itemList[index].s2=style1;
index++;
currentChoice=17;
createNewItem();
drawingArea.repaint();
}
}
public void mouseReleased(MouseEvent e){
statusBar.setText("鼠标松开:["+e.getX()+","+e.getY()+"]");
if(currentChoice==3||currentChoice==16){
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("鼠标进入:["+e.getX()+","+e.getY()+"]");
}
public void mouseExited(MouseEvent e){
statusBar.setText("鼠标移出:["+e.getX()+","+e.getY()+"]");
}
}
//鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作
class mouseB extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
statusBar.setText("画图:["+e.getX()+","+e.getY()+"]");
if(currentChoice==3||currentChoice==16){
itemList[index-
1].x1=itemList[index].x2=itemList[index].x1=e.getX();
itemList[index-
1].y1=itemList[index].y2=itemList[index].y1=e.getY();
index++;
createNewItem();
}
else{
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
}
repaint();
}
public void mouseMoved(MouseEvent e){
statusBar.setText("鼠标位置:["+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 (j<=index){
draw(g2d,itemList[j]);
j++;
}
}
void draw(Graphics2D g2d,drawings i){
i.draw(g2d); //将画笔传入到各个子类中,用来完成各自的绘图
}
}
//新建一个画图基本单元对象的程序段
void createNewItem(){
if(currentChoice==17)//选择文本时鼠标为文本输入形
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURS OR));
else//其他情况十字形
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR _CURSOR));
switch (currentChoice){
case 3:itemList[index]=new Pencil();
break;
case 4:itemList[index]=new Line();
break;
case 5:itemList[index]=new Rect();
break;
case 6:itemList[index]=new fillRect();
break;
case 7:itemList[index]=new Oval();
break;
case 8:itemList[index]=new fillOval();
break;
case 9:itemList[index]=new Circle();
break;
case 10:itemList[index]=new fillCircle();
break;
case 11:itemList[index]=new RoundRect();
break;
case 12:itemList[index]=new fillRoundRect();
break;
case 13:itemList[index]=new Rect3D();
break;
case 14:itemList[index]=new fillRect3D();
break;
case 15:itemList[index]=new Cube();
break;
case 16:itemList[index]=new Rubber();
break;
case 17:itemList[index]=new Word();
break;
}
itemList[index].type=currentChoice;
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
itemList[index].stroke=stroke;
itemList[index].thickness=thickness;
}
//选择当前颜色程序段
public void chooseColor(){
color=JColorChooser.showDialog(MyPaint.this,"选择画笔颜色",color);
R=color.getRed();
G=color.getGreen();
B=color.getBlue();
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
}
//选择背景颜色程序段
public void SetbgColor(){
color=JColorChooser.showDialog(MyPaint.this,"选择背景颜色",color);
R=color.getRed();
G=color.getGreen();
B=color.getBlue();
drawingArea.setBackground(new Color(R,G,B));
}
//选择当前线条粗细程序段
public void setStroke(){
String input;
input=JOptionPane.showInputDialog("请输入画笔粗细值:");
stroke=Float.parseFloat(input);
itemList[index].stroke=stroke;
}
//选择立方体宽度
public void setthickness(){
String input;
input=JOptionPane.showInputDialog("请输入输入立方体宽度:");
thickness=(int) Float.parseFloat(input);
itemList[index].thickness=thickness;
createNewItem();
repaint();
}
//新建一个文件程序段
public void newFile(){
index=0;
currentChoice=3;
color=Color.black;
drawingArea.setBackground(Color.white);
stroke=1.0f;
createNewItem();
repaint(); //将有关值设置为初始状态,并且重画
}
//打开一个图形文件程序段
public void loadFile(){
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result =fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return;
File fileName=fileChooser.getSelectedFile();
fileName.canRead();
if (fileName==null||fileName.getName().equals("")) JOptionPane.showMessageDialog(fileChooser,"无效的文件名", "无效的文件名", JOptionPane.ERROR_MESSAGE);
else {
try {
FileInputStream fis=new FileInputStream(fileName);
input=new ObjectInputStream(fis);
drawings inputRecord;
int countNumber=0;
countNumber=input.readInt();
for(index=0;index< countNumber ;index++){
inputRecord=(drawings)input.readObject();
itemList[ index ] = inputRecord ;
}
createNewItem();
input.close();
repaint();
}
catch(EOFException endofFileException){
JOptionPane.showMessageDialog(this,"没有更多的记录文件", "没有找到类",JOptionPane.ERROR_MESSAGE );
}
catch(ClassNotFoundException classNotFoundException){ JOptionPane.showMessageDialog(this,"无法创建对象",
"文件终点",JOptionPane.ERROR_MESSAGE );
}
catch (IOException ioException){
JOptionPane.showMessageDialog(this,"读取文件时产生错误", "读取错误",JOptionPane.ERROR_MESSAGE );
}
}
}
//保存图形文件程序段
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,"无效的文件名", "无效的文件名",JOptionPane.ERROR_MESSAGE);
else{
try {
fileName.delete();
FileOutputStream fos=new FileOutputStream(fileName);
output=new ObjectOutputStream(fos);
output.writeInt( index );
for(int i=0;i< index ;i++){
drawings p= itemList[i];
output.writeObject(p);
output.flush(); //将所有图形信息强制转换成父类线性化存储到文件中
}
output.close();
fos.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
public JMenuBar getJMenuBar(){
if(jmenuBar == null){
JMenuBar Jmenu = new JMenuBar();
setJMenuBar(Jmenu);
JMenu filemenu = new JMenu("文件(F)");
JMenu editmenu = new JMenu("编辑(E)");
JMenu setmenu = new JMenu("设置(P)");
JMenu helpmenu = new JMenu("帮助(H)");
Jmenu.add(filemenu);
Jmenu.add(editmenu);
Jmenu.add(setmenu);
Jmenu.add(helpmenu);
JMenuItem newitem = new JMenuItem("新建(N)");
JMenuItem openitem = new JMenuItem("打开(O)");
JMenuItem saveitem = new JMenuItem("保存(S)");
JMenuItem saveasitem = new JMenuItem("另存为(A)");
JMenuItem exititem = new JMenuItem("退出(X)");
//菜单图标设置
newitem.setIcon(new ImageIcon("Icons/new.gif"));
openitem.setIcon(new ImageIcon("Icons/open.gif"));
saveitem.setIcon(new ImageIcon("Icons/save.gif"));
saveasitem.setIcon(new ImageIcon("Icons/saveas.gif"));
exititem.setIcon(new ImageIcon("Icons/close.gif"));
//快捷键设置
newitem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_N,InputEvent.CTRL_MASK));
openitem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_O,InputEvent.CTRL_MASK));
saveitem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_S,InputEvent.CTRL_MASK));
exititem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_F4,InputEvent.ALT_MASK));
filemenu.add(newitem);
filemenu.add(openitem);
filemenu.add(saveitem);
filemenu.add(saveasitem);
filemenu.addSeparator();
filemenu.add(exititem);
//新建菜单项事件
newitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
newFile();
}
});
//打开菜单项事件
openitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
loadFile();
}
});
//保存菜单项事件
saveitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
saveFile();
}
});
//另存为菜单项事件
saveasitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
saveFile();
}
});
//退出菜单项的功能实现
exititem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
if(JOptionPane.showConfirmDialog(new Frame(),
"你确定退出吗?","退出提示",
JOptionPane.WARNING_MESSAGE)==JOptionPane.OK_OPTION) System.exit(0);
}
});
//创建编辑菜单上的各个菜单项并添加到菜单上
JMenuItem undoitem = new JMenuItem("撤销(U)");
JMenuItem redoitem = new JMenuItem("恢复(R)");
undoitem.setIcon(new ImageIcon("Icons/undo.jpg")); redoitem.setIcon(new ImageIcon("Icons/redo.jpg")); undoitem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_Z,InputEvent.CTRL_MASK));
redoitem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_Y,InputEvent.CTRL_MASK));
editmenu.add(undoitem);
editmenu.add(redoitem);
//撤销菜单项的功能实现
undoitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
try {
getUndoManager().undo(); // 执行撤销操作
} catch (CannotUndoException ex) {
JOptionPane.showMessageDialog(new JFrame(),
"无法撤销!","撤销提示",
RMATION_MESSAGE);
}
}
});
//恢复菜单项的功能实现
redoitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
try {
getUndoManager().redo(); // 执行重做操作
} catch (CannotRedoException ex) {
JOptionPane.showMessageDialog(new JFrame(),
"无法恢复!","恢复提示",
RMATION_MESSAGE);
}
}
});
//创建设置菜单上的各个菜单项并添加到菜单上
JMenuItem coloritem = new JMenuItem("画笔颜色(C)");
JMenuItem strokeitem = new JMenuItem("画笔大小(S)");
JMenuItem cubeitem = new JMenuItem("立方体宽(W)");
coloritem.setIcon(new ImageIcon("Icons/Color.gif")); strokeitem.setIcon(new ImageIcon("Icons/Stroke.gif")); setmenu.add(coloritem);
setmenu.add(strokeitem);
setmenu.add(cubeitem);
coloritem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
chooseColor();
}
});
strokeitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
setStroke();
}
});
cubeitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setthickness();
}
});
//创建帮助菜单上的各个菜单项并添加到菜单上
JMenuItem findhelpitem = new JMenuItem("查看帮助(H)"); JMenuItem aboutboxitem = new JMenuItem("关于画图板(A)"); JMenuItem writeritem = new JMenuItem("关于作者(S)"); helpmenu.add(findhelpitem);
findhelpitem.setEnabled(false);
helpmenu.addSeparator();
helpmenu.add(aboutboxitem);
helpmenu.addSeparator();
helpmenu.add(writeritem);
aboutboxitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(new JFrame(),
" 画图板1.0预览版\n" +
"本软件用MyEclipse9.1开发制作!\n" +
"如有任何疑问及改善意见,随时欢迎指出,\n" +
"本人将尽最大的努力满足您的需求!\n" +
"谢谢您的使用!\n版权所有,请勿侵权!\n" +
"如有雷同,纯属抄袭O(∩_∩)O ","关于画图板",
RMATION_MESSAGE);
}
});
writeritem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(new JFrame(),
"姓名:王远远\n性别:女\n班级:软件1231n" +
"邮箱:783588970@\nQQ号:783588970\n" +
"院校:长春工程学院\n","关于作者",
RMATION_MESSAGE);
}
});
}
return jmenuBar;
}
public UndoManager getUndoManager() {
return myUndo;
}
class MyUndoManager extends UndoManager {
public void undoableEditHappened(UndoableEditEvent e) { getUndoManager().addEdit(e.getEdit());
}
}
// 工具栏右击菜单,设置工具栏是否可拖动
void ToolMenu() {
final JPopupMenu ToolMenu;
ToolMenu = new JPopupMenu();
final JCheckBox move = new JCheckBox("工具栏是否可拖动");
move.setBackground(new Color(0, 255, 0));
ToolMenu.add(move);
buttonPanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK)
ToolMenu.show(buttonPanel, e.getX(), e.getY());
}
});
move.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (move.isSelected()) {
buttonPanel.setFloatable(true);
} else {
buttonPanel.setFloatable(false);
}
}
});
}
}
Drawings.java:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.Serializable;
//画图类,基本图形单元,用到串行化接口,保存时所用
class drawings implements Serializable{
int x1,y1,x2,y2; //定义坐标属性
int R,G,B; //定义色彩属性
float stroke; //定义线条粗细属性
int type; //定义字体属性
String s1;
String s2; //定义字体风格属性
int thickness;
void draw(Graphics2D g2d){
};//定义绘图函数
}
//各种基本图形单元的子类,都继承自父类drawings
//直线类
class Line extends drawings {
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new
BasicStroke(stroke,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1,y1,x2,y2);
}
}
//矩形类
class Rect extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
}
//实心矩形类
class fillRect extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-
x2),Math.abs(y1-y2));
}
}
//椭圆类
class Oval extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-
x2),Math.abs(y1-y2));
}
}
//实心椭圆
class fillOval extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-
x2),Math.abs(y1-y2));
}
}
//圆类
class Circle extends drawings{
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(R,G,B));。

相关文档
最新文档