📄 svgpath.java
字号:
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(i=0;i<values.length;i++){ nlist.add(new Double(values[i])); } map.put(cmd+new Integer(rg++).toString(), nlist); } } //creates the path element final 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(";"))); //attaches the path to the svg parent element parent.appendChild(path); //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; Node node=null; ch=parent.getChildNodes(); //the newlist of the children of the parent element for(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("undoredosubtraction")){ 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 nodes from the parent 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("undoredosubtraction")){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionList=new SVGUndoRedoActionList((String)labels.get("undoredosubtraction")); actionList.add(action); getSVGEditor().getUndoRedo().addActionList(frame, actionList); } } } } } /** * converts the nodes of the given list into a path node by making an intersection * @param frame the current SVGFrame * @param nodes the list of the nodes to be converted */ protected void intersection(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 parent element for(int i=0;i<ch.getLength();i++){ oldchildren.add(ch.item(i)); } Shape shape=null; Element cur=null; Area area=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.intersect(new Area(shape)); } }catch (Exception ex){} parent.removeChild(cur); frame.unregisterAllUsedResource(cur); } } 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){ 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(i=0;i<values.length;i++){ nlist.add(new Double(values[i])); } map.put(cmd+new Integer(rg++).toString(), nlist); } } //creates the path element final 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(";"))); //attaches the path to the svg root element parent.appendChild(path); //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; Node node=null; ch=parent.getChildNodes(); //the newlist of the children of the root element for(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("undoredointersection")){ 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("undoredointersection")){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionList=new SVGUndoRedoActionList((String)labels.get("undoredointersection")); actionList.add(action); getSVGEditor().getUndoRedo().addActionList(frame, actionList); } } } } } /** * gets the nexts level after the given selection level * @param type a selection level * @return the next selection level */ public String getNextLevel(String type){ if(type!=null){ if(type.equals("level1")){ return "level2"; }else if(type.equals("level2")){ return "level3"; }else if(type.equals("level3")){ return "level1"; } } return "level1"; } /** * draws the selection for the path * @param frame the current SVGFrame * @param graphics the graphics * @param node the node to be selected * @return the list of the selection squares */ protected LinkedList drawModifyPointsSelection(SVGFrame frame, Graphics graphics, Node node){ LinkedList squarelist=new LinkedList(); Graphics2D g=(Graphics2D)graphics; if(frame!=null && g!=null && node!=null){ int sqd=5;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -