private void updateMouse(Canvas canvas, int mx, int my, int mods) { if (active) { boolean shift = (mods & MouseEvent.SHIFT_DOWN_MASK) != 0; Location newEnd; if (shift) { newEnd = LineUtil.snapTo8Cardinals(mouseStart, mx, my); } else { newEnd = Location.create(mx, my); } if ((mods & InputEvent.CTRL_DOWN_MASK) != 0) { int x = newEnd.getX(); int y = newEnd.getY(); x = canvas.snapX(x); y = canvas.snapY(y); newEnd = Location.create(x, y); } if (!newEnd.equals(mouseEnd)) { mouseEnd = newEnd; repaintArea(canvas); } } lastMouseX = mx; lastMouseY = my; }
@Override public void mousePressed(Canvas canvas, MouseEvent e) { int x = e.getX(); int y = e.getY(); int mods = e.getModifiersEx(); if ((mods & InputEvent.CTRL_DOWN_MASK) != 0) { x = canvas.snapX(x); y = canvas.snapY(y); } Location loc = Location.create(x, y); mouseStart = loc; mouseEnd = loc; lastMouseX = loc.getX(); lastMouseY = loc.getY(); active = canvas.getModel() != null; repaintArea(canvas); }
@Override protected void paintForeground(Graphics g) { double zoom = grid.getZoomFactor(); Graphics gScaled = g.create(); if (zoom != 1.0 && zoom != 0.0 && gScaled instanceof Graphics2D) { ((Graphics2D) gScaled).scale(zoom, zoom); } super.paintForeground(gScaled); gScaled.dispose(); }
@Override public void repaintCanvasCoords(int x, int y, int width, int height) { double zoom = grid.getZoomFactor(); if (zoom != 1.0) { x = (int) (x * zoom - 1); y = (int) (y * zoom - 1); width = (int) (width * zoom + 4); height = (int) (height * zoom + 4); } super.repaintCanvasCoords(x, y, width, height); }
@Override public void setModel(CanvasModel value, ActionDispatcher dispatcher) { CanvasModel oldModel = super.getModel(); if (oldModel != null) { oldModel.removeCanvasModelListener(listener); } super.setModel(value, dispatcher); if (value != null) { value.addCanvasModelListener(listener); } }
@Override public void mouseReleased(Canvas canvas, MouseEvent e) { if (active) { updateMouse(canvas, e.getX(), e.getY(), e.getModifiersEx()); Location start = mouseStart; Location end = mouseEnd; CanvasObject add = null; if (!start.equals(end)) { active = false; CanvasModel model = canvas.getModel(); Location[] ends = {start, end}; List<Location> locs = UnmodifiableList.create(ends); add = attrs.applyTo(new Poly(false, locs)); add.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_STROKE); canvas.doAction(new ModelAddAction(model, add)); repaintArea(canvas); } canvas.toolGestureComplete(this, add); } }
@Override public void setTool(CanvasTool value) { hidePopup(); super.setTool(value); }
@Override protected void processMouseMotionEvent(MouseEvent e) { repairEvent(e, grid.getZoomFactor()); super.processMouseMotionEvent(e); }
@Override protected void paintBackground(Graphics g) { super.paintBackground(g); grid.paintGrid(g); }
private void repaintArea(Canvas canvas) { canvas.repaint(); }