📄 appletmousehandler.java
字号:
/****************************************************************** * Copyright (C) 2002-2006 Andrew Girow. All rights reserved. * * ---------------------------------------------------------------* * This software is published under the terms of the TinyLine * * License, a copy of which has been included with this * * distribution in the TINYLINE_LICENSE.TXT file. * * * * For more information on the TinyLine, * * please see <http://www.tinyline.com/>. * *****************************************************************/package com.tinyline.app;import java.awt.event.*;import java.awt.Graphics;import java.awt.Graphics;import java.awt.Color;import java.awt.PopupMenu;import java.awt.CheckboxMenuItem;import java.awt.MenuItem;import java.net.URL;import com.tinyline.svg.*;import com.tinyline.tiny2d.*;/** * This class represents the MouseListener associated with the * PPSVGCanvas and used for the Personal Profile SVG Viewer. * <p> * @author (C) Andrew Girow * @version 1.8.2 * <p> */public class AppletMouseHandler implements ActionListener, ItemListener, MouseListener, MouseMotionListener{ /** * The Canvas that this mouse handler uses */ PPSVGCanvas canvas; TinyLineApplet applet; PopupMenu m_menu = new PopupMenu(); // Toogle buttons CheckboxMenuItem linkButton, panButton, zoomInButton, zoomOutButton; CheckboxMenuItem current; // Function buttons MenuItem pauseButton, refreshButton, helpButton; /** * Type of the MouseHandler */ public static final int LINK_MOUSE = 0; public static final int PAN_MOUSE = 1; public static final int ZOOM_IN_MOUSE = 2; public static final int ZOOM_OUT_MOUSE = 3; public int type = LINK_MOUSE; /** * The original values to calculate pan. */ int pressedX; int pressedY; int draggedX; int draggedY; /** * Constructs a Mouse Handler object with the given canvas. The * handler will perform some function on the canvas when it * is given mouse events. * * @param canvas The canvas this mouse handler is handling. */ public AppletMouseHandler(PPSVGCanvas canvas, TinyLineApplet applet) { this.canvas = canvas; this.applet = applet; // Setup the menu linkButton = new CheckboxMenuItem ("Link",true); linkButton.addItemListener(this); m_menu.add(linkButton); current = linkButton; panButton = new CheckboxMenuItem ("Pan"); panButton.addItemListener(this); m_menu.add(panButton); zoomInButton = new CheckboxMenuItem ("Zoom In"); zoomInButton.addItemListener(this); m_menu.add(zoomInButton); zoomOutButton = new CheckboxMenuItem ("Zoom Out"); zoomOutButton.addItemListener(this); m_menu.add(zoomOutButton); m_menu.addSeparator(); // Function buttons pauseButton = new MenuItem("Pause"); pauseButton.addActionListener(this); m_menu.add(pauseButton); refreshButton = new MenuItem("Refresh"); refreshButton.addActionListener(this); m_menu.add(refreshButton); m_menu.addSeparator(); helpButton = new MenuItem("Help"); helpButton.addActionListener(this); m_menu.add(helpButton); // Add to applet, as menu must have a parent component canvas.add(m_menu); } /** * Invoked when the mouse has been clicked in the Canvas canvas. */ public void mouseClicked(MouseEvent e) { } /* * Popup menus are triggered differently on different systems. * Therefore, isPopupTrigger() should be checked in both * mousePressed() and mouseReleased() for proper cross-platform * functionality. */ private boolean evaluatePopup(MouseEvent e) {//System.out.println("e " + e.paramString()); if (e.isPopupTrigger()) {///System.out.println("e.isPopupTrigger()"); // show the pop up menu... int x = e.getX(); int y = e.getY(); m_menu.show ( canvas, x, y ); return true; } return false; } /** * Invoked when the mouse has been pressed in the canvas. */ public void mousePressed(MouseEvent e) { if(evaluatePopup(e)) return; if(type == PAN_MOUSE) { pressedX = e.getX(); pressedY = e.getY(); draggedX = pressedX; draggedY = pressedY; } } /** * Invoked when the mouse has been released in the canvas. */ public void mouseReleased(MouseEvent e) { if(evaluatePopup(e)) return; if(type == ZOOM_OUT_MOUSE) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_ZOOM, new TinyNumber(1)); canvas.postEvent(event); } else if(type == ZOOM_IN_MOUSE) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_ZOOM, new TinyNumber(0)); canvas.postEvent(event); } else if(type == LINK_MOUSE) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_CLICK, new TinyPoint(e.getX(),e.getY()) ); canvas.postEvent(event); } else if(type == PAN_MOUSE) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_SCROLL,new TinyPoint(pressedX -e.getX(),pressedY -e.getY())); canvas.postEvent(event); } } /** * Invoked when the mouse has entered the canvas. */ public void mouseEntered(MouseEvent e) {} /** * Invoked when the mouse has exited the canvas. */ public void mouseExited(MouseEvent e) {} /** * Invoked when a mouse button is pressed within the Canvas canvas and then * dragged. */ public void mouseDragged(MouseEvent e) { if(type == PAN_MOUSE && canvas.raster.document.isZoomAndPanAnable() ) { drawXORLine(pressedX,pressedY,draggedX,draggedY); draggedX = e.getX(); draggedY = e.getY(); drawXORLine(pressedX,pressedY,draggedX,draggedY); } } /** * Invoked when the mouse button has been moved in the Canvas canvas, * when there are no buttons pressed. */ public void mouseMoved ( MouseEvent e ) {} public void drawXORLine(int x0, int y0, int x1, int y1) { Graphics g = canvas.getGraphics(); if(g!=null) { g.setXORMode(Color.white); g.setColor(Color.black); g.drawLine(x0,y0,x1,y1); g.dispose(); g = null; } } /** Handles action events */ public void actionPerformed(ActionEvent e) {// System.out.println(""+e); Object src = e.getSource();// canvas.stop(); if(src == pauseButton) { canvas.pauseResumeAnimations(); } else if(src == refreshButton) { canvas.origView(); } else if(src == helpButton) { applet.showDocument("http://www.tinyline.com/svgt/download/tinyline4web.html"); } } /** Handles item events */ public void itemStateChanged(ItemEvent e) { CheckboxMenuItem src = (CheckboxMenuItem)e.getSource();// System.out.println("src" + src); if (current != null && current != src) { current.setState(false); } current = src; current.setState(true); if(src == panButton) { type = PAN_MOUSE; } else if(src == zoomInButton) { type = ZOOM_IN_MOUSE; } else if(src == zoomOutButton) { type = ZOOM_OUT_MOUSE; } else if(src == linkButton) { type = LINK_MOUSE; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -