Java程序设计实训报告.doc_第1页
Java程序设计实训报告.doc_第2页
Java程序设计实训报告.doc_第3页
Java程序设计实训报告.doc_第4页
Java程序设计实训报告.doc_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

陕西国际商贸学院Java实训报告 专业: 计算机科学与技术 班级: 姓名: 学号: 指导老师:前 言Java自问世以来,以其得天独厚的优势,在IT业界掀起了研究、开发它的浪潮,显示了鹏程万里的美好前景。作为一种与底层硬件无关的、“编写一次,到处运行”的高级语言和计算平台,Java天生就具有将网络上的各个平台连成一体的能力,真正实现了“网络就是计算机”的理念。以Java为代表的网络的成长,改变了我们的联系方式、生活方式、学习方式和经营方式。它的来到使我们目睹了一场类似印刷术的重大变革。毫无疑问,它将影响人类社会的发展,这就是:Java加速人类进入网络时代!本次课程设计是配合JAVA程序设计课程,帮助我们深入学习掌握JAVA语言,熟练运用这个工具来编写的。通过课程设计各个项目的综合训练,不仅培养学生实际分析问题、编程和动手能力,而且还提高了学生的综合素质。本课程设计尝试使用一些较生动的示例和设计项目,激发学生学习兴趣,引导学生主动学习以及正确理解、接受需要重点掌握的知识点,为参加项目开发及更深入学习做好准备。 本次课程设计主要是用JAVA实现围棋系统,能够实现画笔、画直线、画圆形、并修改该画笔的大小以及颜色。该围棋程序还需具有悔棋功能以及选择棋盘大小的功能。 2014年6月11日目 录一、实训目的及要求4二、概要设计5三、详细设计6四、功能分析7五、测试数据及运行结果10六、总结14 一、实训目的及要求1. 1 实训目的1.使学生熟练掌握,java编成的基础知识,以及巩固图形界面及绘图知识点。2.结合软件工程,熟悉软件开发过程。3.训练了学生学生独立编程以及调试程序的能力。4.训练学生对于算法的理解和设计能力。5.加强了学生实际动手能力,以及对于面向对象方法学的理解。6.巩固图形界面排版,以及界面的设计能力。7.加强学生对于所学知识的理解及运用。1.2 实训要求1.需交电子版和纸制板实训报告各一份,电子版海源程序。2.程序必须可运行。3.界面必须保证美观。4.操作简单易学。5.各项功能不可缺损。6.报告版面美观,易读。二、概要设计1. 整个围棋程序分为3个部分:棋盘、棋子、围棋主类软件结构图(见图一): 围棋程序控制接收鼠标事件 控制面板调控度 棋手操作重新开始控制 悔棋控制 棋盘大小控制更新棋盘更新标签棋盘控制绘制棋盘及棋子 图1 软件结构图2.围棋软件中的棋盘主类都将采用绘制。3.整个程序界面将采用固定大小。 三、详细设计 棋手围棋程序处理棋盘和标签棋子信息1.围棋程序顶层数据流程图如下: 鼠标操作信息 棋盘标签信息图2 顶层数据流程图鼠标坐标棋手重新开始处理棋盘大小选择处理下棋位置捕获处理悔棋处理棋盘大小处理绘制将要下棋位置处理下棋处理棋子信息棋子信息棋盘标签信息 更新棋盘标签鼠标操作信息2功能级数据流图如下:棋子信息棋盘大小棋盘大小信息重置棋盘信息鼠标坐标图3 功能级数据流程图 四、功能分析 整个围棋程序分为3个部分:棋盘、棋子、围棋主类1. 棋盘主类主要功能有:1 绘制棋谱及落棋,程序实现重写的pint方法如下:public void paint(Graphics g) offg.setColor(new Color(180, 150, 100);offg.fillRect(0, 0, size, size);/画出棋盘格子offg.setColor(Color.black);for (int i = 0; i gridNum + 1; i+) int x1 = left + i * gridLen;int x2 = x1;int y1 = top;int y2 = top + gridNum * gridLen;offg.drawLine(x1, y1, x2, y2);x1 = left;x2 = left + gridNum * gridLen;y1 = top + i * gridLen;y2 = y1;offg.drawLine(x1, y1, x2, y2);/画出棋子for (int i = 0; i gridNum + 1; i+)for (int j = 0; j gridNum + 1; j+) if (mapij = null)continue;offg.setColor(mapij.color = Chesspoint.black ? Color.black: Color.white);offg.fillOval(left + i * gridLen - chessmanLength / 2, top + j* gridLen - chessmanLength / 2, chessmanLength,chessmanLength);/画出鼠标的位置,即下一步将要下的位置if (mouseLoc != null) offg.setColor(currentTurn = Chesspoint.black ? Color.gray: new Color(200, 200, 250);offg.fillOval(left + mouseLoc.x * gridLen - chessmanLength / 2, top+ mouseLoc.y * gridLen - chessmanLength / 2,chessmanLength, chessmanLength);/把画面一次性画出g.drawImage(offscreen, 80, 0, this);2 吃子,程序实现见源程序。3 悔棋,并保存所有已下的棋子,包括在棋盘上的所有棋子和被提掉的,程序实现悔棋back()方法如下:/悔棋public void back() if (alreadyNum = 0) controlPanel.setMsg(无子可悔);return;Object obj = chessman.elementAt(-alreadyNum);if (obj instanceof Chesspoint) Chesspoint qizi = (Chesspoint) obj;mapqizi.xqizi.y = null;currentTurn = qizi.color; else Vector v = (Vector) obj;for (int i = 0; i v.size(); i+) Chesspoint q = (Chesspoint) (v.elementAt(i);if (i = v.size() - 1) mapq.xq.y = null;int index = chessman.indexOf(v);chessman.setElementAt(q, index);currentTurn = q.color; else mapq.xq.y = q;controlPanel.setLabel();repaint();4 在该类中使用Vector类型保存踢掉的棋子及这个棋子本身。2棋子主类主要功能有:获得实际的棋子位置,程序实现如下: public class Chesspointpublic static int black=0,white=1;int x,y;int color;public Chesspoint(int i,int j,int c)x=i;y=j;color=c;public String toString()/ 储存x,y位置和颜色String c=(color=black?black:white);return +x+,+y+:+c;3. 围棋主类主要功能: (1)对围棋界面外观进行初始化,构造方法Chess实现如下: public Chess() this.setTitle(围棋程序);this.setLayout(new BorderLayout();jp.setSize(550, 490);jp.setLayout(new BorderLayout();jp.add(Chessboard.controlPanel, West);jp.add(qipan, Center);add(jp);jp.setSize(550, 490);this.setResizable(false);this.setLayout(new BorderLayout();this.setSize(550, 490);this.setVisible(true); (2)创建围棋对象,main方法实现如下: public static void main(String args) /开始下棋程序Chess Igo = new Chess(); 五、测试数据及运行结果1.初始界面如图图5.1所示: 图5.1运行结果图2.18x18棋盘测试如图: 图5.2运行结果图3.2.7x7棋盘测试如图: 图5.3运行结果图 4.悔棋标签反映无子可悔如图:图5.4运行结果图 六、总结时光如梭,光阴似箭,java实训一晃而过,通过这一期的实训,虽然实训的时间不长,但是总体上收获挺大的,当我们正式准备学习实训java编程技术时,让我感到非常高兴,因为java一直学的是课本知识,所以实训对于我来说是必须要学会熟练操作的。当然开始学习后也并非是想象中那样顺利,开始的学习让我异常感到学习任务的艰巨,因为学习中我遇到了很多以前未曾遇到的难点,有时后也难免会失去耐心,但是,通过老师的指导,自己的努力的练习,我顺利的化解了一道道的障碍。克服了java学习上的一道道难关,现在自己已经基本掌握了java的基础知识。我很清楚以后的路还很长,再长的路,一步步也能走完,再短的路,不迈开双脚也无法到达。任何业绩的质变都来自于量变的积累,成功不是将来才有的,而是从决定去做的那一刻起,持续累积而成,让我们将事前的忧虑,换为事前的思考和计划吧! 在实训的过程中,我深深感觉到自身所学知识的有限。以后要更加努力学习.源代码:1、Chessboard类import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.JPanel;public class Chessboard extends JPanel / 默认的棋盘方格长度及数目 public static final int _gridLen = 22, _gridNum = 19;/* * 利用Vector保存所有已下的棋子,包括在棋盘上的所有棋子和被踢掉的,若某一次 * 落子没有造成踢子,包括所有被这个棋子提掉的棋子及这个棋子本身,Vector最后 */private Vector chessman;private int alreadyNum; / 已下数目private int currentTurn; / 轮到谁下private int gridNum, gridLen; / 方格长度及数目private int chessmanLength; / 棋子的直径private Chesspoint map; / 在棋盘上的所有棋子private Image offscreen;private Graphics offg;private int size; / 棋盘的宽度及高度private int top =13, left = 13; / 棋盘左边及上边的边距private Point mouseLoc; / 鼠标的位置,即map数组中的下标public static ControlPanel controlPanel; / 控制面板/获得控制板的距离public int getWidth() return size;public int getHeight() return size;/绘制棋盘外观public Chessboard() gridNum = _gridNum;gridLen = _gridLen;chessmanLength = gridLen * 9 / 10;size = 2 * left + gridNum * gridLen;addMouseListener(new PutChess();addMouseMotionListener(new MML();setLayout(new BorderLayout(); controlPanel = new ControlPanel();startGame();public void addNotify() super.addNotify();offscreen = createImage(size, size);offg = offscreen.getGraphics();public void paint(Graphics g) offg.setColor(new Color(180, 150, 100);offg.fillRect(0, 0, size, size);/画出棋盘格子offg.setColor(Color.black);for (int i = 0; i gridNum + 1; i+) int x1 = left + i * gridLen;int x2 = x1;int y1 = top;int y2 = top + gridNum * gridLen;offg.drawLine(x1, y1, x2, y2);x1 = left;x2 = left + gridNum * gridLen;y1 = top + i * gridLen;y2 = y1;offg.drawLine(x1, y1, x2, y2);/画出棋子for (int i = 0; i gridNum + 1; i+)for (int j = 0; j gridNum + 1; j+) if (mapij = null)continue;offg.setColor(mapij.color = Chesspoint.black ? Color.black: Color.white);offg.fillOval(left +i* gridLen - chessmanLength / 2, top + j* gridLen - chessmanLength / 2, chessmanLength,chessmanLength);/画出鼠标的位置,即下一步将要下的位置if (mouseLoc != null) offg.setColor(currentTurn = Chesspoint.black ? Color.gray: new Color(200, 200, 250);offg.fillOval(left + mouseLoc.x * gridLen - chessmanLength/2 , top+ mouseLoc.y * gridLen - chessmanLength / 2,chessmanLength, chessmanLength);/把画面一次性画出g.drawImage(offscreen, 3, 0, this);/ 更新棋盘public void update(Graphics g) paint(g);/下棋子class PutChess extends MouseAdapter / 放一颗棋子public void mousePressed(MouseEvent evt) int xoff = left / 2;int yoff = top / 2;int x = (evt.getX() - xoff) / gridLen;int y = (evt.getY() - yoff) / gridLen;if (x gridNum | y gridNum)return;if (mapxy != null)return;/ *清除多余的棋子*if (alreadyNum = alreadyNum; i-)chessman.removeElementAt(i);/ *Chesspoint qizi = new Chesspoint(x, y, currentTurn);mapxy = qizi;/ *chessman.addElement(qizi);alreadyNum+;if (currentTurn = Chesspoint.black)currentTurn = Chesspoint.white;elsecurrentTurn = Chesspoint.black;/ *判断在x,y落子后,是否可以提掉对方的子tizi(x, y);/ *判断是否挤死了自己,若是则已落的子无效if (allDead(qizi).size() != 0) mapxy = null;repaint();controlPanel.setMsg(挤死自己);/ *back*chessman.removeElement(qizi);alreadyNum-;if (currentTurn = Chesspoint.black)currentTurn = Chesspoint.white;elsecurrentTurn = Chesspoint.black;return;mouseLoc = null;/ 更新控制面板controlPanel.setLabel();public void mouseExited(MouseEvent evt) / 鼠标退出时,清除将要落子的位置mouseLoc = null;repaint();private class MML extends MouseMotionAdapter / 取得将要落子的位置public void mouseMoved(MouseEvent evt) int xoff = left / 2;int yoff = top / 2;int x = (evt.getX() - xoff) / gridLen;int y = (evt.getY() - yoff) / gridLen;if (x gridNum | y gridNum)return;if (mapxy != null)return;mouseLoc = new Point(x, y);repaint();/判断在x,y落子后,是否可以踢掉对方的子public static int xdir = 0, 0, 1, -1 ;public static int ydir = 1, -1, 0, 0 ;public void tizi(int x, int y) Chesspoint qizi;if (qizi = mapxy) = null)return;int color = qizi.color;/取得棋子四周围的几个子Vector v = around(qizi);for (int l = 0; l v.size(); l+) Chesspoint q = (Chesspoint) (v.elementAt(l);if (q.color = color)continue;/若颜色不同,取得和q连在一起的所有已死的子,/若没有已死的子则返回一个空的VectorVector dead = allDead(q);/移去所有已死的子removeAll(dead);/如果踢子,则保存所有被踢掉的棋子if (dead.size() != 0) Object obj = chessman.elementAt(alreadyNum - 1);if (obj instanceof Chesspoint) qizi = (Chesspoint) (chessman.elementAt(alreadyNum - 1);dead.addElement(qizi); else Vector vector = (Vector) obj;for (int i = 0; i vector.size(); i+)dead.addElement(vector.elementAt(i);/ 更新Vector chessman中的第num个元素chessman.setElementAt(dead, alreadyNum - 1);repaint();/判断棋子周围是否有空白public boolean sideByBlank(Chesspoint qizi) for (int l = 0; l xdir.length; l+) int x1 = qizi.x + xdirl;int y1 = qizi.y + ydirl;if (x1 gridNum | y1 gridNum)continue;if (mapx1y1 = null)return true;return false;/取得棋子四周围的几个子public Vector around(Chesspoint qizi) Vector v = new Vector();for (int l = 0; l xdir.length; l+) int x1 = qizi.x + xdirl;int y1 = qizi.y + ydirl;if (x1 gridNum | y1 gridNum| mapx1y1 = null)continue;v.addElement(mapx1y1);return v;/取得连在一起的所有已死的子public Vector allDead(Chesspoint q) Vector v = new Vector();v.addElement(q);int count = 0;while (true) int origsize = v.size();for (int i = count; i origsize; i+) Chesspoint qizi = (Chesspoint) (v.elementAt(i);if (sideByBlank(qizi)return new Vector();Vector around = around(qizi);for (int j = 0; j around.size(); j+) Chesspoint a = (Chesspoint) (around.elementAt(j);if (a.color != qizi.color)continue;if (v.indexOf(a) 0)v.addElement(a);if (origsize = v.size()break;elsecount = origsize;return v;/ 从棋盘上移去中棋子public void removeAll(Vector v) for (int i = 0; i v.size(); i+) Chesspoint q = (Chesspoint) (v.elementAt(i);mapq.xq.y = null;repaint();/悔棋public void back() if (alreadyNum = 0) controlPanel.setMsg(无子可悔);return;Object obj = chessman.elementAt(-alreadyNum);if (obj instanceof Chesspoint) Chesspoint qizi = (Chesspoint) obj;mapqizi.xqizi.y = null;currentTurn = qizi.color; else Vector v = (Vector) obj;for (int i = 0; i v.size(); i+) Chesspoint q = (Chesspoint) (v.elementAt(i);if (i = v.size() - 1) mapq.xq.y = null;int index = chessman.indexOf(v);chessman.setElementAt(q, index);currentTurn = q.color; else mapq.xq.y = q; controlPanel.setLabel();repaint();/悔棋后再次前进public void forward() if (alreadyNum = chessman.size() controlPanel.setMsg(不能前进);return;Object obj = chessman.elementAt(alreadyNum+);Chesspoint qizi;if (obj instanceof Chesspoint) qizi = (Chesspoint) (obj);mapqizi.xqizi.y = qizi; else Vector v = (Vector) obj;qizi = (Chesspoint) (v.elementAt(v.size() - 1);mapqizi.xqizi.y = qizi;if (qizi.color = Chesspoint.black)currentTurn = Chesspoint.white;elsecurrentTurn = Chesspoint.black;tizi(qizi.x, qizi.y);controlPanel.setLabel();repaint();/重新开始游戏public void startGame() chessman = new Vector();alreadyNum = 0;map = new ChesspointgridNum + 1gridNum + 1;currentTurn = Chesspoint.black; controlPanel.setLabel();repaint();/控制面板类class ControlPanel extends Panel protected Label lblTurn = new Label(, Label.CENTER);protected Label lblNum = new Label(, Label.CENTER);protected Label lblMsg = new Label(, Label.CENTER);protected Choice choice = new Choice();protected Button back = new Button(悔 棋);protected Button start = new Button(重新开局);public int getWidth() return 50;public int getHeight() return size;/选择棋盘的大小public ControlPanel() setSize(this.getWidth(), this.getHeight();setLayout(new GridLayout(12, 1, 0, 10);setLabel();choice.add(18 X 18);choice.add(14 X 14);choice.add(12 X 12);choice.add(11 X 11);choice.add( 7 X 7 );choice.addItemListener(new ChessAction();add(lblTurn);add(lblNum);add(start);add(choice);add(lblMsg);add(back);back.addActionListener(new BackChess();start.addActionListener(new BackChess();setBackground(new Color(120, 120, 200);public Insets getInsets() return new Insets(5, 5, 5, 5);/悔棋private class BackChess implements ActionListener public void actionPerformed(ActionEvent evt) if (evt.getSource() = back)Chessboard.this.back();else if (evt.getSource() = start)Chessboard.this.startGame();/ 下棋动作private class ChessAction implements ItemListener public void itemStateChanged(ItemEvent evt) String s = (String) (evt.getItem();Chessboard.this.left = Chessboard.this.top =13;int rects = Integer.parseInt(s.substring(0, 2).trim(); if (rects != Chessboard.this.gridNum) if(rects18) Chessboard.this.left = Chessboard.this.top =Chessboard.this.left+(28-rects); Chessboard.this.gridLen = (_gridLen * _gridNum-Chessboard.this.left) / rects;Chessboard.this.chessmanLength = gridLen * 9 / 10; Chessboard.this.gridNum = rects;Chessboard.this.startGame(); else Chessboard.this.gridLen = (_gridLen * _gridNum) / rects; Chessboard.this.chessmanLength = gridLen * 9 / 10; Chessboard.this.gridNum = rects; Chessboard.this.startGame(); / 待下方的颜色与步数public void setLabel() lblTurn.setText(Chessboard.this.curr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论