📄 svgpath.java
字号:
} parent.appendChild(path); paths.add(path); parent.removeChild(cur); frame.unregisterAllUsedResource(node); } } } //creates final variables final LinkedList newchildren=new LinkedList(); final SVGFrame fframe=frame; ch=parent.getChildNodes(); //the new list of the children of the parent element for(i=0;i<ch.getLength();i++){ newchildren.add(ch.item(i)); } //sets that the svg has been modified frame.setModified(true); //create the undo/redo action and insert it into the undo/redo stack if(getSVGEditor().getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredoconvert")){ public void undo(){ Node node=null; //removes all the nodes from the parent element while(parent.hasChildNodes()){ node=parent.getFirstChild(); parent.removeChild(node); fframe.unregisterAllUsedResource(node); } //appends all the old children for(int i=0;i<oldchildren.size();i++){ try{ node=(Node)oldchildren.get(i); }catch(Exception ex){node=null;} if(node!=null){ parent.appendChild(node); fframe.registerUsedResource(node); } } } /** * used to call all the actions that have to be done to redo an action */ public void redo(){ Node node=null; //removes all the old children for(int i=0;i<oldchildren.size();i++){ try{ node=(Node)oldchildren.get(i); }catch(Exception ex){node=null;} if(node!=null){ parent.removeChild(node); fframe.unregisterAllUsedResource(node); } } //appends all the new children for(int i=0;i<newchildren.size();i++){ try{ node=(Node)newchildren.get(i); }catch(Exception ex){} if(node!=null){ parent.appendChild(node); fframe.registerUsedResource(node); } } } }; //adds the undo/redo actions into the stack SVGSelection selection=getSVGEditor().getSVGSelection(); if(selection!=null){ selection.deselectAll(frame, false, false); selection.addUndoRedoAction(frame, action); //selects all the path nodes for(Iterator it=paths.iterator(); it.hasNext();){ try{ node=(Node)it.next(); }catch(Exception ex){node=null;} if(node!=null && node instanceof Element){ selection.handleNodeSelection(frame, (Element)node); } } selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredoconvert")){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionList=new SVGUndoRedoActionList((String)labels.get("undoredoconvert")); actionList.add(action); getSVGEditor().getUndoRedo().addActionList(frame, actionList); } } } } /** * converts the nodes of the given list into a path node by making a union * @param frame the current SVGFrame * @param nodes the list of the nodes to be converted */ protected void union(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 && parent!=null && nodes!=null && nodes.size()>0){ final LinkedList oldchildren=new LinkedList(); NodeList ch=parent.getChildNodes(); //the list of the children of the root element for(int i=0;i<ch.getLength();i++){ oldchildren.add(ch.item(i)); } //creating the union of the shapes of the elements Area area=new Area(); Element cur=null; Shape outline=null; for(Iterator it=nodes.iterator(); it.hasNext();){ try{ cur=(Element)it.next(); }catch (Exception ex){cur=null;} if(cur!=null){ outline=frame.getOutline(cur); area.add(new Area(outline)); //removes this node from the children nodes parent.removeChild(cur); frame.unregisterAllUsedResource(cur); } } //creating the path ExtendedGeneralPath exPath=new ExtendedGeneralPath(area); if(exPath!=null){ LinkedHashMap map=new LinkedHashMap(); LinkedList nlist=null; char cmd=' '; double[] values=new double[7], vals=new double[7]; int type=-1, rg=0; //for each command in the path, the command and its values are added to the map for(ExtendedPathIterator pit=exPath.getExtendedPathIterator(); ! pit.isDone(); pit.next()){ type=pit.currentSegment(vals); if(type==ExtendedPathIterator.SEG_CLOSE){ values=null; cmd='Z'; }else if(type==ExtendedPathIterator.SEG_CUBICTO){ values=new double[6]; pit.currentSegment(values); cmd='C'; }else if(type==ExtendedPathIterator.SEG_LINETO){ values=new double[2]; pit.currentSegment(values); cmd='L'; }else if(type==ExtendedPathIterator.SEG_MOVETO){ values=new double[2]; pit.currentSegment(values); cmd='M'; }else if(type==ExtendedPathIterator.SEG_QUADTO){ values=new double[4]; pit.currentSegment(values); cmd='Q'; }else if(type==ExtendedPathIterator.SEG_ARCTO){ values=new double[7]; pit.currentSegment(values); cmd='A'; }else{ cmd=' '; values=null; } //adding the current values to the map if(values!=null){ nlist=new LinkedList(); for(int 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"); //converting the map of double values into point values 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(";"))); //sets that the svg has been modified frame.setModified(true); //creates final variables final Node fpath=path; final LinkedList newchildren=new LinkedList(); final SVGFrame fframe=frame; //appends the path to the root node parent.appendChild(fpath); Node node=null; ch=parent.getChildNodes(); //the newlist of the children of the root element for(int i=0;i<ch.getLength();i++){ newchildren.add(ch.item(i)); } //create the undo/redo action and insert it into the undo/redo stack if(getSVGEditor().getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredounion")){ public void undo(){ Node node=null; //removes all the nodes from the root element while(parent.hasChildNodes()){ node=parent.getFirstChild(); parent.removeChild(node); fframe.unregisterAllUsedResource(node); } //appends all the old children for(int i=0;i<oldchildren.size();i++){ try{ node=(Node)oldchildren.get(i); }catch(Exception ex){node=null;} if(node!=null){ parent.appendChild(node); fframe.registerUsedResource(node); } } } public void redo(){ Node node=null; //removes all the nodes from the root element while(parent.hasChildNodes()){ node=parent.getFirstChild(); parent.removeChild(node); fframe.unregisterAllUsedResource(node); } //appends all the new children for(int i=0;i<newchildren.size();i++){ try{ node=(Node)newchildren.get(i); }catch(Exception ex){node=null;} if(node!=null){ parent.appendChild(node); fframe.registerUsedResource(node); } } } }; //adds the undo/redo actions into the stack SVGSelection selection=getSVGEditor().getSVGSelection(); if(selection!=null){ selection.deselectAll(frame, false, false); selection.addUndoRedoAction(frame, action); selection.handleNodeSelection(frame, path); selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredounion")){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionList=new SVGUndoRedoActionList((String)labels.get("undoredounion")); actionList.add(action); getSVGEditor().getUndoRedo().addActionList(frame, actionList); } } } } } /** * converts the nodes of the given list into a path node by making a subtraction * @param frame the current SVGFrame * @param nodes the list of the nodes to be converted */ protected void subtraction(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 && parent!=null && nodes!=null && nodes.size()==2){ final LinkedList oldchildren=new LinkedList(); NodeList ch=parent.getChildNodes(); //the list of the children of the parent element for(int i=0;i<ch.getLength();i++){ oldchildren.add(ch.item(i)); } Element cur=null; Area area=null; Shape shape=null; for(Iterator it=nodes.iterator(); it.hasNext();){ try{cur=(Element)it.next();}catch (Exception ex){cur=null;} if(cur!=null){ shape=frame.getOutline(cur); try{ //creates the area (shape) that will be used to make the union if(area==null){ area=new Area(shape); }else{ //adds the current shape to the area area.subtract(new Area(shape)); } }catch (Exception ex){} final Element fcur=cur; parent.removeChild(fcur); frame.unregisterAllUsedResource(fcur); } } if(area!=null){ //creates a path that allows to creates the string value of the path element ExtendedGeneralPath gpath=new ExtendedGeneralPath(area); LinkedHashMap map=new LinkedHashMap(); LinkedList nlist=null; char cmd=' '; double[] values=new double[7], vals=new double[7]; int type=-1, rg=0, i; //for each command in the path, the command and its values are added to the map for(ExtendedPathIterator pit=gpath.getExtendedPathIterator(); ! pit.isDone(); pit.next()){ type=pit.currentSegment(vals); if(type==ExtendedPathIterator.SEG_CLOSE){ values=null; cmd='Z'; }else if(type==ExtendedPathIterator.SEG_CUBICTO){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -