画图系统实验报告
面向对象程序设计课外实践报告
项 目 名 称: 画图系统 所 在 班 级: 2012级 小 组 成 员: 任 课 教 师:
起 止 时 间: 2013/12/16--2013/12/31
项目基本信息
课外实践评定成绩记录
1 引 言
1.1 课程设计选题 《画图系统》 1.2 课程设计的目的
在实践中更深的学习和理解以Java 语言为基础的面向对象程序设计语言,掌握以Java 为核心的应用软件开发方案,达到能独立阅读、编制和调试一定规模的Java 程序的水平,与此同时提高我们实际动手编程的能力、解决问题的能力。程序通过调用JAVA 提供的基本类进行了简单的界面设计,运用了JAVA 中类的继承特性实现了对父类方法的调用,运用了适配器类简化了监听器类的重写工作。
1.3 课程设计的背景
在日常生活和学习中,我们经常需要画图,简单的画图我们可以手工完成,但往往还是不能满足需求。因此,借助计算机准确而快速的画图就显得十分有必要了。好的画图工具能够很好的完成日常生活中的各种画图工作。
2 需求分析
2.1功能需求
设计一个画图系统,能够实现在屏幕上任意绘画、书写的功能;能实现画直线、画椭圆、画圆、画矩形、画空心图形,画实心图形、画3D 矩形,画3D 立方体等功能;在绘图的过程中能够修改该画笔的大小以及颜色;能够改变背景颜色;该画图程序还需具有橡皮擦功能。
2.2系统的开发运行环境
本系统开发环境:MyEclipse
本系统运行环境: windows xp 或以上版本
3总体设计
3.1系统结构设计
画图系统设计的画图板主要包括菜单栏,工具栏,画图区和菜单栏四个部分。菜单栏主要包括文件、编辑、设置、帮助等常见菜单,可完成一些基本操作; 工具栏可根实际需要选择不同的工具完成画图操作;画图区能够对图片及文字进行编辑处理;菜单栏用来显示鼠标当前的状态。
3.2软件功能模块及主要类设计
3.2.1功能描述
本软件可以绘制日常生活和学习中用到的基本图形及进行文本输入。软件主要有菜单栏、工具栏、画图区、状态栏组成,菜单栏主要包括文件、编辑、设置、帮
助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出功能;编辑菜单有撤销、恢复功能;设置菜单含有对画笔颜色、画笔大小等的设置功能;帮助菜单提供画图板的帮助信息及作者信息。工具栏可以选择用户需要的图形样式、设置参数、插入文本等操作。画图区就是简单绘图区域,用户在画图区可自由绘制图形。状态栏可以显示鼠标的动态(画图、点击、松开、移出) 和位置。右击工具栏可设置工具是否可拖动。此外,还可以根据不同的需要设置背景色,画笔大小、画笔颜色。能够在任意点添加文字并设置文字样式和字体颜色。 3.2.2 功能结构图
3.2.3主要类设计
4 系统详细设计与实现
4.1主界面
4.1.1主界面功能设计
画图板的主界面分为菜单栏、工具栏、画图区、状态栏四部分。 4.1.2主界面设计
画图板的主界面如下图所示,在主类中加载了系统风格,使其界面更为美观;为工具栏和工具栏按钮添加了颜色,使画图板颜色不单调;为画图板的工具栏设置各种图标,使画图板更有个性。在实现画图功能的基础上使其外观更为美观。
4.1.3主界面主要代码
1. 加载系统风格: try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
//加载系统界面风格
catch (Exception e){
}
2. 设置窗体图标:
Toolkit kit=Toolkit.getDefaultToolkit (); //获取Toolkit 实例 Image image=kit.getImage("Icons/Title.jpg"); setIconImage(image); //设置窗体图标
//获取图片
4.2菜单栏
4.2.1菜单栏功能设计
菜单栏包括文件、编辑、设置、帮助四部分。其中,文件菜单实现了新建、打开、保存、另存为、退出功能;编辑菜单实现了撤销和恢复功能;设置菜单实现了画笔颜色、画笔大小和立方体宽度的设置;帮助菜单实现了关于画图板的帮助信息及作者的信息。
4.2.2菜单栏界面设计 1. 文件菜单:
2. 编辑菜单:
3. 设置菜单
4. 帮助菜单
5. 画笔大小
6. 画笔颜色
4.2.3菜单栏主要代码
菜单栏代码较多,详细代码见附录。
4.3工具栏
4.3.1工具栏功能设计
工具栏是一系列快捷操作的按钮,分别是:新建、打开、保存、自由画笔、直线、空心矩形、实心矩形、空心椭圆、实心椭圆、圆、实心圆、空心圆角矩形、实心圆角矩形、空心3D 矩形、实心3D 矩形、3D 长方体、橡皮、设置背景色、画笔颜色、画笔粗细、添加文字,用来实现相应的功能。另外还为工具栏设置了按钮图片和鼠标右击事件,右击鼠标可以设置工具栏是否可拖动。 4.3.2工具栏界面设计
4.3.3工具栏主要代码 1. 工具栏按钮图标
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,260,280)); //导入图形图标,图标存放在项目文件夹下的Icons 目录内 for(int i=0;i
items[i]=new ImageIcon("Icons/"+names[i]+".jpg"); choices[i]=new JButton(items[i]); choices[i].setToolTipText(tipText[i]);
//工具栏背景色设置
choices[i].setBackground(new Color(204,204,204)); //按钮背景色设置 buttonPanel.add(choices[i]);
}
2. 鼠标右击事件:
// 工具栏右击菜单,设置工具栏是否可拖动 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.3画图区主要代码
详细代码见附录。 4.5状态栏
4.5.1状态栏功能设计
状态栏是显示鼠标的状态的信息栏,随时反馈鼠标信息。 4.5.2状态栏设计
4.5.3状态栏主要代码
部分代码如下,详细代码见附录:
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();
}
5 设计小结
在本次的课程设计中,应用了java 中开发图形应用程序的常用控件、事件
处理机制、常用功能实现等。设计的此软件基本达到理想的效果,在在编写这一软件的过程中我学到了很多java 编程知识,并学会了如何快速的改正程序中的错误。从编写记事本到画图系统的设计,有很多相同的地方,但不同的地方更多。在编写菜单栏时可以借鉴记事本中的编程思想。但是画图系统增添了一行工具栏,实现了更多的功能,还用到了文件的输入输出流等。本次设计我对面向对象的程序设计有了一个总体的把握,从软件工程的角度看,有了一个完整的脉络,如需求、设计 实现、测试。编程中遇到了很多问题,通过查阅书籍、上网搜资料以及同学之间的交流讨论,最终完成了画图软件的设计。经过这两周的课程设计,使我收获了很多java 编程方面的知识,而这些知识是实践中才能学得到的。这让我想起一句话:纸上得来终觉浅,绝知此事要躬行。没错,学习不仅仅是大脑的思考,更要有实践的配合。现在觉得自己掌握的Java 知识比课堂上学到的多很多,同时对其他编程语言的学习也有了一定的帮助。
其实,这只是java 编程知识的冰山一角,我还有很多东西没有学到,仍需继续努力。但是由于所学知识有限,能力水平有限,难免有些功能还不能很好的实现,还望老师批评指正。在今后的学习中,我还会将更多的时间与精力投入到Java 的学习中,不断为自己所学的知识补充新鲜血液。
参考文献
(1) 耿祥义, 张跃平. 《Java 实用教程(第3版)》 北京:电子工业出版社, 2012.1 (2) 陈明. 《Java 语言程序设计课程实践》, 北京: 清华大学出版社, 2009.1 (3) 陈嵩 等. 新手学Java [M]. 北京:北京希望电子出版社, 2010
6 附 录
源程序清单
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", "f3DRect", "Cube", "Rubber", "bgColor", "Color", "Stroke", "Word"
};
//新建 //打开
//保存
//自由画笔 //直线
//空心矩形
//实心矩形
//空心椭圆
//实心椭圆 //圆形
//实心圆形
//圆角矩形 //实心圆角矩形 //3D矩形 //实心3D 矩形
//立方体
//橡皮擦
//背景色
//画笔颜色 //画笔大小
//文本输入
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("绘图系统第十组版"); setLocation(300,100);
//画图板窗口起始位置
//获取图片
//setBounds(200,100,850,750);//画图板大小 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,260,280));
//工具栏背景色设置
//导入图形图标,图标存放在项目文件夹下的Icons 目录内 for(int i=0;i
items[i]=new ImageIcon("Icons/"+names[i]+".jpg"); choices[i]=new JButton(items[i]); choices[i].setToolTipText(tipText[i]);
choices[i].setBackground(new Color(204,204,204)); //按钮背景色设置
}
ToolMenu(); //工具栏右击事件调用
//将动作侦听器加入按钮里面
for(int i=3;i
choices[0].addActionListener(new ActionListener(){ });
choices[1].addActionListener(new ActionListener(){ });
choices[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveFile(); }
public void actionPerformed(ActionEvent e){ }
loadFile();
public void actionPerformed(ActionEvent e){ }
newFile();
choices[i].addActionListener(handler);
});
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(){
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,255)); italic.setBackground(new Color(0,255,255)); buttonPanel.add(bold); buttonPanel.add(italic); buttonPanel.addSeparator();
buttonPanel.add(new JLabel("字体:")); buttonPanel.add(styles);
buttonPanel.setFloatable(false);
styles.setMinimumSize(new Dimension(200,30)); styles.setMaximumSize(new Dimension(220,30)); 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();
}
//字体选框大小设置
public class ButtonHandler implements ActionListener{ }
public void actionPerformed(ActionEvent e){ }
for(int j=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-4]){ }
if(e.getSource()==choices[choices.length-3]){ }
if(e.getSource()==choices[choices.length-2]){ }
if(e.getSource()==choices[choices.length-1]){
JOptionPane.showMessageDialog(null,"在鼠标点击处添加文本",
" 添加文本",JOptionPane.INFORMATION_MESSAGE );
setStroke(); chooseColor(); SetbgColor();
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){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } statusBar.setText("鼠标移出:["+e.getX()+","+e.getY()+"]"); statusBar.setText("鼠标进入:["+e.getX()+","+e.getY()+"]"); 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();
//鼠标事件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{ } repaint(); itemList[index].x2=e.getX(); itemList[index].y2=e.getY(); public void mouseMoved(MouseEvent e){ } statusBar.setText("鼠标位置:["+e.getX()+","+e.getY()+"]");
//选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中
private class checkBoxHandler implements ItemListener{ } //画图面板类,用来画图 class DrawPanel extends JPanel{ public DrawPanel(){ setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 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;
} } addMouseListener(new mouseA()); addMouseMotionListener(new mouseB()); public void paintComponent(Graphics g){ } void draw(Graphics2D g2d,drawings i){ } i.draw(g2d); //将画笔传入到各个子类中,用来完成各自的绘图 super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; //定义画笔 int j=0; while (j
} 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].stroke=stroke; itemList[index].thickness=thickness; //选择当前颜色程序段 public void chooseColor(){ } //选择背景颜色程序段 public void SetbgColor(){ } //选择当前线条粗细程序段 public void setStroke(){ } //选择立方体宽度 String input; input=JOptionPane.showInputDialog("请输入画笔粗细值:"); stroke=Float.parseFloat(input); itemList[index].stroke=stroke; color=JColorChooser.showDialog(MyPaint.this,"选择背景颜色",color); R=color.getRed(); G=color.getGreen(); B=color.getBlue(); drawingArea.setBackground(new Color(R,G,B)); 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 newFile(){ } //打开一个图形文件程序段 public void loadFile(){ JFileChooser fileChooser=new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result =fileChooser.showOpenDialog(this); if(result==JFileChooser.CANCEL_OPTION) return; index=0; currentChoice=3; color=Color.black; drawingArea.setBackground(Color.white); stroke=1.0f; createNewItem(); repaint(); //将有关值设置为初始状态,并且重画 String input; input=JOptionPane.showInputDialog("请输入输入立方体宽度:"); thickness=(int) Float.parseFloat(input); itemList[index].thickness=thickness; createNewItem(); repaint(); File fileName=fileChooser.getSelectedFile(); fileName.canRead(); if (fileName==null||fileName.getName().equals("")) JOptionPane.showMessageDialog(fileChooser,"无效的文件名", " 无效的文件名", JOptionPane.ERROR_MESSAGE);
} } try { } catch(EOFException endofFileException){ } catch(ClassNotFoundException classNotFoundException){ } catch (IOException ioException){ } JOptionPane.showMessageDialog(this,"读取文件时产生错误", " 读取错误",JOptionPane.ERROR_MESSAGE ); JOptionPane.showMessageDialog(this,"无法创建对象", " 文件终点",JOptionPane.ERROR_MESSAGE ); JOptionPane.showMessageDialog(this,"没有更多的记录文件", " 没有找到类",JOptionPane.ERROR_MESSAGE ); FileInputStream fis=new FileInputStream(fileName); input=new ObjectInputStream(fis); drawings inputRecord; int countNumber=0; countNumber=input.readInt(); for(index=0;index
//保存图形文件程序段
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
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.jpg")); openitem.setIcon(new ImageIcon("Icons/open.jpg")); saveitem.setIcon(new ImageIcon("Icons/save.jpg")); saveasitem.setIcon(new ImageIcon("Icons/saveas.jpg")); exititem.setIcon(new ImageIcon("Icons/close.jpg")); //快捷键设置 newitem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_N,InputEvent.CTRL_MASK)); openitem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_O,InputEvent.CTRL_MASK)); saveitem.setAccelerator(KeyStroke.getKeyStroke
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(){ }); //打开菜单项事件 openitem.addActionListener(new ActionListener(){ }); //保存菜单项事件 saveitem.addActionListener(new ActionListener(){ }); //另存为菜单项事件 saveasitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { } saveFile(); public void actionPerformed(ActionEvent e) { } loadFile(); public void actionPerformed(ActionEvent e){ newFile(); }
}); } //退出菜单项的功能实现 exititem.addActionListener(new ActionListener(){ }); //创建编辑菜单上的各个菜单项并添加到菜单上 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)); public void actionPerformed(ActionEvent e) { } if(JOptionPane.showConfirmDialog(new Frame(), " 你确定退出吗?"," 退出提示", JOptionPane.WARNING_MESSAGE)==JOptionPane.OK_OPTION) System.exit(0); 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(),
}); } } JOptionPane.INFORMATION_MESSAGE); //恢复菜单项的功能实现 redoitem.addActionListener(new ActionListener(){ }); //创建设置菜单上的各个菜单项并添加到菜单上 JMenuItem coloritem = new JMenuItem("画笔颜色(C)"); JMenuItem strokeitem = new JMenuItem("画笔大小(S)"); JMenuItem cubeitem = new JMenuItem("立方体宽(W)"); coloritem.setIcon(new ImageIcon("Icons/Color.jpg")); strokeitem.setIcon(new ImageIcon("Icons/Stroke.jpg")); setmenu.add(coloritem); setmenu.add(strokeitem); setmenu.add(cubeitem); coloritem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ chooseColor(); } public void actionPerformed(ActionEvent e){ } try { getUndoManager().redo(); // 执行重做操作 } catch (CannotRedoException ex) { } JOptionPane.showMessageDialog(new JFrame(), " 无法恢复!"," 恢复提示", JOptionPane.INFORMATION_MESSAGE);
strokeitem.addActionListener(new ActionListener(){ }); cubeitem.addActionListener(new ActionListener(){ }); //创建帮助菜单上的各个菜单项并添加到菜单上 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(), " 绘图系统\n" + " 本软件用MyEclipse8.5开发制作!\n" + " 如有任何疑问及改善意见,随时欢迎指出,\n" + " 本组成员将尽最大的努力满足您的需求!\n" + " 谢谢您的使用!\n!O(∩_∩)O\n" ,"关于画图板", JOptionPane.INFORMATION_MESSAGE); public void actionPerformed(ActionEvent e){ } setthickness(); public void actionPerformed(ActionEvent e){ setStroke(); }
} } }); writeritem.addActionListener(new ActionListener(){ }); } public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(new JFrame(), " 安阳师范学院\n" + " 编者:李士鹏\n" + " 第十组全体成员祝您寒假快乐\n","关于作者", JOptionPane.INFORMATION_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);
} } }); move.addActionListener(new ActionListener() { }); public void actionPerformed(ActionEvent e) { } if (move.isSelected()) { buttonPanel.setFloatable(true); public void mousePressed(MouseEvent e) { } if (e.getModifiers() == InputEvent.BUTTON3_MASK) ToolMenu.show(buttonPanel, e.getX(), e.getY()); } 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; //定义线条粗细属性 //定义字体属性
} 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));
}
//矩形类
class Rect extends drawings{
}
//实心矩形类
class fillRect extends drawings{
void draw(Graphics2D g2d){ g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); 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)); } g2d.drawLine(x1,y1,x2,y2);
} }
//椭圆类
class Oval extends drawings{
}
//实心椭圆
class fillOval extends drawings{
}
//圆类
class Circle 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.max(Math.abs(x1-x2),Math.abs(y1-y2)), Math.max(Math.abs(x1-x2),Math.abs(y1-y2))); 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)); 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 fillCircle extends drawings{
}
//圆角矩形类
class RoundRect extends drawings{
}
//实心圆角矩形类
class fillRoundRect extends drawings{
}
//随笔画类
class Pencil extends drawings{ void draw(Graphics2D g2d){ void draw(Graphics2D g2d){ } g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.fillRoundRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2),50,35); void draw(Graphics2D g2d){ } g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.drawRoundRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2),50,35); 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.max(Math.abs(x1-x2),Math.abs(y1-y2)), Math.max(Math.abs(x1-x2),Math.abs(y1-y2)));
} } g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL)); g2d.drawLine(x1,y1,x2,y2);
//3D矩形类
class Rect3D extends drawings{
}
//填充的3D 矩形类
class fillRect3D extends drawings{
}
//立方体类
class Cube extends drawings{
void draw(Graphics2D g2d){ g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); thickness=MyPaint.thickness; void draw(Graphics2D g2d){ } g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.fill3DRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2),false); void draw(Graphics2D g2d){ } g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.draw3DRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2), Math.abs(y1-y2),false);
} } } g2d.fill3DRect(Math.min(x1,x2) + i, Math.min(y1,y2) - i, Math.abs(x1-x2), Math.abs(y1-y2), true);
//橡皮擦类
class Rubber extends drawings{
}
//输入文字类
class Word extends drawings{
} void draw(Graphics2D g2d){ } g2d.setPaint(new Color(R,G,B)); g2d.setFont(new Font(s2,x2+y2,((int)stroke)*18)); if (s1!= null ) g2d.drawString(s1,x1,y1); void draw(Graphics2D g2d){ } g2d.setPaint(new Color(255,255,255)); g2d.setStroke(new BasicStroke(stroke+4,BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL)); g2d.drawLine(x1,y1,x2,y2);