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

📄 svgpath.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		return menuItems;	}		/**	 * @return a map associating a tool item id to its tool item object	 */	public Hashtable getToolItems(){				Hashtable toolItems=new Hashtable();		toolItems.put((String)ids.get("idquadratic"), quadraticTool);		toolItems.put((String)ids.get("idcubic"), cubicTool);				return toolItems;	}	/**	 * Returns the collection of the popup items	 * @return the collection of the popup items	 */	public Collection getPopupItems(){				LinkedList popupItems=new LinkedList();				SVGPopupSubMenu subMenu=new SVGPopupSubMenu(getSVGEditor(), (String)ids.get("idmenupathoperations"), (String)labels.get("labelpathoperations"), "");				popupItems.add(subMenu);				//creating the convert to path popup item		SVGPopupItem convertItem=new SVGPopupItem(getSVGEditor(), (String)ids.get("idconvert"), (String)labels.get("labelconvert"), (String)ids.get("idconvert")){					public JMenuItem getPopupItem(LinkedList nodes) {								if(nodes!=null && nodes.size()>0){										menuItem.setEnabled(true);										//adds the action listener					menuItem.addActionListener(convertListener);									}else{										menuItem.setEnabled(false);				}								return super.getPopupItem(nodes);			}		};				//creating the path union item		SVGPopupItem unionItem=new SVGPopupItem(getSVGEditor(), (String)ids.get("idunion"), (String)labels.get("labelunion"), (String)ids.get("idunion")){					public JMenuItem getPopupItem(LinkedList nodes){								if(nodes!=null && nodes.size()>=2){										menuItem.setEnabled(true);										//adds the action listener					menuItem.addActionListener(unionListener);									}else{										menuItem.setEnabled(false);				}								return super.getPopupItem(nodes);			}		};				//creating the path subtraction item		SVGPopupItem subtractionItem=new SVGPopupItem(getSVGEditor(), (String)ids.get("idsubtraction"), (String)labels.get("labelsubtraction"), (String)ids.get("idsubtraction")){					public JMenuItem getPopupItem(LinkedList nodes){								if(nodes!=null && nodes.size()==2){										menuItem.setEnabled(true);										//adds the action listener					menuItem.addActionListener(subtractionListener);									}else{										menuItem.setEnabled(false);				}								return super.getPopupItem(nodes);			}		};				//creating the path intersection item		SVGPopupItem intersectionItem=new SVGPopupItem(getSVGEditor(), (String)ids.get("idintersection"), (String)labels.get("labelintersection"), (String)ids.get("idintersection")){					public JMenuItem getPopupItem(LinkedList nodes){								if(nodes!=null && nodes.size()>=2){										menuItem.setEnabled(true);										//adds the action listener					menuItem.addActionListener(intersectionListener);									}else{										menuItem.setEnabled(false);				}								return super.getPopupItem(nodes);			}		};				//adding the popup items to the sub menu		subMenu.addPopupItem(convertItem);		subMenu.addPopupItem(unionItem);		subMenu.addPopupItem(subtractionItem);		subMenu.addPopupItem(intersectionItem);				return popupItems;	}		/**	 * draws a quadratic Bezier curve	 * @param frame the current SVGFrame	 * @param points the array of points	 */	protected void drawQuadraticBezier(SVGFrame frame, Point2D.Double[] points){				if(frame!=null && points!=null && points.length>1){						Document doc=frame.getScrollPane().getSVGCanvas().getDocument();						if(getSVGEditor().getSVGSelection()!=null && doc!=null){			    				final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame);								if(parent!=null && points.length>1){								//creates the string of the values for the attribute					int i;					String value="M "+format.format(points[0].x)+" "+format.format(points[0].y)+" Q ";								for(i=1;i<points.length;i++){					    						value=value.concat(format.format(points[i-1].x+(points[i].x-points[i-1].x)/2)+" "+format.format(points[i-1].y+(points[i].y-points[i-1].y)/2)+" ");						value=value.concat(format.format(points[i].x)+" "+format.format(points[i].y)+" ");					}					//creates the path					Element path = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"path");								path.setAttributeNS(null, "d", value);					String colorString=getSVGEditor().getColorChooser().getColorString(getSVGEditor().getSVGColorManager().getCurrentColor());					path.setAttributeNS(null, "style", "stroke:".concat(colorString.concat("; fill:none")));								//sets that the svg has been modified					frame.setModified(true);								//creates final variables					final Document fdoc=doc;					final Node fpath=path;								// attaches the element to the svg root element					parent.appendChild(fpath);					//create the undo/redo action and insert it into the undo/redo stack					if(getSVGEditor().getUndoRedo()!=null){						SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredocreatequadratic")){							public void undo(){							    							    parent.removeChild(fpath);							}							public void redo(){							    							    parent.appendChild(fpath);							}						};										//adds the undo/redo actions into the stack						SVGSelection selection=getSVGEditor().getSVGSelection();										if(selection!=null){						    							selection.deselectAll(frame, false, true);							selection.addUndoRedoAction(frame, action);							selection.handleNodeSelection(frame, path);							selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredocreatequadratic")){});							selection.refreshSelection(frame);											}else{						    							SVGUndoRedoActionList actionList=new SVGUndoRedoActionList((String)labels.get("undoredocreatequadratic"));							actionList.add(action);							getSVGEditor().getUndoRedo().addActionList(frame, actionList);						}					}				}			}		}	}		/**	 * draws a cubic Bezier curve	 * @param frame the current SVGFrame	 * @param points the array of points	 */	protected void drawCubicBezier(SVGFrame frame, Point2D.Double[] points){				if(frame!=null && points!=null && points.length>1){						Document doc=frame.getScrollPane().getSVGCanvas().getDocument();						if(getSVGEditor().getSVGSelection()!=null && doc!=null){			    				final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame);								if(parent!=null && points.length>1){					//creates the string of the values for the attribute					int i;					String value="M "+format.format(points[0].getX())+" "+format.format(points[0].getY())+" C ";								for(i=1;i<points.length;i++){					    						value=value.concat(format.format(points[i-1].x+(points[i].x-points[i-1].x)/4)+" "+format.format(points[i-1].y+(points[i].y-points[i-1].y)/4)+" ");						value=value.concat(format.format(points[i].x-(points[i].x-points[i-1].x)/4)+" "+format.format(points[i].y-(points[i].y-points[i-1].y)/4)+" ");						value=value.concat(format.format(points[i].x)+" "+format.format(points[i].y)+" ");					}					//creates the path					Element path=doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"path");								path.setAttributeNS(null,"d",value);					String colorString=getSVGEditor().getColorChooser().getColorString(getSVGEditor().getSVGColorManager().getCurrentColor());					path.setAttributeNS(null, "style", "stroke:".concat(colorString.concat("; fill:none")));								//sets that the svg has been modified					frame.setModified(true);								//creates final variables					final Node fpath=path;											// attaches the element to the svg root element					parent.appendChild(fpath);								//create the undo/redo action and insert it into the undo/redo stack					if(getSVGEditor().getUndoRedo()!=null){						SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredocreatecubic")){							public void undo(){							    							    parent.removeChild(fpath);							}							public void redo(){							    							    parent.appendChild(fpath);							}						};										//adds the undo/redo actions into the stack						SVGSelection selection=getSVGEditor().getSVGSelection();										if(selection!=null){						    							selection.deselectAll(frame, false, true);							selection.addUndoRedoAction(frame, action);							selection.handleNodeSelection(frame, path);							selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredocreatecubic")){});							selection.refreshSelection(frame);											}else{						    							SVGUndoRedoActionList actionList=new SVGUndoRedoActionList((String)labels.get("undoredocreatecubic"));							actionList.add(action);							getSVGEditor().getUndoRedo().addActionList(frame, actionList);						}					}				}			}		}	}			/**	 * draws what the path will be if the user double clicks	 * @param frame the current SVGFrame	 * @param graphics the graphics	 * @param points an array of points	 */	protected void drawGhost(SVGFrame frame, Graphics graphics, Point2D.Double[] points){				Graphics2D g=(Graphics2D)graphics;				if(frame!=null && points!=null && points.length>1 && g!=null){		    		    g=(Graphics2D)g.create();						g.setColor(GHOST_COLOR);			g.setXORMode(Color.white);			for(int i=1;i<points.length;i++){			    				g.drawLine((int)points[i-1].x, (int)points[i-1].y, (int)points[i].x, (int)points[i].y);			}						g.dispose();					}			}		/**	 * converts the nodes of the given list into paths	 * @param frame the current SVGFrame	 * @param nodes the list of the nodes to be converted	 */	protected void convertToPath(SVGFrame frame, LinkedList nodes){				Document doc=frame.getScrollPane().getSVGCanvas().getDocument();				LinkedList snodes=new LinkedList(nodes);			    //getting the parent element	    Element p=null;	    	    try{	        p=(Element)((Element)snodes.getFirst()).getParentNode();	    }catch(Exception ex){p=null;}	    	    final Element parent=p;								if(doc!=null && snodes!=null && snodes.size()>0 && parent!=null){			final LinkedList oldchildren=new LinkedList();			NodeList ch=parent.getChildNodes();			Node node=null;			int i;						//the list of the children of the parent element			for(i=0;i<ch.getLength();i++){				oldchildren.add(ch.item(i));			}			//the list of the path that will be created			LinkedList paths=new LinkedList();			Element cur=null;			Shape outline=null;			GeneralPath gpath=null;			LinkedHashMap map=null;			LinkedList nlist=null;			PathIterator pit=null;			char cmd=' ';			int rg=0;			double[] values=null, vals=new double[7];			int type=-1;			Node cnode=null;						for(Iterator it=snodes.iterator(); it.hasNext();){			    				try{cur=(Element)it.next();}catch (Exception ex){cur=null;}								if(cur!=null){				    outline=frame.getOutline(cur);					gpath=new GeneralPath(outline);				    				    if(gpath!=null){				        						gpath.closePath();						map=new LinkedHashMap();						rg=0;												//for each command in the path, the command and its values are added to the string value						for(pit=gpath.getPathIterator(new AffineTransform()); ! pit.isDone(); pit.next()){						    							type=pit.currentSegment(vals);														if(type==PathIterator.SEG_CLOSE){							    								cmd='Z';															}else if(type==PathIterator.SEG_CUBICTO){							    								values=new double[6];								pit.currentSegment(values);								cmd='C';															}else if(type==PathIterator.SEG_LINETO){							    								values=new double[2];								pit.currentSegment(values);								cmd='L';															}else if(type==PathIterator.SEG_MOVETO){							    								values=new double[2];								pit.currentSegment(values);								cmd='M';															}else if(type==PathIterator.SEG_QUADTO){							    								values=new double[4];								pit.currentSegment(values);								cmd='Q';															}else{							    							    cmd=' ';							    values=null;							}							if(values!=null){							    								nlist=new LinkedList();																for(i=0;i<values.length;i++){								    									nlist.add(new Double(values[i]));								}																map.put(cmd+new Integer(rg++).toString(), nlist);							}						}												//creates the path element						Element path=doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"path");												//converts the map to pass to accurate argument for setting the d attribute						LinkedHashMap map2=getSVGEditor().getSVGToolkit().convertPathValues(map);						getSVGEditor().getSVGToolkit().setPathSeg(path, map2);						String colorString=getSVGEditor().getColorChooser().getColorString(getSVGEditor().getSVGColorManager().getCurrentColor());												path.setAttributeNS(null, "style", "fill:".concat(colorString.concat(";")));						//appends the children nodes of the current node to the path node						if(cur instanceof Element && ! cur.getNodeName().equals("text")){						    for(node=cur.getFirstChild(); node!=null; node=node.getNextSibling()){						        						        cnode=node.cloneNode(true);						        						        if(cnode!=null){						            						            path.appendChild(cnode);						        }						    }

⌨️ 快捷键说明

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