📄 svgpolyline.java
字号:
//computes the coordinates of the selection squares Point2D.Double[] scpoints=new Point2D.Double[points.length]; for(i=0;i<points.length;i++){ scpoints[i]=frame.getScaledPoint(points[i], false); } //the cursor associated with the selection points Cursor cursor=new Cursor(Cursor.HAND_CURSOR); //draws the selection Shape shape=null; GradientPaint gradient=null; int sqx=0,sqy=0; for(i=0;i<scpoints.length;i++){ sqx=(int)(scpoints[i].getX()-sqd); sqy=(int)(scpoints[i].getY()-sqd); if(getSVGEditor().getSVGSelection()!=null){ squarelist.add(new SVGSelectionSquare(node, new Integer(i).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); } } } } } 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 && square.getNode() instanceof Element && point2!=null){ //gets the paintlistener associated with the frame CanvasPaintListener paintListener=null; try{paintListener=(CanvasPaintListener)modifyPointFrameTable.get(frame);}catch (Exception ex){paintListener=null;} Element elt=(Element)square.getNode(); String pts=elt.getAttributeNS(null,"points"); if(pts!=null && ! pts.equals("")){ double px=0, py=0; //gets the coordinates of the points ArrayList listpt=new ArrayList(); int i=0; pts=pts.replaceAll("[,]"," "); //removes the first space characters from the string if(!pts.equals("") && pts.charAt(0)==' '){ pts=pts.replaceFirst("[\\s]+",""); } while(! pts.equals("")){ try{ px=new Double(pts.substring(0,pts.indexOf(' '))).doubleValue(); pts=pts.substring(pts.indexOf(' ')+1,pts.length()); if(!pts.equals("") && pts.charAt(0)==' '){ pts=pts.replaceFirst("[\\s]+",""); } py=new Double(pts.substring(0,pts.indexOf(' '))).doubleValue(); pts=pts.substring(pts.indexOf(' ')+1,pts.length()); if(!pts.equals("") && pts.charAt(0)==' '){ pts=pts.replaceFirst("[\\s]+",""); } listpt.add(new Point2D.Double(px, py)); }catch (Exception ex){break;} } //the transform matrix SVGTransformMatrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(elt); //the affine transform AffineTransform af=matrix.getTransform(); //the path will be displayed GeneralPath path=new GeneralPath(); Point2D.Double pt0=(Point2D.Double)listpt.get(0), pt=null; //the initial point path.moveTo((float)pt0.x, (float)pt0.y); //building the path for(i=1;i<listpt.size();i++){ pt=(Point2D.Double)listpt.get(i); path.lineTo((float)pt.x, (float)pt.y); } //transforming the path// try{ path=(GeneralPath)af.createTransformedShape(path); }catch (Exception ex){} //the index of the moved point int index=-1; try{ index=Integer.parseInt(square.getType()); }catch(Exception ex){} //creating the new path GeneralPath endPath=new GeneralPath(); float[] seg=new float[6]; i=0; for(PathIterator it=path.getPathIterator(new AffineTransform()); ! it.isDone(); i++){ //getting the current segment it.currentSegment(seg); it.next(); try{ if(i==0 && i!=index){ endPath.moveTo(seg[0], seg[1]); }else if(i>0 && i!=index){ endPath.lineTo(seg[0], seg[1]); //modifying the moved point }else if(index>=0 && index==i){ if(i==0){ endPath.moveTo((float)point2.x, (float)point2.y); }else if(i>0){ endPath.lineTo((float)point2.x, (float)point2.y); } } }catch (Exception ex){} } //computes the scale and translate values if(endPath!=null){ //closing the path endPath.closePath(); //concatenates the transforms to draw the outline 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(endPath); 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 && square.getNode() instanceof Element && 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(); final String initPts=elt.getAttributeNS(null,"points"); String pts=new String(initPts); if(pts!=null && ! pts.equals("")){ double px=0, py=0; //gets the coordinates of the points ArrayList listpt=new ArrayList(); int i=0; pts=pts.replaceAll("[,]"," "); //removes the first space characters from the string if(!pts.equals("") && pts.charAt(0)==' '){ pts=pts.replaceFirst("[\\s]+",""); } while(!pts.equals("")){ try{ px=new Double(pts.substring(0,pts.indexOf(' '))).doubleValue(); pts=pts.substring(pts.indexOf(' ')+1,pts.length()); if(!pts.equals("") && pts.charAt(0)==' '){ pts=pts.replaceFirst("[\\s]+",""); } py=new Double(pts.substring(0,pts.indexOf(' '))).doubleValue(); pts=pts.substring(pts.indexOf(' ')+1,pts.length()); if(!pts.equals("") && pts.charAt(0)==' '){ pts=pts.replaceFirst("[\\s]+",""); } listpt.add(new Point2D.Double(px, py)); }catch (Exception ex){break;} } //the transform matrix final SVGTransformMatrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(elt); //the affine transform AffineTransform af=matrix.getTransform(); //the path will be displayed GeneralPath path=new GeneralPath(); Point2D.Double pt0=(Point2D.Double)listpt.get(0), pt=null; //the initial point path.moveTo((float)pt0.x, (float)pt0.y); //building the path for(i=1;i<listpt.size();i++){ pt=(Point2D.Double)listpt.get(i); path.lineTo((float)pt.x, (float)pt.y); } //transforming the path// try{ path=(GeneralPath)af.createTransformedShape(path); }catch (Exception ex){} //the index of the moved point int index=-1; try{ index=Integer.parseInt(square.getType()); }catch(Exception ex){} //creating the new string path String points=""; float[] seg=new float[6]; i=0; for(PathIterator it=path.getPathIterator(new AffineTransform()); ! it.isDone();){ //getting the current segment it.currentSegment(seg); it.next(); if(index!=i){ points=points.concat(format.format(seg[0]).concat(" , ").concat(format.format(seg[1]).concat(" "))); }else{ points=points.concat(format.format(point2.x).concat(" , ").concat(format.format(point2.y).concat(" "))); } i++; } //computes the scale and translate values if(points!=null && ! points.equals("")){ //applying the modifications getSVGEditor().getSVGToolkit().setTransformMatrix(elt, new SVGTransformMatrix(1, 0, 0, 1, 0, 0)); elt.setAttributeNS(null, "points", points); //create the undo/redo action and insert it into the undo/redo stack if(getSVGEditor().getUndoRedo()!=null){ final String fpoints=points; SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredomodifypoint")){ public void undo(){ getSVGEditor().getSVGToolkit().setTransformMatrix(elt, matrix); elt.setAttributeNS(null, "points", initPts); //notifies that the selection has changed if(getSVGEditor().getSVGSelection()!=null){ getSVGEditor().getSVGSelection().selectionChanged(true); } } public void redo(){ getSVGEditor().getSVGToolkit().setTransformMatrix(elt, new SVGTransformMatrix(1, 0, 0, 1, 0, 0)); elt.setAttributeNS(null, "points", fpoints); //notifies that the selection has changed if(getSVGEditor().getSVGSelection()!=null){ getSVGEditor().getSVGSelection().selectionChanged(true); } } };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -