⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svgshape.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			//the bounds of the node			Rectangle2D bounds=frame.getNodeGeometryBounds((Element)node);						if(bounds!=null){			    			    //correcting the resize action values			    if(diff.x==bounds.getWidth()){			        			        diff.x--;			        			    }else if(diff.x==-bounds.getWidth()){			        			        diff.x++;			    }			    			    if(diff.y==bounds.getHeight()){			        			        diff.y--;			        			    }else if(diff.y==-bounds.getHeight()){			        			        diff.y++;			    }								double sx=1.0, sy=1.0, tx=0, ty=0;								if(square.getType().equals("NW")){										sx=1-diff.x/bounds.getWidth();					sy=1-diff.y/bounds.getHeight();										tx=(bounds.getX()+bounds.getWidth())*(1-sx);					ty=(bounds.getY()+bounds.getHeight())*(1-sy);								}else if(square.getType().equals("N")){									sy=1-diff.y/bounds.getHeight();					ty=(bounds.getY()+bounds.getHeight())*(1-sy);								}else if(square.getType().equals("NE")){										sx=1+diff.x/bounds.getWidth();					sy=1-diff.y/bounds.getHeight();										tx=(bounds.getX())*(1-sx);					ty=(bounds.getY()+bounds.getHeight())*(1-sy);											}else if(square.getType().equals("E")){																			sx=1+diff.x/bounds.getWidth();											tx=bounds.getX()*(1-sx);																			}else if(square.getType().equals("SE")){										sx=1+diff.x/bounds.getWidth();					sy=1+diff.y/bounds.getHeight();										tx=bounds.getX()*(1-sx);					ty=bounds.getY()*(1-sy);									}else if(square.getType().equals("S")){										sy=1+diff.y/bounds.getHeight();										ty=bounds.getY()*(1-sy);									}else if(square.getType().equals("SW")){										sx=1-diff.x/bounds.getWidth();					sy=1+diff.y/bounds.getHeight();										tx=(bounds.getX()+bounds.getWidth())*(1-sx);					ty=(bounds.getY())*(1-sy);												}else if(square.getType().equals("W")){										sx=1-diff.x/bounds.getWidth();					tx=(bounds.getX()+bounds.getWidth())*(1-sx);					}								final AffineTransform af=AffineTransform.getScaleInstance(sx, sy);				af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty));									if(! af.isIdentity()){										//gets, modifies and sets the matrix					SVGTransformMatrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node);					matrix.concatenateTransform(af);										if(matrix.isMatrixCorrect()){					    						getSVGEditor().getSVGToolkit().setTransformMatrix(node, matrix);												//create the undo/redo action and insert it into the undo/redo stack						if(editor.getUndoRedo()!=null){							SVGUndoRedoAction action=new SVGUndoRedoAction(labels.get("undoredoresize")){								@Override								public void undo(){								    									//gets, modifies and sets the matrix								    SVGTransformMatrix fmatrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node);									try{fmatrix.concatenateTransform(af.createInverse());}catch (Exception ex){}																		if(fmatrix.isMatrixCorrect()){									    										getSVGEditor().getSVGToolkit().setTransformMatrix(node, fmatrix);									}																		//notifies that the selection has changed									if(getSVGEditor().getSVGSelection()!=null){																				getSVGEditor().getSVGSelection().selectionChanged(true);									}								}								@Override								public void redo(){								    									//gets, modifies and sets the matrix								    SVGTransformMatrix fmatrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node);									fmatrix.concatenateTransform(af);																		if(fmatrix.isMatrixCorrect()){									    										getSVGEditor().getSVGToolkit().setTransformMatrix(node, fmatrix);  									}																		//notifies that the selection has changed									if(getSVGEditor().getSVGSelection()!=null){																				getSVGEditor().getSVGSelection().selectionChanged(true);									}								}							};														//gets or creates the undo/redo list and adds the action into it							SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(labels.get("undoredoresize"));							actionlist.add(action);							editor.getUndoRedo().addActionList(frame, actionlist);								actionlist=null;						}					}				}					}		}	}		/**	 * the method to rotate a node	 * @param frame the current SVGFrame	 * @param square the current selection square	 * @param point1 the first point clicked	 * @param point2 the second point clicked	 */	public void rotateSkewNode(SVGFrame frame, SVGSelectionSquare square, Point2D.Double point1, Point2D.Double point2){				if(frame!=null && square!=null && square.getNode()!=null && point1!=null && point2!=null){						//gets the paintlistener associated with the frame			CanvasPaintListener paintListener=null;						try{				paintListener=rotateFrameTable.get(frame);			}catch (Exception ex){paintListener=null;}				Node node=square.getNode();			//the bounds of the node			Rectangle2D bounds=frame.getNodeGeometryBounds((Element)node);						//the transform of the action			AffineTransform af=new AffineTransform();			//computes the scale and translate values taking the type of the selection square into account			if(bounds!=null && point1!=null && point2!=null){								double angle=0, cx=0, cy=0;				Point2D.Double centerpoint=null;								if(square.getType().equals("C")){										centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth()/2, bounds.getY()+bounds.getHeight()/2);					double x2=0, y2=0, n2=0;										n2=Math.sqrt(Math.pow(point2.x-centerpoint.x,2)+Math.pow(point2.y-centerpoint.y,2));										x2=(point2.x-centerpoint.x)/n2;					y2=(point2.y-centerpoint.y)/n2;										if(y2>=0){						angle=Math.acos(x2);											}else{					    						angle=-Math.acos(x2);					}					cx=centerpoint.x; 					cy=centerpoint.y;					//sets the new rotation values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getRotateInstance(angle));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));				}else if(square.getType().equals("N")){				    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getHeight()/2,bounds.getY());					angle=Math.toRadians(point2.x-point1.x);					cx=centerpoint.x; 					cy=centerpoint.y;					//sets the new skew values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(Math.tan(angle), 0));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));												}else if(square.getType().equals("S")){				    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth()/2,bounds.getY()+bounds.getHeight());					angle=Math.toRadians(point2.x-point1.x);					cx=centerpoint.x; 					cy=centerpoint.y;										//sets the new skew values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(Math.tan(angle), 0));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));											}else if(square.getType().equals("E")){				    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth(), bounds.getY()+bounds.getHeight()/2);					angle=Math.toRadians(point2.y-point1.y);					cx=centerpoint.x; 					cy=centerpoint.y;						//sets the new skew values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(0, Math.tan(angle)));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));						}else if(square.getType().equals("W")){				    					centerpoint=new Point2D.Double(bounds.getX() ,bounds.getY()+bounds.getHeight()/2);					angle=Math.toRadians(point2.y-point1.y);					cx=centerpoint.x; 					cy=centerpoint.y;						//sets the new skew values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(0, Math.tan(angle)));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));				}				final Shape foutline=frame.getTransformedOutline((Element)node, af);				//removes the paint listener				if(paintListener!=null){				    					frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false);				}								//creates and sets the paint listener				paintListener=new CanvasPaintListener(){				    					public void paintToBeDone(Graphics g) {						Graphics2D g2=(Graphics2D)g;												fillShape(g2, foutline);					}				};			   frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.DRAW_LAYER, paintListener, true);			   rotateFrameTable.put(frame, paintListener);			}		}	}		/**	 * validates the rotateNode method	 * @param frame the current SVGFrame	 * @param square the current selection square	 * @param point1 the first point clicked	 * @param point2 the second point clicked	 */	public void validateRotateSkewNode(SVGFrame frame, SVGSelectionSquare square, Point2D.Double point1, Point2D.Double point2){		if(frame!=null && square!=null && square.getNode()!=null){					//gets the paintlistener associated with the frame			CanvasPaintListener paintListener=null;			SVGFrame f=null;						for(Iterator it=new LinkedList<SVGFrame>(rotateFrameTable.keySet()).iterator(); it.hasNext();){			    				try{				    f=(SVGFrame)it.next();					paintListener=rotateFrameTable.get(f);				}catch (Exception ex){paintListener=null;}								if(paintListener!=null){				    					rotateFrameTable.remove(frame);					frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false);				}			}			final Node node=square.getNode();						//the bounds of the node			Rectangle2D bounds=frame.getNodeGeometryBounds((Element)node);			final AffineTransform af=new AffineTransform();						if(point1!=null && point2!=null && bounds!=null){								//the values used for computing the rotate or skew values				double angle=0, cx=0, cy=0;				Point2D.Double centerpoint=null;								if(square.getType().equals("C")){					    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth()/2, bounds.getY()+bounds.getHeight()/2);					double x2=0, y2=0, n2=0;										n2=Math.sqrt(Math.pow(point2.x-centerpoint.x, 2)+Math.pow(point2.y-centerpoint.y, 2));										x2=(point2.x-centerpoint.x)/n2;					y2=(point2.y-centerpoint.y)/n2;										if(y2>=0){					    						angle=Math.acos(x2);											}else{					    						angle=-Math.acos(x2);					}					cx=centerpoint.x; 					cy=centerpoint.y;								//sets the new rotation values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getRotateInstance(angle));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));									}else if(square.getType().equals("N")){				    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth()/2,bounds.getY());					angle=Math.toRadians(point2.x-point1.x);					cx=centerpoint.x; 					cy=centerpoint.y;						//sets the new rotation values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(Math.tan(angle), 0));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));									}else if(square.getType().equals("S")){				    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth()/2,bounds.getY()+bounds.getHeight());					angle=Math.toRadians(point2.x-point1.x);					cx=centerpoint.x; 					cy=centerpoint.y;						//sets the new rotation values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(Math.tan(angle), 0));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));														}else if(square.getType().equals("E")){				    					centerpoint=new Point2D.Double(bounds.getX()+bounds.getWidth(), bounds.getY()+bounds.getHeight()/2);					angle=Math.toRadians(point2.y-point1.y);					cx=centerpoint.x; 					cy=centerpoint.y;						//sets the new rotation values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(0, Math.tan(angle)));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));				}else if(square.getType().equals("W")){				    					centerpoint=new Point2D.Double(bounds.getX(), bounds.getY()+bounds.getHeight()/2);					angle=Math.toRadians(point2.y-point1.y);					cx=centerpoint.x; 					cy=centerpoint.y;						//sets the new rotation values					af.preConcatenate(AffineTransform.getTranslateInstance(-cx, -cy));					af.preConcatenate(AffineTransform.getShearInstance(0, Math.tan(angle)));					af.preConcatenate(AffineTransform.getTranslateInstance(cx, cy));													}				if(! af.isIdentity()){				    					//gets, modifies and sets the matrix				    SVGTransformMatrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node);					matrix.concatenateTransform(af);					if(matrix.isMatrixCorrect()){					    						getSVGEditor().getSVGToolkit().setTransformMatrix(node, matrix);												//creates the undo/redo action and insert it into the undo/redo stack						if(editor.getUndoRedo()!=null){						    							SVGUndoRedoAction action=new SVGUndoRedoAction(labels.get("undoredorotate")){								@Override								public void undo(){								    									//gets, modifies and sets the matrix								    SVGTransformMatrix fmatrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node);									try{fmatrix.concatenateTransform(af.createInverse());}catch (Exception ex){}																		if(fmatrix.isMatrixCorrect()){									    										getSVGEditor().getSVGToolkit().setTransformMatrix(node, fmatrix);									}																		//notifies that the selection has changed									if(getSVGEditor().getSVGSelection()!=null){																				getSVGEditor().getSVGSelection().selectionChanged(true);									}								}								@Override								public void redo(){								    									//gets, modifies and sets the matrix								    SVGTransformMatrix fmatrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node);									fmatrix.concatenateTransform(af);																		if(fmatrix.isMatrixCorrect()){									    										getSVGEditor().getSVGToolkit().setTransformMatrix(node, fmatrix);									}																		//notifies that the selection has changed									if(getSVGEditor().getSVGSelection()!=null){																				getSVGEditor().getSVGSelection().selectionChanged(true);									}								}							};														//gets or creates the undo/redo list and adds the action into it							SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(labels.get("undoredorotate"));							actionlist.add(action);										editor.getUndoRedo().addActionList(frame, actionlist);								actionlist=null;							}					}				}					}		}	}	/**	 * the method to modify a point of a node	 * @param frame the current SVGFrame	 * @param square the selection square	 * @param point1 the first point clicked	 * @param point2 the second point clicked	 */	public void modifyPoint(SVGFrame frame, SVGSelectionSquare square, Point2D.Double point1, Point2D.Double point2){			}		/**	 * validates the modifyPoint method	 * @param frame the current SVGFrame	 * @param square the selection square	 * @param point1 the first point clicked	 * @param point2 the second point clicked	 */	public void validateModifyPoint(SVGFrame frame, SVGSelectionSquare square, Point2D.Double point1, Point2D.Double point2){	}		/**	 * gets the name associated with the module	 * @return the module's name	 */	public String getName(){		return ids.get("id");	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -