📄 svgeditor.java
字号:
zoomFactor = zoom / 100f; trans.scale(zoomFactor, zoomFactor); setPreferredSize(new Dimension( (int) (image.getWidth(null) * zoomFactor), (int) (image.getHeight(null) * zoomFactor))); editRect.setRect(0, 0, getPreferredSize().width, getPreferredSize().height); paintBuffer(); revalidate(); } /** * Sets the tool mode, i.e. the current active tool. * * @param mode the new active tool mode */ public void setToolMode(int mode) { toolMode = mode; } /** * Returns the current tool mode. * * @return the current tool mode */ public int getToolMode() { return toolMode; } /** * Handles mouseClicked events. * * @param e the mouseClicked event */ public void mouseClicked(MouseEvent e) { } /** * Handles mousePressed events. * * @param me the mousePressed event */ public void mousePressed(MouseEvent me) { Point p = me.getPoint(); // inverse transform, i.e. adjust for the zoomfactor p.x = (int) (p.x / zoomFactor); p.y = (int) (p.y / zoomFactor); dragStart = p; dragging = true; switch (toolMode) { case SELECT_TOOL: if (bBox != null) { switch (toolSubMode) { case MOVE_TOOL: if (copyShape instanceof RectangularShape) { if (!bBox.contains(p)) { dragging = false; } } else if (copyShape instanceof Line2D) { if (!lineBox.contains(p)) { dragging = false; } } break; case MOVE_POINT_TOOL: if (copyShape instanceof Line2D) { Point2D p1 = ((Line2D) copyShape).getP1(); Point2D p2 = ((Line2D) copyShape).getP2(); for (int i = 0; i < corners.length; i++) { if (corners[i].contains(p) && corners[i].contains(p1)) { editPoint = new Point((int) p1.getX(), (int) p1.getY()); break; } else if (corners[i].contains(p) && corners[i].contains(p2)) { editPoint = new Point((int) p2.getX(), (int) p2.getY()); break; } } } break; default: resizeStartRect = (Rectangle2D) bBox.clone(); } } break; case RECT_TOOL: if (editRect.contains(me.getPoint())) { copyShape = new Rectangle(p.x, p.y, 1, 1); bBox = copyShape.getBounds2D(); SVGEditor.this.updateToolBarShapeButtons(); } break; case ELLIPSE_TOOL: if (editRect.contains(me.getPoint())) { copyShape = new Ellipse2D.Float(p.x, p.y, 1, 1); bBox = copyShape.getBounds2D(); SVGEditor.this.updateToolBarShapeButtons(); } break; case LINE_TOOL: if (editRect.contains(me.getPoint())) { copyShape = new Line2D.Float(p.x, p.y, p.x + 1, p.y + 1); bBox = copyShape.getBounds2D(); adjustActiveLineArea(); SVGEditor.this.updateToolBarShapeButtons(); } break; default: // do nothing } updateObjectStatus(); updateMouseStatus(p.x, p.y); } /** * Handles the mouseReleased event. * * @param me the mouseReleased event */ public void mouseReleased(MouseEvent me) { adjustAnchors(); adjustActiveLineArea(); dragging = false; setCursor(Cursor.getDefaultCursor()); repaint(); updateObjectStatus(); } /** * Stub. * * @param e the mouseEntered event */ public void mouseEntered(MouseEvent e) { } /** * Stub. * * @param e the mouseExited event */ public void mouseExited(MouseEvent e) { updateMouseStatus(0, 0); } /** * Handles the mouseDragged event. * * @param me the mouseDragged event */ public void mouseDragged(MouseEvent me) { boolean shiftDown = me.isShiftDown(); if (dragging) { Point p = me.getPoint(); // inverse transform, i.e. adjust for the zoomfactor p.x = (int) (p.x / zoomFactor); p.y = (int) (p.y / zoomFactor); if (editRect.contains(me.getPoint())) { // constrain mouse events to the image size switch (toolMode) { case SELECT_TOOL: if (bBox != null) { int xdif = p.x - dragStart.x; int ydif = p.y - dragStart.y; switch (toolSubMode) { case MOVE_TOOL: dragMoveShape(p, xdif, ydif, shiftDown); break; case RESIZE_NW_TOOL: dragResizeNW(xdif, ydif, shiftDown); break; case RESIZE_NE_TOOL: dragResizeNE(xdif, ydif, shiftDown); break; case RESIZE_SE_TOOL: dragResizeSE(xdif, ydif, shiftDown); break; case RESIZE_SW_TOOL: dragResizeSW(xdif, ydif, shiftDown); break; case MOVE_POINT_TOOL: dragLinePoint(p, shiftDown); break; } } break; case RECT_TOOL: // fall through case ELLIPSE_TOOL: dragCreateShape(p, shiftDown); break; case LINE_TOOL: dragCreateLine(p, shiftDown); } } updateMouseStatus(p.x, p.y); } updateObjectStatus(); } /** * Update the cursor. * * @param me the mouseMoved event */ public void mouseMoved(MouseEvent me) { if (!dragging) { Point p = me.getPoint(); p.x = (int) (p.x / zoomFactor); p.y = (int) (p.y / zoomFactor); updateMouseStatus(p.x, p.y); if (toolMode == SELECT_TOOL) { if (copyShape instanceof RectangularShape) { if (bBox != null) { if (corners[0].contains(p)) { setCursor(Cursor.getPredefinedCursor( Cursor.NW_RESIZE_CURSOR)); toolSubMode = RESIZE_NW_TOOL; return; } if (corners[1].contains(p)) { setCursor(Cursor.getPredefinedCursor( Cursor.NE_RESIZE_CURSOR)); toolSubMode = RESIZE_NE_TOOL; return; } if (corners[2].contains(p)) { setCursor(Cursor.getPredefinedCursor( Cursor.SE_RESIZE_CURSOR)); toolSubMode = RESIZE_SE_TOOL; return; } if (corners[3].contains(p)) { setCursor(Cursor.getPredefinedCursor( Cursor.SW_RESIZE_CURSOR)); toolSubMode = RESIZE_SW_TOOL; return; } if (bBox.contains(p)) { setCursor(Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR)); } else { setCursor(Cursor.getDefaultCursor()); } toolSubMode = MOVE_TOOL; return; } } else if (copyShape instanceof Line2D) { // first try the end points Point2D p1 = ((Line2D) copyShape).getP1(); Point2D p2 = ((Line2D) copyShape).getP2(); for (int i = 0; i < corners.length; i++) { if (corners[i].contains(p) && (corners[i].contains(p1) || corners[i].contains(p2))) { setCursor(Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR)); toolSubMode = MOVE_POINT_TOOL; return; } } // next poll the active line area if (lineBox.contains(p)) { setCursor(Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR)); toolSubMode = MOVE_TOOL; return; } } setCursor(Cursor.getDefaultCursor()); toolSubMode = MOVE_TOOL; } } } /** * Create a scaled stroke for the current zoom level * * @return DOCUMENT ME! */ private BasicStroke createScaledStroke() { if (zoomFactor == 1.0f) { return selectionStroke; } else { return new BasicStroke(1.0f / zoomFactor, selectionStroke.getEndCap(), selectionStroke.getLineJoin(), selectionStroke.getMiterLimit(), selectionStroke.getDashArray(), 1.0f); } } /** * Update the coordinates of the rectangles that mark the bounding * box's corner */ private void adjustAnchors() { if (bBox != null) { corners[0].setRect(bBox.getMinX() - (MARKER_SIZE / 2), bBox.getMinY() - (MARKER_SIZE / 2), MARKER_SIZE, MARKER_SIZE); corners[1].setRect(bBox.getMaxX() - (MARKER_SIZE / 2), bBox.getMinY() - (MARKER_SIZE / 2), MARKER_SIZE, MARKER_SIZE); corners[2].setRect(bBox.getMaxX() - (MARKER_SIZE / 2), bBox.getMaxY() - (MARKER_SIZE / 2), MARKER_SIZE, MARKER_SIZE); corners[3].setRect(bBox.getMinX() - (MARKER_SIZE / 2), bBox.getMaxY() - (MARKER_SIZE / 2), MARKER_SIZE, MARKER_SIZE); } } /** * Update the coordinates of the polygon that marks the line's active * area */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -