📄 mousehandler.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 tinyapp;import java.awt.event.*;import java.awt.Graphics;import java.awt.Color;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 Static SVG Viewer. * <p> * @author (C) Andrew Girow * @version 1.10 * <p> */public class MouseHandler implements MouseListener, MouseMotionListener{ /** * The Canvas that this mouse handler uses */ ViewerCanvas canvas; /** * 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 MouseHandler 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 MouseHandler(ViewerCanvas canvas) { this.canvas = canvas; } /** * Invoked when the mouse has been clicked in the Canvas canvas. */ public void mouseClicked(MouseEvent e) { } /** * Invoked when the mouse has been pressed in the canvas. */ public void mousePressed(MouseEvent e) { 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(type == ZOOM_OUT_MOUSE) zoom(1); else if(type == ZOOM_IN_MOUSE) zoom(0); else if(type == LINK_MOUSE) { // No links! Its a Static SVG Viewer !!! } else if(type == PAN_MOUSE) { pan(pressedX -e.getX(),pressedY -e.getY()); } } /** * 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) { 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; } } /** The default zoom levels */ private static int MAX_ZOOMLEVEL = 5; private static int MIN_ZOOMLEVEL = -5; private int zoomLevel = 0; /** * Zooms in and out the current SVGT document. * @return true if the zoom is allowed; otherwise return false. */ public boolean zoom(int direction) { // zoom in '0' size / 2 if(direction == 0) { zoomLevel--; if(zoomLevel < MIN_ZOOMLEVEL) { zoomLevel = MIN_ZOOMLEVEL; return false; } } else //zoom out size * 2 { zoomLevel++; if(zoomLevel > MAX_ZOOMLEVEL) { zoomLevel = MAX_ZOOMLEVEL; return false; } } SVGRect newView = new SVGRect(); SVGRect view = canvas.raster.view; int midX = view.x + view.width/2; int midY = view.y + view.height/2; // zoom in '0' size / 2 if(direction == 0) { newView.width = (view.width/2); newView.height = (view.height/2); } else //zoom out size * 2 { newView.width = (view.width * 2 ); newView.height = (view.height * 2); } newView.x = midX - (newView.width) / 2; newView.y = midY - (newView.height) / 2; view.x = newView.x; view.y = newView.y; view.width = newView.width; view.height = newView.height; canvas.raster.setCamera(); canvas.raster.update(); canvas.raster.sendPixels(); return true; } /** * Returns the current SVGT document to its original view. */ public void origView() { SVGRaster raster = canvas.raster; zoomLevel = 0; raster.view = new SVGRect(raster.origview); canvas.raster.setCamera(); canvas.raster.update(); canvas.raster.sendPixels(); } /** * Pans the current SVGT document. * @param x The distance on X coordinate. * @param y The distance on Y coordinate. */ public void pan(int x , int y) { SVGRaster raster = canvas.raster; SVGRect view = raster.view; SVGDocument doc = raster.getSVGDocument(); SVGNode root = doc.root; int scale = ((SVGSVGElem)root).getCurrentScale(); view.x += TinyUtil.div(x<<TinyUtil.FIX_BITS,scale); view.y += TinyUtil.div(y<<TinyUtil.FIX_BITS,scale); canvas.raster.setCamera(); canvas.raster.update(); canvas.raster.sendPixels(); } /** * Switch anitliasing on and off. */ public void quality() { SVGRaster raster = canvas.raster; boolean flag = raster.isAntialiased(); raster.setAntialiased(!flag); canvas.raster.setCamera(); canvas.raster.update(); canvas.raster.sendPixels(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -