吉林师范大学博达学院
计算机与信息科学系
课程设计报告
课程名称:JAVA课程设计
设计题目:画笔程序
专 业:信息科学技术
姓 名:李瑞雪
班 级:09级1班
学 号:0935145
指导教师:侯锟吕凯
2012 年 3 月
目 录
课程设计题目审批表... 2
完成情况一览表... 3
摘 要... 4
一、设计分析... 5
二、设计要求... 5
三、设计原理... 6
四、系统调试... 9
五、总结.... 10
附录... 10
1源代码... 10
2参考资料... 24
课程设计题目审批表
课程名称:JAVA课程设计
完成情况一览表
摘 要
本次课程设计是利用JAVA的GUI(Graphical User Interface,图形用户接口)实现画图应用程序。其目的是通过本课程设计使我们能够较全面的掌握面向对象程序设计的开发方法,以便能较全面的理解、掌握和综合运用所学的知识,提高自身的编程能力。
画图程序运用Java语言实现了作为画笔应具备的基本功能要求,作为一个画笔,它能在程序界面内实现任意书写的功能,还可以根据作图人不同的要求来改变画笔颜色、宽度以达到不同的作图效果。图片完成后可以进行保存。打开已有的图形文件可以进行修改编辑。
关键字:JAVA;图形界面;画笔;像素点;颜色
一、设计分析
在JCreator环境中利用Java程序设计语言设计一个画图工具,本次设计重点难点应在于画图工具主窗体的设计、画图的基本功能实现。
1、主窗体的设计:主类用来生成主界面,完成画图板的主要框架。主界面由菜单栏、工具栏和画图区域三部分组成。
2、画图的基本功能:
(1)打开、保存一个图形文件。
(2)设置画笔的粗细程度。
(3)设置画笔的颜色。
二、设计要求
1、根据要求查阅相关文献,对画笔程序进行设计,实现画笔的基本功能。
程序功能需求:
(1)利用JAVA语言设计一个画图应用程序。
(2)在程序界面内可改变画笔的粗细程度和颜色,根据不同的要求,设置
颜色工具条内有常用的几种颜色和自定义颜色,以达到不同的效果。
(3)可完成图形文件的保存、打开功能。还可以对打开的图形文件进行修
改编辑。
2、掌握Java图形用户界面程序的布局与设计,掌握Java图形用户界面中组件的事件处理方法。巩固加深对Java语言课程基本知识的理解与掌握,培养我们利用Java语言进行程序的编写与运用。
三、设计原理
本程序通过调用JAVA提供的BorderLayout,Button,Color,Graphics,JFrame,JPanel等基本类进行了简单的界面设计,运用了JAVA中类的继承特性实现了对父类方法的调用。
对菜单栏、工具栏和鼠标画图的事件响应进行处理。
1、菜单栏中文件的打开和保存菜单事件的处理:Java把每个文件都视作顺序字节流。如果要在Java中进行文件处理,则必须用到Java.io包。这个包定义了一些流类,选用FileInputStream和FileOutputStream,通过建立这些文件的对象便可打开文件。在程序中可以用newFile(),loadFile()和saveFile()三个函数分别实现文件的新建、打开和保存。
2、工具栏事件的处理:主要有一些Button组成,在画图类DrawPanel中定义CreateNewItem()函数处理工具栏中的画图事件。
3、鼠标画图:由于画图板主要是用来画图,所以需要在程序中添加对鼠标拖动等事件进行监听并处理。
主界面框架如图1:
图1 主界面
主界面代码如下:
mainFrame=new JFrame();
mainFrame.setTitle("画图");
mainFrame.setSize(WIDTH,HEIGHT);
mainFrame.setResizable(false);
本次画笔程序的界面框架主要包括4个部分:
(1) 菜单栏包括两个菜单:“文件”和“帮助”。每个菜单中又包括不同的
菜单选项。每个选项用于触发相应的事件。代码如下:
public void showMenu(){
menuBar=new JMenuBar();
fileMenu=new JMenu("文件");
图2 菜单栏
(2) 设置线宽工具条:用来设计线宽的选项。包括1个设置线宽的面板,
由JPanel类创建,面板定义为带提示文字的边框。设置线宽面板按6行1列的网格布局,依次将各单选按钮添加到单选按钮组和线宽面板。单选按钮用来设计画笔的线宽。每个单选按钮都触发相应的事件,然后将线宽面板添加到线宽工具条。代码如下:
JRadioButton b1=new JRadioButton(" 1 ",true);
JRadioButton b3=new JRadioButton(" 3 ");
JRadioButton b5=new JRadioButton(" 5 ");
JRadioButton b10=new JRadioButton(" 10 ");
图3 线宽工具条
(3) 设置颜色工具条:用来设置画笔的颜色。包括1个标签6个按钮,依
次添加到工具条中。代码如下:
info=new JLabel(" 设置颜色: ");
图4 颜色工具条
图5 颜色工具条中自定义颜色的弹出窗口
(4) 窗口的绘图区域:绘图区域面板,由自定义的类MyPanel创建,该类
继承自JPanel并实现了MouseMotionListener接口,作为鼠标事件的监听者,完成画笔的绘制功能。
图6 示例图
四、系统调试
经过此次课程设计,我了解到自己对JAVA语言,只学到了最基本的知识,因为在课程设计的过程中遇到了一些问题,将自己所学到的知识运用到实际例子中没有想象的那么简单。我这次设计的画笔程序是一个图形用户界面设计的实例,要将画图工具设计好就要经过以下几个步骤:设置布局、初始化组建、注册监听器、添加组件实现事务处理方法。所以需要记住大量的组件和事务处理方法。例如:Java swing是一个用于开发java图形化应用程序用户界面的开发工具包。Swing提供了JFrame、JDialog和JAppet三个通用的顶层容器类。面板类JPanel是一种最常见的中间容器。Swing中常见组件有标签、文本框、编辑框、复选框等。这些都应该是在日常的学习中积累起来的,在短时间内完成一个程序设计,需要查阅大量的资料。
五、总结
回顾起此次课程设计,至今我仍感慨颇多,从确定题目到完成整个编程,从理论到实践,在整整两个星期的日子里,学到很多很多的东西,不仅可以巩固以前所学过的知识,还学到了很多在书本上所没有学到过的知识。通过这次课程设计使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,才能真正做出东西来,从而提高自己的实际动手能力和独立思考的能力。在设计的过程中遇到问题,这毕竟独立做的,难免会遇到过各种各样的问题,同时在设计的过程中发现了自己的不足之处,对以前所学过的知识理解不够深刻,掌握不够牢固,比如说类与类之间的关系等,通过这次课程设计之,我会把以前所学过的知识重新温故。
毕业在即,在以后的工作学习中,我还会将更多的时间与精力投入到Java的学习中,不断为自己所学的知识补充新鲜血液。
附录
1源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class MyPaint{
public static final int WIDTH=600;
public static final int HEIGHT=400;
JFrame mainFrame;
JMenuBar menuBar;
JMenu fileMenu;
JMenu helpMenu;
//JMenu helpMenu;
JMenuItem openItem;
JMenuItem saveItem;
JMenuItem exitItem;
JMenuItem aboutItem;
JToolBar lineTool;
JToolBar colorTool;
JPanel setLinePanel;
JLabel info;
JButton blackButton;
JButton whiteButton;
JButton redButton;
JButton greenButton;
JButton blueButton;
JButton otherButton;
JColorChooser otherChooser;
MyPanel drawPane1;
public void showPaint(){
mainFrame=new JFrame();
mainFrame.setTitle("画图");
mainFrame.setSize(WIDTH,HEIGHT);
mainFrame.setResizable(false);
//设置用户关闭框架时的响应动作
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
showMenu();
showTool();
mainFrame.show(); }
public void showMenu(){
menuBar=new JMenuBar();
fileMenu=new JMenu("文件");
openItem=new JMenuItem("打开(O)",'O');
saveItem=new JMenuItem("保存(S)",'S');
exitItem=new JMenuItem("退出(X)",'X');
//设置打开菜单项的快捷键为Ctrl+O
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK)); saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK)); exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.loadFile();}});
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.saveFile();}});
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);}});
//定义帮助菜单
helpMenu=new JMenu("帮助",false);
//定义关于菜单,设置快捷键为A
aboutItem=new JMenuItem("关于(A)",'A');
//关于菜单项事件监听器,弹出一个消息框
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(mainFrame
," 这是一个简单的画图程序!"
,"提示",JOptionPane.INFORMATION_MESSAGE);}});
//设置菜单栏
menuBar.add(fileMenu);
menuBar.add(helpMenu);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
fileMenu.add(aboutItem);
//设置菜单栏添加到主框架
mainFrame.setJMenuBar(menuBar); }
public void showTool(){
lineTool=new JToolBar();
lineTool.setSize(WIDTH/4,HEIGHT/5*3);
//按边界布局方式将设置线宽工具条放在WE5T部分
Container contentPane=mainFrame.getContentPane();
contentPane.add(lineTool,BorderLayout.WEST);
//设置线宽面板的实现
setLinePanel=new JPanel();
setLinePanel.setSize(WIDTH/5,HEIGHT/5*4);
//为设置线宽面板定义带提示文字的边框
setLinePanel.setBorder(BorderFactory.createTitledBorder("设置线宽"));
//设置线宽面板按6行1列的网络布局
setLinePanel.setLayout(new GridLayout(6,1));
//定义单选按钮组
ButtonGroup bg=new ButtonGroup();
//定义单选按钮,设置单选按钮1为已选状态
JRadioButton b1=new JRadioButton(" 1 ",true);
JRadioButton b3=new JRadioButton(" 3 ");
JRadioButton b5=new JRadioButton(" 5 ");
JRadioButton b10=new JRadioButton(" 10 ");
b1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==e.SELECTED)
drawPane1.setLineWidth(1); }});
//单选按钮3事件监听器,设置线宽为3像素点
b3.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==e.SELECTED)
drawPane1.setLineWidth(3); }});
b5.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==e.SELECTED)
drawPane1.setLineWidth(5); }});
b10.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==e.SELECTED)
drawPane1.setLineWidth(10);}});
//将单选按钮添加到按钮组
bg.add(b1); bg.add(b3);
bg.add(b5); bg.add(b10);
//将单选按钮添加到设置线宽面板
setLinePanel.add(b1); setLinePanel.add(b3);
setLinePanel.add(b5); setLinePanel.add(b10);
//将设置线宽面板添加到设置线宽工具条
lineTool.add(setLinePanel);
//绘图区面板的实现
drawPane1=new MyPanel();
drawPane1.setSize(WIDTH/4*3,HEIGHT/5*3);
//设置绘图区面板背景色为白色
drawPane1.setBackground(Color.WHITE);
//添加绘图区监听器,监听器就是绘图面板对象本身
drawPane1.addMouseMotionListener(drawPane1);
//按边界布局方式将绘图区面板放在CENTER部分
contentPane.add(drawPane1,BorderLayout.CENTER);
//设置颜色工具条的实现
colorTool=new JToolBar();
colorTool.setSize(WIDTH,HEIGHT/5);
//按边界布局方式将设置颜包工具条放在SOUTH部分
contentPane.add(colorTool,BorderLayout.SOUTH);
info=new JLabel(" 设置颜色: ");
//定义设置颜色的按钮,用颜色图标表示要设置的颜色
blackButton=new JButton("黑");
whiteButton=new JButton("白");
redButton=new JButton("红");
greenButton=new JButton("绿");
blueButton=new JButton("蓝");
//自定义颜色设置按钮为文字按钮
otherButton=new JButton("自定义");
//将提示信息标签和设置颜色按钮添加到设置颜色工具条
colorTool.add(info); colorTool.add(blackButton);
colorTool.add(whiteButton); colorTool.add(redButton);
colorTool.add(greenButton); colorTool.add(blueButton);
colorTool.add(otherButton);
//黑色按钮事件监听器,设置颜色为黑色
blackButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.setLineColor(Color.BLACK);}});
//黑色按钮事件监听器,设置颜色为黑色
blackButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.setLineColor(Color.BLACK);}});
//白色按钮事件监听器,设置颜色为黑色
whiteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.setLineColor(Color.WHITE);}});
//红色事件监听器,设置颜色为黑色
redButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.setLineColor(Color.RED);}});
//绿色按钮事件监听器,设置颜色为黑色
greenButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.setLineColor(Color.GREEN);}});
//蓝色按钮事件监听器,设置颜色为黑色
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPane1.setLineColor(Color.BLUE);}});
//自定义按钮事件监听器,弹出调色板供用户选择颜色
//黑色按钮事件监听器,设置颜色为黑色
otherButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color color;
otherChooser=new JColorChooser();
//显示调色板对话框,返回值为所选的颜色
color=otherChooser.showDialog(mainFrame,"自定义颜色",drawPane1.getLineColor());
if(color!=null)
drawPane1.setLineColor(color);}}); }}
class Drawings implements Serializable{
public int x,y;
public int R,G,B;
public int width;
public Drawings(){ }
public void copyData(Drawings s){
x=s.x; y=s.y; R=s.R; G=s.G;
B=s.B; width=s.width; }}
class MyPanel extends JPanel implements MouseMotionListener{
private ObjectInputStream input;
private ObjectOutputStream output;
private int lineWidth=1;
private Color lineColor=Color.BLACK;
private Drawings[] itemList;
private int index=-1; //已绘制基本图元下表,初值为-1表示没有绘图
public MyPanel(){ itemList=new Drawings[20000]; }
public void setLineWidth(int w){ if(w>0)lineWidth=w; }
public void setLineColor(Color c){ lineColor=c; }
public int getLineWidth(){ return lineWidth; }
public Color getLineColor(){ return lineColor; }
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);
Drawings record;
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 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]=new Drawings(); //新建已绘图元数组
itemList[index].copyData(inputRecord); }
index=countNumber;
input.close();}
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);}
repaint(); }}
public void paintComponent(Graphics g){
super.paintComponent(g);
int j=0;
while(j<=index){
draw(g,itemList[j]);
j++;} }
void draw(Graphics g,Drawings i){ //设置颜色
Color color=new Color(i.R,i.G,i.B);
g.setColor(color);
g.fillRect(i.x,i.y,i.width,i.width); //绘制正方形图元区域,左上角坐标为 //(x,y),边长为width }
public void mouseDragged(MouseEvent e){ //鼠标拖动事件,会制图元
Graphics myCanVas=getGraphics(); //获取画笔
index++;
itemList[index]=new Drawings();
itemList[index].x=e.getX();
itemList[index].y=e.getY();
itemList[index].R=lineColor.getRed();
itemList[index].G=lineColor.getGreen();
itemList[index].B=lineColor.getBlue();
itemList[index].width=lineWidth;
myCanVas.setColor(lineColor); //设置颜色 myCanVas.fillRect(e.getX(),e.getY(),lineWidth,lineWidth);}
public void mouseMoved(MouseEvent e){}}
class TestMyPaint{
public static void main(String arg[]){
MyPaint testPaint=new MyPaint();
testPaint.showPaint();}
}
2参考资料
[1] 魏先民 Java程序设计实例教程,2008, 中国水利水电出版社
[2] 于静 Java语言程序设计,2009, 清华大学出版社
[3] 王鹏 Java Swing图形界面开发与案例详解,2008,清华大学出版社