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

📄 svgpath.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			final Element elt=(Element)node;					    		    //getting the path that will be analysed		    ExtendedGeneralPath path=getSVGEditor().getSVGToolkit().getGeneralPath(elt);			//the cursor associated with the selection points			Cursor cursor=new Cursor(Cursor.HAND_CURSOR);			Shape shape=null;			int type=-1;			double[] values=new double[7];			int index=0, sqx=0, sqy=0;			Point2D.Double scPoint=null, scCtrlPoint=null, scCtrlPoint2=null, lastScPoint=new Point2D.Double(0, 0);			GradientPaint gradient=null;			//draws the selection			for(ExtendedPathIterator pit=path.getExtendedPathIterator(); ! pit.isDone(); pit.next()){				type=pit.currentSegment(values);								if(type==ExtendedPathIterator.SEG_CLOSE){									}else if(type==ExtendedPathIterator.SEG_CUBICTO){				    					scCtrlPoint=frame.getScaledPoint(new Point2D.Double(values[0], values[1]), false);					scCtrlPoint2=frame.getScaledPoint(new Point2D.Double(values[2], values[3]), false);					scPoint=frame.getScaledPoint(new Point2D.Double(values[4], values[5]), false);					g.setColor(LINE_SELECTION_COLOR);					g.drawLine((int)lastScPoint.x, (int)lastScPoint.y, (int)scCtrlPoint.x, (int)scCtrlPoint.y);									sqx=(int)(scCtrlPoint.x-sqd);					sqy=(int)(scCtrlPoint.y-sqd);									if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node,"Cctrl"+new Integer(index++).toString(),new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd),cursor));					}											shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "Ctrl", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}										sqx=(int)(scCtrlPoint2.x-sqd);					sqy=(int)(scCtrlPoint2.y-sqd);										if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node,"Cctrl"+new Integer(index++).toString(),new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd),cursor));					}					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "Ctrl", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}										g.setColor(LINE_SELECTION_COLOR);					g.drawLine((int)scCtrlPoint2.x, (int)scCtrlPoint2.y, (int)scPoint.x, (int)scPoint.y);										sqx=(int)(scPoint.x-sqd);					sqy=(int)(scPoint.y-sqd);					if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node,"C"+new Integer(index++).toString(),new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd),cursor));					}					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "P", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}										lastScPoint=scPoint;				}else if(type==ExtendedPathIterator.SEG_LINETO){										scPoint=frame.getScaledPoint(new Point2D.Double(values[0], values[1]), false);					lastScPoint=scPoint;										sqx=(int)(scPoint.x-sqd);					sqy=(int)(scPoint.y-sqd);					if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node, "L"+new Integer(index++).toString(), 					            				new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd), cursor));					}					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "P", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}									}else if(type==ExtendedPathIterator.SEG_ARCTO){										scPoint=frame.getScaledPoint(new Point2D.Double(values[5], values[6]), false);					lastScPoint=scPoint;										sqx=(int)(scPoint.x-sqd);					sqy=(int)(scPoint.y-sqd);					if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node, "A"+new Integer(index++).toString(), 					            				new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd), cursor));					}					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "P", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}									}else if(type==ExtendedPathIterator.SEG_MOVETO){					scPoint=frame.getScaledPoint(new Point2D.Double(values[0], values[1]), false);					lastScPoint=scPoint;										sqx=(int)(scPoint.x-sqd);					sqy=(int)(scPoint.y-sqd);					if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node, "M"+new Integer(index++).toString(), 					            				new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd), cursor));					}					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "P", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}				}else if(type==ExtendedPathIterator.SEG_QUADTO){					scCtrlPoint=frame.getScaledPoint(new Point2D.Double(values[0], values[1]), false);					scPoint=frame.getScaledPoint(new Point2D.Double(values[2], values[3]), false);					g.setColor(LINE_SELECTION_COLOR);					g.drawLine((int)lastScPoint.x, (int)lastScPoint.y, (int)scCtrlPoint.x, (int)scCtrlPoint.y);										sqx=(int)(scCtrlPoint.x-sqd);					sqy=(int)(scCtrlPoint.y-sqd);										if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node,"Qctrl"+new Integer(index++).toString(),new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd),cursor));					}					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "Ctrl", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}										sqx=(int)(scPoint.x-sqd);					sqy=(int)(scPoint.y-sqd);					if(getSVGEditor().getSVGSelection()!=null){					    					    squarelist.add(new SVGSelectionSquare(node,"Q"+new Integer(index++).toString(),new Rectangle2D.Double(sqx, sqy, 2*sqd, 2*sqd),cursor));                    }					shape=getArrow(new Point2D.Double(sqx+sqd, sqy+sqd), "P", REGULAR_SELECTION);					if(shape!=null){					    						gradient=new GradientPaint(sqx, sqy, SQUARE_SELECTION_COLOR1, sqx+2*sqd, sqy+2*sqd, SQUARE_SELECTION_COLOR2, true);						g.setPaint(gradient);						g.fill(shape);						g.setColor(LINE_SELECTION_COLOR);						g.draw(shape);					}										lastScPoint=scPoint;				}			}		}			return squarelist;	}		/**	 * 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){				if(frame!=null && square!=null && square.getNode()!=null && point2!=null){			//gets the paintlistener associated with the frame			CanvasPaintListener paintListener=null;						try{				paintListener=(CanvasPaintListener)modifyPointFrameTable.get(frame);			}catch (Exception ex){paintListener=null;}			final Element elt=(Element)square.getNode();			    		    //the new path		    ExtendedGeneralPath newPath=new ExtendedGeneralPath(), tmpPath=null;					    		    //getting the path that will be analysed		    tmpPath=getSVGEditor().getSVGToolkit().getGeneralPath(elt);			int type=-1;			double[] values=new double[7];						int i=0, index=-1;						//gets the index of the point that has to be modified 			try{				//if the point is a control point				if(square.getType().indexOf("ctrl")==-1){				    					index=new Integer(square.getType().substring(1,square.getType().length())).intValue();									}else{				    					index=new Integer(square.getType().substring(square.getType().indexOf("ctrl")+4, square.getType().length())).intValue();				}			}catch(Exception ex){}			//for each command in the path, the command and its values are added to the string value			for(ExtendedPathIterator pit=tmpPath.getExtendedPathIterator(); ! pit.isDone(); pit.next()){				type=pit.currentSegment(values);								if(type==ExtendedPathIterator.SEG_CLOSE){				    				    newPath.closePath();									}else if(type==ExtendedPathIterator.SEG_CUBICTO){					if(index==i){					    					    newPath.curveTo((float)point2.x, (float)point2.y, (float)values[2], (float)values[3], (float)values[4], (float)values[5]);					    					}else if(index==i+1){					    					    newPath.curveTo((float)values[0], (float)values[1], (float)point2.x, (float)point2.y, (float)values[4], (float)values[5]);					    					}else if(index==i+2){					    					    newPath.curveTo((float)values[0], (float)values[1], (float)values[2], (float)values[3], (float)point2.x, (float)point2.y);										}else{					    					    newPath.curveTo((float)values[0], (float)values[1], (float)values[2], (float)values[3], (float)values[4], (float)values[5]);					}										i+=3;				}else if(type==ExtendedPathIterator.SEG_LINETO){					if(index==i){					    					    newPath.lineTo((float)point2.x, (float)point2.y);					    					}else{					    					    newPath.lineTo((float)values[0], (float)values[1]);					}										i++;									}else if(type==ExtendedPathIterator.SEG_ARCTO){					if(index==i){					    					    newPath.arcTo((float)values[0], (float)values[1], (float)values[2], values[3]==0?false:true, values[4]==0?false:true, (float)point2.x, (float)point2.y);					    					}else{					    					    newPath.arcTo((float)values[0], (float)values[1], (float)values[2], values[3]==0?false:true, values[4]==0?false:true, (float)values[5], (float)values[6]);					}										i++;									}else if(type==ExtendedPathIterator.SEG_MOVETO){					if(index==i){					    					    newPath.moveTo((float)point2.x, (float)point2.y);					    					}else{					    					    newPath.moveTo((float)values[0], (float)values[1]);					}										i++;									}else if(type==ExtendedPathIterator.SEG_QUADTO){					if(index==i){					    					    newPath.quadTo((float)point2.x, (float)point2.y, (float)values[2], (float)values[3]);					    					}else if(index==i+1){					    					    newPath.quadTo((float)values[0], (float)values[1], (float)point2.x, (float)point2.y);					}else{					    					    newPath.quadTo((float)values[0], (float)values[1], (float)values[2], (float)values[3]);					}										i+=2;				}			}						//computes the scale and translate values			if(newPath!=null){			    				//concatenates the transforms to draw the outline				//concatenates the transforms to draw the outline				AffineTransform af=new AffineTransform();								try{af.preConcatenate(frame.getScrollPane().getSVGCanvas().getViewingTransform());}catch (Exception ex){}				try{af.preConcatenate(frame.getScrollPane().getSVGCanvas().getRenderingTransform());}catch (Exception ex){}				//computing the outline				Shape outline=af.createTransformedShape(newPath);				final Shape foutline=outline;								//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);			   modifyPointFrameTable.put(frame, paintListener);			}		}	}		/**	 * 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){				if(frame!=null && square!=null && square.getNode()!=null && point2!=null){			//gets the paintlistener associated with the frame			CanvasPaintListener paintListener=null;			SVGFrame f=null;						for(Iterator it=new LinkedList(modifyPointFrameTable.keySet()).iterator(); it.hasNext();){			    				try{				    f=(SVGFrame)it.next();					paintListener=(CanvasPaintListener)modifyPointFrameTable.get(f);				}catch (Exception ex){paintListener=null;}								if(paintListener!=null){				    				    modifyPointFrameTable.remove(frame);					frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false);				}  			}						final Element elt=(Element)square.getNode();			    		    //the new path		    ExtendedGeneralPath newPath=new ExtendedGeneralPath(), tmpPath=null;					    		    //getting the path that will be analysed		    tmpPath=getSVGEditor().getSVGToolkit().getGeneralPath(elt);			int type=-1, i=0, index=-1;			double[] values=new double[7];			//gets the index of the point that has to be modified 			try{				//if the point is a control point				if(square.getType().indexOf("ctrl")==-1){				    					index=new Integer(square.getType().substring(1,square.getType().length())).intValue();									}else{				    					index=new Integer(square.getType().substring(square.getType().indexOf("ctrl")+4, square.getType().length())).intValue();				}			}catch(Exception ex){}			//for each command in the path, the command and its 

⌨️ 快捷键说明

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