1 18 package org.apache.batik.swing.gvt; 19 20 import java.awt.BasicStroke ; 21 import java.awt.Color ; 22 import java.awt.Dimension ; 23 import java.awt.Graphics ; 24 import java.awt.Graphics2D ; 25 import java.awt.event.MouseEvent ; 26 import java.awt.geom.AffineTransform ; 27 import java.awt.geom.Line2D ; 28 29 37 public class AbstractZoomInteractor extends InteractorAdapter { 38 39 42 protected boolean finished = true; 43 44 47 protected int xStart; 48 49 52 protected int yStart; 53 54 57 protected int xCurrent; 58 59 62 protected int yCurrent; 63 64 67 protected Line2D markerTop; 68 69 72 protected Line2D markerLeft; 73 74 77 protected Line2D markerBottom; 78 79 82 protected Line2D markerRight; 83 84 87 protected Overlay overlay = new ZoomOverlay(); 88 89 92 protected BasicStroke markerStroke = new BasicStroke (1, 93 BasicStroke.CAP_SQUARE, 94 BasicStroke.JOIN_MITER, 95 10, 96 new float[] { 4, 4 }, 0); 97 98 101 public boolean endInteraction() { 102 return finished; 103 } 104 105 107 110 public void mousePressed(MouseEvent e) { 111 if (!finished) { 112 mouseExited(e); 113 return; 114 } 115 116 finished = false; 117 markerTop = null; 118 markerLeft = null; 119 markerBottom = null; 120 markerRight = null; 121 122 xStart = e.getX(); 123 yStart = e.getY(); 124 JGVTComponent c = (JGVTComponent)e.getSource(); 125 c.getOverlays().add(overlay); 126 } 127 128 131 public void mouseReleased(MouseEvent e) { 132 finished = true; 133 JGVTComponent c = (JGVTComponent)e.getSource(); 134 c.getOverlays().remove(overlay); 135 overlay.paint(c.getGraphics()); 136 137 xCurrent = e.getX(); 138 yCurrent = e.getY(); 139 140 if ((xCurrent - xStart) != 0 && 141 (yCurrent - yStart) != 0) { 142 143 int dx = xCurrent - xStart; 144 int dy = yCurrent - yStart; 145 146 if (dx < 0) { 147 dx = -dx; 148 xStart = xCurrent; 149 } 150 if (dy < 0) { 151 dy = -dy; 152 yStart = yCurrent; 153 } 154 155 Dimension size = c.getSize(); 156 157 float scaleX = size.width / (float)dx; 159 float scaleY = size.height / (float)dy; 160 float scale = (scaleX < scaleY) ? scaleX : scaleY; 161 162 AffineTransform at = new AffineTransform (); 164 at.scale(scale, scale); 165 at.translate(-xStart, -yStart); 166 167 at.concatenate(c.getRenderingTransform()); 168 c.setRenderingTransform(at); 169 } 170 } 171 172 175 public void mouseExited(MouseEvent e) { 176 finished = true; 177 JGVTComponent c = (JGVTComponent)e.getSource(); 178 c.getOverlays().remove(overlay); 179 overlay.paint(c.getGraphics()); 180 } 181 182 184 191 public void mouseDragged(MouseEvent e) { 192 JGVTComponent c = (JGVTComponent)e.getSource(); 193 194 overlay.paint(c.getGraphics()); 195 196 xCurrent = e.getX(); 197 yCurrent = e.getY(); 198 199 float xMin, yMin, width, height; 201 if (xStart < xCurrent) { 202 xMin = xStart; 203 width = xCurrent - xStart; 204 } else { 205 xMin = xCurrent; 206 width = xStart - xCurrent; 207 } 208 if (yStart < yCurrent) { 209 yMin = yStart; 210 height = yCurrent - yStart; 211 } else { 212 yMin = yCurrent; 213 height = yStart - yCurrent; 214 } 215 Dimension d = c.getSize(); 216 float compAR = d.width/(float)d.height; 217 if (compAR > width/height) { 218 width = compAR*height; 219 } else { 220 height = width/compAR; 221 } 222 223 markerTop = new Line2D.Float (xMin, yMin, xMin+width, yMin); 224 markerLeft = new Line2D.Float (xMin, yMin, xMin, yMin+height); 225 markerBottom = new Line2D.Float (xMin, yMin+height, 226 xMin+width, yMin+height); 227 markerRight = new Line2D.Float (xMin+width, yMin, 228 xMin+width, yMin+height); 229 230 overlay.paint(c.getGraphics()); 231 } 232 233 236 protected class ZoomOverlay implements Overlay { 237 238 241 public void paint(Graphics g) { 242 if (markerTop != null) { 243 Graphics2D g2d = (Graphics2D )g; 244 245 g2d.setXORMode(Color.white); 246 g2d.setColor(Color.black); 247 g2d.setStroke(markerStroke); 248 249 g2d.draw(markerTop); 250 g2d.draw(markerLeft); 251 g2d.draw(markerBottom); 252 g2d.draw(markerRight); 253 } 254 } 255 } 256 } 257 | Popular Tags |