| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.drawing; |
| 16 | |
| 17 | import java.awt.BasicStroke; |
| 18 | import java.awt.Color; |
| 19 | import java.awt.Graphics2D; |
| 20 | import java.awt.Point; |
| 21 | |
| 22 | import javax.swing.JComponent; |
| 23 | |
| 24 | public abstract class AbstractRubberBandTool extends AbstractMouseDragTool |
| 25 | { |
| 26 | public void start(JComponent area) |
| 27 | { |
| 28 | _area = area; |
| 29 | _penColor = (Color) _properties.get(ColorPropertyTool.TOOL_PROPERTY_COLOR); |
| 30 | _stroke = (BasicStroke) _properties.get(LinePropertyTool.TOOL_PROPERTY_LINE); |
| 31 | _started = false; |
| 32 | } |
| 33 | |
| 34 | public void mouseMotion(Point location) |
| 35 | { |
| 36 | if (!_started) |
| 37 | { |
| 38 | _started = true; |
| 39 | _prevX = location.x; |
| 40 | _prevY = location.y; |
| 41 | _startX = location.x; |
| 42 | _startY = location.y; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | _lastX = location.x; |
| 47 | _lastY = location.y; |
| 48 | feedback(); |
| 49 | _prevX = _lastX; |
| 50 | _prevY = _lastY; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // This method provides immediate feedback to writer (not to other participants) |
| 55 | private void feedback() |
| 56 | { |
| 57 | Graphics2D graf = (Graphics2D) _area.getGraphics(); |
| 58 | graf.setColor(_penColor); |
| 59 | graf.setXORMode(Color.WHITE); |
| 60 | graf.setStroke(_stroke); |
| 61 | // Remove previous "rubber band" |
| 62 | draw(graf, _startX, _startY, _prevX, _prevY); |
| 63 | scroll(_area, new Point(_lastX, _lastY)); |
| 64 | // Display new rubber band |
| 65 | draw(graf, _startX, _startY, _lastX, _lastY); |
| 66 | graf.dispose(); |
| 67 | } |
| 68 | |
| 69 | abstract protected void draw(Graphics2D graf, int x1, int y1, int x2, int y2); |
| 70 | |
| 71 | protected Color _penColor; |
| 72 | protected BasicStroke _stroke; |
| 73 | protected JComponent _area; |
| 74 | protected int _startX; |
| 75 | protected int _startY; |
| 76 | protected int _lastX; |
| 77 | protected int _lastY; |
| 78 | protected int _prevX; |
| 79 | protected int _prevY; |
| 80 | private boolean _started; |
| 81 | } |