已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
注:班里的可以向我要工程文件实验一 设计模式综合应用(一)一、实验目的:熟练掌握Java设计模式中的命令模式和观察者模式,并培养学生将两者综合应用到具体软件项目中的能力。二、实验内容:制作如图1所示GUI界面,需求如下:1. 鼠标左键点击界面时,在鼠标所在位置填充一个直径为20像素的圆,并在界面上方的标签上显示“新增圆点位于:(x,y)”;2. 鼠标右键点击时,则实现undo操作,将最后填充的圆点从界面中删除,并在界面上方的标签上显示“删除圆点位于:(x,y)”;3. 界面下方的标签随时显示“鼠标位于:(x,y)”;图1 GUI界面三、实验要求:1. 绘制和撤销圆点使用命令模式;2. 两个标签内容的变更使用观察者模式;3. 在代码实现之前,进行UML类图设计;4. 根据UML类图,在eclipse中编程实现程序的功能。四、实验学时:2+2学时(课外2个学时)五、提示:1设计一个Circle类,该类对象用来记录某个填充圆的信息;2. 每填充一个圆点,就实例化一个Circle类对象,并将其放置到具体命令对象关联的List对象中,用来作为undo操作的依据;3. 填充圆可以使用Graphics的fillOval方法;4. 删除圆可以先将Graphics对象的颜色设置为画布的背景色,再使用Graphics的fillRect方法;5. 标签显示内容的需求不用观察者模式就可以轻松实现,但要求使用观察者模式进行设计;5. 实验完成后,将UML文件和程序的工程文件打包,命名为“实验一.rar”,并上传至2。六 UML图七 源代码1.package lsu.egg.sy1;public class Circle private int x;private int y;Circle(int x, int y) this.x = x;this.y = y;public int getX() return x;public void setX(int x) this.x = x;public int getY() return y;public void setY(int y) this.y = y;2.package lsu.egg.sy1;import javax.swing.JPanel;public interface Command public void excute(JPanel jpanel, Circle circle);public void undo(JPanel jpanel);3.package lsu.egg.sy1;import java.util.ArrayList;import javax.swing.JPanel;public class ConcreteCommand implements Command private Receiver receiver;private ArrayList arraylist;private ConcreteOfInvoker concreteofinvoker;ConcreteCommand(Receiver receiver, ConcreteOfInvoker concreteofinvoker) this.receiver = receiver;this.arraylist = new ArrayList();this.concreteofinvoker = concreteofinvoker;Overridepublic void excute(JPanel jpanel, Circle circle) receiver.addSpot(jpanel, circle);arraylist.add(circle);Overridepublic void undo(JPanel jpanel) Circle circle;if(arraylist.size()=1)circle = arraylist.get(arraylist.size() - 1);receiver.deleteSpotr(jpanel, circle);arraylist.remove(arraylist.size() - 1);int n = 2;concreteofinvoker.notifyObservers(n, circle.getX(), circle.getY();4. package lsu.egg.sy1;import javax.swing.JLabel;SuppressWarnings(serial)public class ConcreteObserver1 extends JLabel implements Observer ConcreteObserver1() super(新增圆点位于:(0,0);Overridepublic void update(int n, int x, int y) if(n=1)this.setText(新增圆点位于:( + x + , + y + );else if(n=2)this.setText(删除圆点位于:( + x + , + y + );5. package lsu.egg.sy1;import javax.swing.JLabel;SuppressWarnings(serial)public class ConcreteObserver2 extends JLabel implements Observer Overridepublic void update(int n, int x, int y) this.setText(鼠标位于:( + x + , + y + );6. package lsu.egg.sy1;public interface ConcreteOfInvoker public void notifyObservers(int n, int x ,int y);7.package lsu.egg.sy1;import java.util.ArrayList;public class ConcreteSubject implements ConcreteOfInvoker, Subject ArrayList arraylist;ConcreteSubject() arraylist = new ArrayList();Overridepublic void addObserver(Observer observer) arraylist.add(observer);Overridepublic void deleteObserver(Observer observer) arraylist.remove(observer);Overridepublic void notifyObservers(int n, int x ,int y) for(Observer observer : arraylist)observer.update(n, x ,y);8.package lsu.egg.sy1;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import javax.swing.JPanel;SuppressWarnings(serial)public class Invoker extends JPanel private Command command;private ConcreteOfInvoker concreteofinvoker;Invoker(Command command, ConcreteOfInvoker concreteofinvoker) mand = command;this.concreteofinvoker = concreteofinvoker;this.addMouseListener(new Mouse();this.addMouseMotionListener(new MouseMotion();class Mouse implements MouseListener Overridepublic void mouseClicked(MouseEvent arg0) excuteCommand(arg0);Overridepublic void mouseEntered(MouseEvent arg0) / TODO 自动生成的方法存根Overridepublic void mouseExited(MouseEvent arg0) / TODO 自动生成的方法存根Overridepublic void mousePressed(MouseEvent arg0) / TODO 自动生成的方法存根Overridepublic void mouseReleased(MouseEvent arg0) / TODO 自动生成的方法存根class MouseMotion implements MouseMotionListener Overridepublic void mouseDragged(MouseEvent arg0) / TODO 自动生成的方法存根Overridepublic void mouseMoved(MouseEvent arg0) int n = 0;concreteofinvoker.notifyObservers(n, arg0.getX(), arg0.getY();public void excuteCommand(MouseEvent mouseevent) int x = mouseevent.getX();int y = mouseevent.getY();Circle circle = new Circle(x, y);if(mouseevent.getButton()=MouseEvent.BUTTON1)mand.excute(this, circle);int n = 1;concreteofinvoker.notifyObservers(n, mouseevent.getX(), mouseevent.getY();else if(mouseevent.getButton()=MouseEvent.BUTTON3)mand.undo(this);9.package lsu.egg.sy1;public interface Observer public void update(int n, int x, int y);10.package lsu.egg.sy1;import java.awt.Color;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.geom.Ellipse2D;import javax.swing.JPanel;public class Receiver private Graphics2D graphics2D;public void addSpot(JPanel jpanel, Circle circle) Shape shape = new Ellipse2D.Double(circle.getX()-10, circle.getY()-10, 20, 20);graphics2D = (Graphics2D) jpanel.getGraphics();graphics2D.setPaint(Color.black);graphics2D.fill(shape);public void deleteSpotr(JPanel jpanel, Circle circle) Shape shape = new Ellipse2D.Double(circle.getX()-10, circle.getY()-10, 20, 20);graphics2D = (Graphics2D) jpanel.getGraphics();graphics2D.setPaint(jpanel.getBackground();graphics2D.fill(shape);11. package lsu.egg.sy1;public interface Subject public void addObserver(Observer observer);public void deleteObserver(Observer observer);12.package lsu.egg.sy1;import javax.swing.JFrame;SuppressWarnings(serial)public class Test extends JFrameTest() Receiver receiver = new Receiver();ConcreteSubject co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 复方扑热息痛片行业深度研究报告
- 中国手动偏三星卷板机项目投资可行性研究报告
- 2025年带有详细说明的房屋租赁合同范本
- 对硝基苄溴行业深度研究报告
- 中国超硬磨料项目投资可行性研究报告
- 中国CT22E工业吸尘器项目投资可行性研究报告
- 膝关节外侧副韧带损伤的护理个案
- 2025电视剧拍摄场地租赁合同模板
- 2025年大学《密码科学与技术-密码算法实现与优化》考试参考题库及答案解析
- 2025年大学《土木水利与海洋工程-水力学与流体力学》考试模拟试题及答案解析
- 农业穿戴设备的人体工学设计
- 2025年高中英语50篇语法填空记高考3500词(翻译及原文版)
- 2025年安徽芜湖市南陵县县属国有企业招聘笔试参考题库含答案解析
- 【产业图谱】2024年四川低空经济产业全景图谱(附产业布局、发展现状、重点企业等)
- 《家乡历史名人》课件
- 新生儿咽下综合症护理查房
- 用火用电安全管理制度
- 颈椎病课件完整版
- 中远海运集团笔试题库2025
- DGJ 08-107-2015 公共建筑节能设计标准
- 员工规章制度范文(2篇)
评论
0/150
提交评论