Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)...
控制台程序。
在模型中表示数据视图的类用来显示草图并处理用户的交互操作,所以这种类把显示方法和草图控制器合并在一起。不专用于某个视图的通用GUI创建和操作在SketcherFrame类中处理。
模型对象包含构成草图的文本和图形。模型类可以称为SketcherModel,表示模型视图的类可以称为SketcherView.
应用程序对象全面负责管理程序涉及的其他对象之间的链接。只要应用程序类能使每个对象可用,可以访问应用程序对象的任何对象就都要与其他对象进行通信。因此,应用程序对象是对象之间的通信通道。
注意:SketcherFrame不是视图类,这个类仅仅定义应用程序窗口及其关联的GUI组件。创建SketcherView对象时,应把SketcherView对象插入到SketcherFrame对象的内容面板中,并使用内容面板的布局管理器进行管理。通过将视图类与应用程序类分开定义,就可以把草图的视图与用于和程序交互的菜单及其他组件分开。这么做的优点是显示草图的区域能拥有自己的坐标系统,这种坐标系统独立于应用程序窗口的坐标系统。
1、SketcherModel类:
1 import java.io.Serializable; 2 import java.util.Observable; 3 4 public class SketcherModel extends Observable implements Serializable { 5 // Detail of the rest of class to be filled in later... 6 private final static long serialVersionUID = 1001L; 7 }
这个类可以串行化,因为我们希望把草图保存到文件中。SketcherModel类扩展了Observable类,所以可以使用这个类把视图类注册为观察器,并将视图的任何变化自动通知给模型。当草图拥有多个视图时,这个功能就会很有用。
2、SketcherView类:
1 import javax.swing.JComponent; 2 import java.util.*; 3 import java.awt.*; 4 5 @SuppressWarnings("serial") 6 public class SketcherView extends JComponent implements Observer { 7 public SketcherView(Sketcher theApp) { 8 this.theApp = theApp; 9 } 10 11 // Method called by Observable object when it changes 12 public void update(Observable o, Object rectangle) { 13 // Code to respond to changes in the model... 14 } 15 16 // Method to draw on the view 17 @Override 18 public void paint(Graphics g) { 19 // Temporary code... 20 Graphics2D g2D = (Graphics2D)g; // Get a Java 2D device context 21 22 g2D.setPaint(Color.RED); // Draw in red 23 g2D.draw3DRect(50, 50, 150, 100, true); // Draw a raised 3D rectangle 24 g2D.drawString("A nice 3D rectangle", 60, 100); // Draw some text 25 } 26 27 private Sketcher theApp; // The application object 28 }
视图需要访问模型才能显示,但构造函数不是在视图中保存对模型的引用,而是让应用程序对象通过参数把引用传送给视图。视图对象可以使用应用程序对象来访问模型对象,如果需要,还可以访问应用程序窗口。
视图注册为模型的观察器。如果存在完全不同的对象表示模型,例如要加载新的文件,视图对象就会自动获得模型已发生改变的通知,并且能重绘视图作为响应。
3、Sketcher类:
1 // Sketching application 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 6 public class Sketcher { 7 public static void main(String[] args) { 8 theApp = new Sketcher(); // Create the application object 9 SwingUtilities.invokeLater(new Runnable() { 10 public void run() { 11 theApp.createGUI(); // Call GUI creator 12 } 13 }); 14 } 15 16 // Method to create the application GUI 17 private void createGUI() { 18 window = new SketcherFrame("Sketcher", this); // Create the app window 19 Toolkit theKit = window.getToolkit(); // Get the window toolkit 20 Dimension wndSize = theKit.getScreenSize(); // Get screen size 21 22 // Set the position to screen center & size to half screen size 23 window.setSize(wndSize.width/2, wndSize.height/2); // Set window size 24 window.setLocationRelativeTo(null); // Center window 25 26 window.addWindowListener(new WindowHandler()); // Add window listener 27 28 sketch = new SketcherModel(); // Create the model 29 view = new SketcherView(this); // Create the view 30 sketch.addObserver(view); // Register view with the model 31 window.getContentPane().add(view, BorderLayout.CENTER); 32 window.setVisible(true); 33 } 34 35 // Return a reference to the application window 36 public SketcherFrame getWindow() { 37 return window; 38 } 39 40 // Return a reference to the model 41 public SketcherModel getModel() { 42 return sketch; 43 } 44 45 // Return a reference to the view 46 public SketcherView getView() { 47 return view; 48 } 49 50 // Handler class for window events 51 class WindowHandler extends WindowAdapter { 52 // Handler for window closing event 53 @Override 54 public void windowClosing(WindowEvent e) { 55 // Code to be added here... 56 } 57 } 58 59 private SketcherModel sketch; // The data model for the sketch 60 private SketcherView view; // The view of the sketch 61 private SketcherFrame window; // The application window 62 private static Sketcher theApp; // The application object 63 }
Sketcher类中的新方法返回对应用程序窗口、模型和视图的引用,所以在Sketcher应用程序代码的任何地方,只要有对应用程序对象的引用,就可以访问它们。
在Sketcher类的createGUI()方法中创建完模型和视图对象后,就把视图注册为模型的观察器,让模型在发生变化时通知视图。
4、SketcherFrame类:
// Frame for the Sketcher application import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; import java.awt.*;import static java.awt.event.InputEvent.*; import static java.awt.AWTEvent.*; import static java.awt.Color.*; import static Constants.SketcherConstants.*; import static javax.swing.Action.*;@SuppressWarnings("serial") public class SketcherFrame extends JFrame {// Constructorpublic SketcherFrame(String title, Sketcher theApp) {setTitle(title); // Set the window titlethis.theApp = theApp; // Save app. object referencesetJMenuBar(menuBar); // Add the menu bar to the windowsetDefaultCloseOperation(EXIT_ON_CLOSE); // Default is exit the application createFileMenu(); // Create the File menucreateElementMenu(); // Create the element menucreateColorMenu(); // Create the element menu createToolbar();toolBar.setRollover(true);getContentPane().add(toolBar, BorderLayout.NORTH);}// Create File menu item actionsprivate void createFileMenuActions() {newAction = new FileAction("New", 'N', CTRL_DOWN_MASK);openAction = new FileAction("Open", 'O', CTRL_DOWN_MASK);closeAction = new FileAction("Close");saveAction = new FileAction("Save", 'S', CTRL_DOWN_MASK);saveAsAction = new FileAction("Save As...");printAction = new FileAction("Print", 'P', CTRL_DOWN_MASK);exitAction = new FileAction("Exit", 'X', CTRL_DOWN_MASK);// Initialize the arrayFileAction[] actions = {openAction, closeAction, saveAction, saveAsAction, printAction, exitAction};fileActions = actions;// Add toolbar icons newAction.putValue(LARGE_ICON_KEY, NEW24);openAction.putValue(LARGE_ICON_KEY, OPEN24);saveAction.putValue(LARGE_ICON_KEY, SAVE24);saveAsAction.putValue(LARGE_ICON_KEY, SAVEAS24);printAction.putValue(LARGE_ICON_KEY, PRINT24);// Add menu item icons newAction.putValue(SMALL_ICON, NEW16);openAction.putValue(SMALL_ICON, OPEN16);saveAction.putValue(SMALL_ICON, SAVE16);saveAsAction.putValue(SMALL_ICON,SAVEAS16);printAction.putValue(SMALL_ICON, PRINT16);// Add tooltip textnewAction.putValue(SHORT_DESCRIPTION, "Create a new sketch");openAction.putValue(SHORT_DESCRIPTION, "Read a sketch from a file");closeAction.putValue(SHORT_DESCRIPTION, "Close the current sketch");saveAction.putValue(SHORT_DESCRIPTION, "Save the current sketch to file");saveAsAction.putValue(SHORT_DESCRIPTION, "Save the current sketch to a new file");printAction.putValue(SHORT_DESCRIPTION, "Print the current sketch");exitAction.putValue(SHORT_DESCRIPTION, "Exit Sketcher");}// Create the File menuprivate void createFileMenu() {JMenu fileMenu = new JMenu("File"); // Create File menufileMenu.setMnemonic('F'); // Create shortcutcreateFileMenuActions(); // Create Actions for File menu item// Construct the file drop-down menufileMenu.add(newAction); // New Sketch menu itemfileMenu.add(openAction); // Open sketch menu itemfileMenu.add(closeAction); // Close sketch menu itemfileMenu.addSeparator(); // Add separatorfileMenu.add(saveAction); // Save sketch to filefileMenu.add(saveAsAction); // Save As menu itemfileMenu.addSeparator(); // Add separatorfileMenu.add(printAction); // Print sketch menu itemfileMenu.addSeparator(); // Add separatorfileMenu.add(exitAction); // Print sketch menu itemmenuBar.add(fileMenu); // Add the file menu }// Create Element menu actionsprivate void createElementTypeActions() {lineAction = new TypeAction("Line", LINE, 'L', CTRL_DOWN_MASK);rectangleAction = new TypeAction("Rectangle", RECTANGLE, 'R', CTRL_DOWN_MASK);circleAction = new TypeAction("Circle", CIRCLE,'C', CTRL_DOWN_MASK);curveAction = new TypeAction("Curve", CURVE,'U', CTRL_DOWN_MASK);// Initialize the arrayTypeAction[] actions = {lineAction, rectangleAction, circleAction, curveAction};typeActions = actions;// Add toolbar icons lineAction.putValue(LARGE_ICON_KEY, LINE24);rectangleAction.putValue(LARGE_ICON_KEY, RECTANGLE24);circleAction.putValue(LARGE_ICON_KEY, CIRCLE24);curveAction.putValue(LARGE_ICON_KEY, CURVE24);// Add menu item icons lineAction.putValue(SMALL_ICON, LINE16);rectangleAction.putValue(SMALL_ICON, RECTANGLE16);circleAction.putValue(SMALL_ICON, CIRCLE16);curveAction.putValue(SMALL_ICON, CURVE16);// Add tooltip textlineAction.putValue(SHORT_DESCRIPTION, "Draw lines");rectangleAction.putValue(SHORT_DESCRIPTION, "Draw rectangles");circleAction.putValue(SHORT_DESCRIPTION, "Draw circles");curveAction.putValue(SHORT_DESCRIPTION, "Draw curves");}// Create the Elements menuprivate void createElementMenu() {createElementTypeActions();elementMenu = new JMenu("Elements"); // Create Elements menuelementMenu.setMnemonic('E'); // Create shortcut createRadioButtonDropDown(elementMenu, typeActions, lineAction);menuBar.add(elementMenu); // Add the element menu }// Create Color menu actionsprivate void createElementColorActions() {redAction = new ColorAction("Red", RED, 'R', CTRL_DOWN_MASK|ALT_DOWN_MASK);yellowAction = new ColorAction("Yellow", YELLOW, 'Y', CTRL_DOWN_MASK|ALT_DOWN_MASK);greenAction = new ColorAction("Green", GREEN, 'G', CTRL_DOWN_MASK|ALT_DOWN_MASK);blueAction = new ColorAction("Blue", BLUE, 'B', CTRL_DOWN_MASK|ALT_DOWN_MASK);// Initialize the arrayColorAction[] actions = {redAction, greenAction, blueAction, yellowAction};colorActions = actions;// Add toolbar icons redAction.putValue(LARGE_ICON_KEY, RED24);greenAction.putValue(LARGE_ICON_KEY, GREEN24);blueAction.putValue(LARGE_ICON_KEY, BLUE24);yellowAction.putValue(LARGE_ICON_KEY, YELLOW24);// Add menu item icons redAction.putValue(SMALL_ICON, RED16);blueAction.putValue(SMALL_ICON, BLUE16);greenAction.putValue(SMALL_ICON, GREEN16);yellowAction.putValue(SMALL_ICON, YELLOW16);// Add tooltip textredAction.putValue(SHORT_DESCRIPTION, "Draw in red");blueAction.putValue(SHORT_DESCRIPTION, "Draw in blue");greenAction.putValue(SHORT_DESCRIPTION, "Draw in green");yellowAction.putValue(SHORT_DESCRIPTION, "Draw in yellow");}// Create the Color menuprivate void createColorMenu() {createElementColorActions();colorMenu = new JMenu("Color"); // Create Elements menucolorMenu.setMnemonic('C'); // Create shortcut createRadioButtonDropDown(colorMenu, colorActions, blueAction);menuBar.add(colorMenu); // Add the color menu }// Menu creation helperprivate void createRadioButtonDropDown(JMenu menu, Action[] actions, Action selected) {ButtonGroup group = new ButtonGroup();JRadioButtonMenuItem item = null;for(Action action : actions) {group.add(menu.add(item = new JRadioButtonMenuItem(action)));if(action == selected) {item.setSelected(true); // This is default selected }}}// Create toolbar buttons on the toolbarprivate void createToolbar() {for(FileAction action: fileActions){if(action != exitAction && action != closeAction)addToolbarButton(action); // Add the toolbar button }toolBar.addSeparator();// Create Color menu buttonsfor(ColorAction action:colorActions){addToolbarButton(action); // Add the toolbar button }toolBar.addSeparator();// Create Elements menu buttonsfor(TypeAction action:typeActions){addToolbarButton(action); // Add the toolbar button }}// Create and add a toolbar buttonprivate void addToolbarButton(Action action) {JButton button = new JButton(action); // Create from Actionbutton.setBorder(BorderFactory.createCompoundBorder( // Add button bordernew EmptyBorder(2,5,5,2), // Outside borderBorderFactory.createRaisedBevelBorder())); // Inside borderbutton.setHideActionText(true); // No label on the buttontoolBar.add(button); // Add the toolbar button }// Set radio button menu checksprivate void setChecks(JMenu menu, Object eventSource) {if(eventSource instanceof JButton){JButton button = (JButton)eventSource;Action action = button.getAction();for(int i = 0 ; i < menu.getItemCount() ; ++i) {JMenuItem item = menu.getItem(i);item.setSelected(item.getAction() == action);}}}// Inner class defining Action objects for File menu itemsclass FileAction extends AbstractAction {// Create action with a name FileAction(String name) {super(name);}// Create action with a name and acceleratorFileAction(String name, char ch, int modifiers) {super(name);putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));// Now find the character to underlineint index = name.toUpperCase().indexOf(ch);if(index != -1) {putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);}}// Event handlerpublic void actionPerformed(ActionEvent e) {// You will add action code here eventually... }}// Inner class defining Action objects for Element type menu itemsclass TypeAction extends AbstractAction {// Create action with just a name propertyTypeAction(String name, int typeID) {super(name);this.typeID = typeID;}// Create action with a name and an acceleratorprivate TypeAction(String name,int typeID, char ch, int modifiers) {this(name, typeID);putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));// Now find the character to underlineint index = name.toUpperCase().indexOf(ch);if(index != -1) {putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);}}public void actionPerformed(ActionEvent e) {elementType = typeID;setChecks(elementMenu, e.getSource());}private int typeID;}// Handles color menu itemsclass ColorAction extends AbstractAction {// Create an action with a name and a colorpublic ColorAction(String name, Color color) {super(name);this.color = color;}// Create an action with a name, a color, and an acceleratorpublic ColorAction(String name, Color color, char ch, int modifiers) {this(name, color);putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));// Now find the character to underlineint index = name.toUpperCase().indexOf(ch);if(index != -1) {putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);}}public void actionPerformed(ActionEvent e) {elementColor = color;setChecks(colorMenu, e.getSource());}private Color color;}// File actionsprivate FileAction newAction, openAction, closeAction, saveAction, saveAsAction, printAction, exitAction;private FileAction[] fileActions; // File actions as an array// Element type actionsprivate TypeAction lineAction, rectangleAction, circleAction, curveAction;private TypeAction[] typeActions; // Type actions as an array// Element color actionsprivate ColorAction redAction, yellowAction,greenAction, blueAction;private ColorAction[] colorActions; // Color actions as an arrayprivate JMenuBar menuBar = new JMenuBar(); // Window menu barprivate JMenu elementMenu; // Elements menuprivate JMenu colorMenu; // Color menuprivate Color elementColor = DEFAULT_ELEMENT_COLOR; // Current element colorprivate int elementType = DEFAULT_ELEMENT_TYPE; // Current element typeprivate JToolBar toolBar = new JToolBar(); // Window toolbarprivate Sketcher theApp; // The application object }
View Code
5、自定义常量包SketcherConstants:
1 // Defines application wide constants 2 package Constants; 3 import java.awt.Color; 4 import javax.swing.*; 5 6 public class SketcherConstants { 7 // Path for images 8 public final static String imagePath = "E:/JavaProject/BeginningJava/Images/"; 9 10 // Toolbar icons 11 public final static Icon NEW24 = new ImageIcon(imagePath + "New24.gif"); 12 public final static Icon OPEN24 = new ImageIcon(imagePath + "Open24.gif"); 13 public final static Icon SAVE24 = new ImageIcon(imagePath + "Save24.gif"); 14 public final static Icon SAVEAS24 = new ImageIcon(imagePath + "SaveAs24.gif"); 15 public final static Icon PRINT24 = new ImageIcon(imagePath + "Print24.gif"); 16 17 public final static Icon LINE24 = new ImageIcon(imagePath + "Line24.gif"); 18 public final static Icon RECTANGLE24 = new ImageIcon(imagePath + "Rectangle24.gif"); 19 public final static Icon CIRCLE24 = new ImageIcon(imagePath + "Circle24.gif"); 20 public final static Icon CURVE24 = new ImageIcon(imagePath + "Curve24.gif"); 21 22 public final static Icon RED24 = new ImageIcon(imagePath + "Red24.gif"); 23 public final static Icon GREEN24 = new ImageIcon(imagePath + "Green24.gif"); 24 public final static Icon BLUE24 = new ImageIcon(imagePath + "Blue24.gif"); 25 public final static Icon YELLOW24 = new ImageIcon(imagePath + "Yellow24.gif"); 26 27 // Menu item icons 28 public final static Icon NEW16 = new ImageIcon(imagePath + "new16.gif"); 29 public final static Icon OPEN16 = new ImageIcon(imagePath + "Open16.gif"); 30 public final static Icon SAVE16 = new ImageIcon(imagePath + "Save16.gif"); 31 public final static Icon SAVEAS16 = new ImageIcon(imagePath + "SaveAs16.gif"); 32 public final static Icon PRINT16 = new ImageIcon(imagePath + "print16.gif"); 33 34 public final static Icon LINE16 = new ImageIcon(imagePath + "Line16.gif"); 35 public final static Icon RECTANGLE16 = new ImageIcon(imagePath + "Rectangle16.gif"); 36 public final static Icon CIRCLE16 = new ImageIcon(imagePath + "Circle16.gif"); 37 public final static Icon CURVE16 = new ImageIcon(imagePath + "Curve16.gif"); 38 39 public final static Icon RED16 = new ImageIcon(imagePath + "Red16.gif"); 40 public final static Icon GREEN16 = new ImageIcon(imagePath + "Green16.gif"); 41 public final static Icon BLUE16 = new ImageIcon(imagePath + "Blue16.gif"); 42 public final static Icon YELLOW16 = new ImageIcon(imagePath + "Yellow16.gif"); 43 44 // Element type definitions 45 public final static int LINE = 101; 46 public final static int RECTANGLE = 102; 47 public final static int CIRCLE = 103; 48 public final static int CURVE = 104; 49 50 // Initial conditions 51 public final static int DEFAULT_ELEMENT_TYPE = LINE; 52 public final static Color DEFAULT_ELEMENT_COLOR = Color.BLUE; 53 }
View Code
转载于:https://www.cnblogs.com/mannixiang/p/3488325.html
Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)...相关推荐
- Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)...
控制台程序. 在Sketcher中创建形状时,并不知道应该以什么顺序创建不同类型的形状,这完全取决于使用Sketcher程序生成草图的人.因此需要绘制形状,对它们执行其他操作而不必知道图形是什么.当然 ...
- Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
控制台程序. 1 import javax.swing.JComponent; 2 import java.util.*; 3 import java.awt.*; 4 import java.awt ...
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)...
控制台程序. 1 import javax.swing.JComponent; 2 import java.util.*; 3 import java.awt.*; 4 import java.awt ...
- 【java基础知识】spring框架开发时,怎样解决mysql数据库中Timestamp到String的简单转换
Springboot框架中的sql查询使用的Mybatis,直接查询数据库数据返回的Timestamp是一串数字,并不是我们在数据库中看到的 yyyy-MM-dd HH:mm:ss 格式. 两种方式, ...
- Java基础学习总结(168)——为什么推荐在RPC的接口中入参和出参都不要使用枚举
前言: 为什么推荐在RPC的接口中入参和出参都不要使用枚举.最近,我们的线上环境出现了一个问题,线上代码在执行过程中抛出了一个IllegalArgumentException,分析堆栈后,发现最根本的 ...
- java基础,鼠标拖动拼图,求教,我的这个拼图程序中的移动图片的改怎么做
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 public class button extends JPanel implements MouseListener, ActionListener { ...
- java 绘制长方形_Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)...
控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ...
- 笔记整理:狂神说java——java基础00
笔记整理:狂神说java--Java基础00 笔记参考:b站狂神说java 视频链接:https://www.bilibili.com/video/BV12J41137hu?p=1 边学边写,欢迎浏览 ...
- Java基础day14
Java基础day14 Java基础day14-集合 1.Collection集合 1.1集合体系结构 1.2Collection集合概述和基本使用 1.3Collection集合的常用方法 1.4C ...
最新文章
- 【复盘】升级打怪第一关,冲啊!
- R语言数据结构之矩阵
- infoq 视频下载 [转老赵]
- HashMap与ConcurrentHashMap的区别
- [转]用android LinearLayout和RelativeLayout实现精确布局
- 苹果计算机磁盘格式,Mac 上“磁盘工具”中可用的文件系统格式
- 南信大滨江学院计算机考试姜青山,【数据库原理】滨江学院姜青山 期末试卷知识点笔记整理 南京信息工程大学...
- 操作系统定义、功能、特征、分类介绍
- PHP把列表数据的子级内容合并到父级
- scala教程(一)
- 锐捷交换机查看配置命令
- U盘分区,一盘两用,分为启动盘和读写盘
- php如何拼接图片路径,如何把图片拼接在一张图上?
- VMware 15 安装 macOS High Sierra 10.13 图文教程
- 95前的中年人,00后的「社交玩法」了解一下?
- Ubuntu wine QQ 微信乱码
- android点击展开全文,Android显示全文折叠控件使用方法详解
- 【ros】初学ROS的学习笔记——创建Publisher
- SSM+校园社团平台 毕业设计-附源码251554
- bazel 的安装与卸载
热门文章
- oracle支持几国语言,Oracle数据库多语言支持
- python Sina微博自动转发带抽奖字样的微博,添加关注,取消关注
- 【Magisk】猫猫也能学会的卡刷root教程
- 360手机:360Q5plus Twrp、Root、Magisk教程
- mac多个终端并排显示技巧
- 浙江省计算机选考重点知识,2020年1月浙江省普通高校招生选考科目考试信息技术试卷及答案...
- python 二维列表
- 【渝粤教育】电大中专多媒体技术基础作业 题库
- Excel CPK “逆公式“
- python 小爱音箱集成_python--抢购小米AI音响