📄 svgline.java
字号:
protected LinkedList drawModifyPointsSelection(SVGFrame frame, Graphics graphics, Node nde){ LinkedList squarelist=new LinkedList(); Element node=(Element)nde; Graphics2D g=(Graphics2D)graphics; if(g!=null && node!=null){ Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); Element root=null; if(doc!=null){ root=doc.getDocumentElement(); } if(root!=null){ int sqd=5; double x1=0, y1=0, x2=0, y2=0; try{ x1=new Double(node.getAttributeNS(null,"x1")).doubleValue(); y1=new Double(node.getAttributeNS(null,"y1")).doubleValue(); x2=new Double(node.getAttributeNS(null,"x2")).doubleValue(); y2=new Double(node.getAttributeNS(null,"y2")).doubleValue(); }catch (Exception ex){} //computes the transformed points BridgeContext ctxt=frame.getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ Point2D pt1=new Point2D.Double(), pt2=new Point2D.Double(); GraphicsNode gnode=ctxt.getGraphicsNode(node); if(gnode!=null){ AffineTransform af=new AffineTransform(); try{af.preConcatenate(gnode.getTransform());}catch (Exception ex){} try{af.preConcatenate(frame.getScrollPane().getSVGCanvas().getViewingTransform());}catch (Exception ex){} try{af.preConcatenate(frame.getScrollPane().getSVGCanvas().getRenderingTransform());}catch (Exception ex){} if(af!=null){ try{ pt2=af.transform(new Point2D.Double(x2,y2), null); pt1=af.transform(new Point2D.Double(x1,y1), null); }catch (Exception e){} if(pt1!=null && pt2!=null){ x1=pt1.getX(); y1=pt1.getY(); x2=pt2.getX(); y2=pt2.getY(); } } } } //computes the coordinates of the selection squares Point2D.Double point1=new Point2D.Double(x1,y1), point2=new Point2D.Double(x2,y2); double sx1=point1.x, sy1=point1.y, sx2=point2.x, sy2=point2.y; int[] sqx=new int[2]; int[] sqy=new int[2]; double sin=(sy2-sy1)/(2*Math.sqrt(Math.pow((sx2-sx1)/2,2)+Math.pow((sy2-sy1)/2,2))), cos=(sx2-sx1)/(2*Math.sqrt(Math.pow((sx2-sx1)/2,2)+Math.pow((sy2-sy1)/2,2))); //computes the coordinates of the two selection points sqx[0]=(int)(sx1+(-sqd/2*cos-sqd)); sqx[1]=(int)(sx2+(sqd/2*cos-sqd)); sqy[0]=(int)(sy1+(-sqd/2*sin-sqd)); sqy[1]=(int)(sy2+(sqd/2*sin-sqd)); //the ids of the selection points String[] types=new String[2]; types[0]="Begin"; types[1]="End"; //the cursors of the selection points Cursor[] cursors=new Cursor[2]; cursors[0]=new Cursor(Cursor.HAND_CURSOR); cursors[1]=new Cursor(Cursor.HAND_CURSOR); //draws the graphic elements for the selection Shape shape=null; GradientPaint gradient=null; for(int i=0;i<2;i++){ if(editor.getSVGSelection()!=null){ squarelist.add(new SVGSelectionSquare(node, types[i], new Rectangle2D.Double(sqx[i],sqy[i],2*sqd,2*sqd), cursors[i])); } shape=getArrow(new Point2D.Double(sqx[i]+sqd, sqy[i]+sqd), types[i], REGULAR_SELECTION); if(shape!=null){ gradient=new GradientPaint(sqx[i], sqy[i], SQUARE_SELECTION_COLOR1, sqx[i]+2*sqd, sqy[i]+2*sqd, SQUARE_SELECTION_COLOR2, true); g.setPaint(gradient); g.fill(shape); g.setColor(LINE_SELECTION_COLOR); g.draw(shape); } } } } return squarelist; } /** * fills the given shape * @param g a graphics * @param shape a shape */ protected void fillShape(Graphics2D g, Shape shape){ if(g!=null && shape!=null){ g.setColor(OUTLINE_COLOR); g.draw(shape); } } /** * 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=modifyPointFrameTable.get(frame); }catch (Exception ex){paintListener=null;} //getting the node and the two points Element elt=(Element)square.getNode(); double x1=0, y1=0, x2=0, y2=0; try{ x1=Double.parseDouble(elt.getAttribute("x1")); y1=Double.parseDouble(elt.getAttribute("y1")); x2=Double.parseDouble(elt.getAttribute("x2")); y2=Double.parseDouble(elt.getAttribute("y2")); }catch (Exception ex){} //the matrix transform SVGTransformMatrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(elt); //the path GeneralPath path=new GeneralPath(); path.moveTo((float)x1, (float)y1); path.lineTo((float)x2, (float)y2); //the transform AffineTransform af=null; //transforming the outline if(matrix!=null && matrix.getTransform()!=null){ af=matrix.getTransform(); try{ path=(GeneralPath)af.createTransformedShape(path); }catch (Exception ex){} //modifying the moved point PathIterator it=path.getPathIterator(new AffineTransform()); double[] seg=new double[6]; if(square.getType().equals("Begin")){ //the first point x1=point2.x; y1=point2.y; //getting the second point it.next(); it.currentSegment(seg); x2=seg[0]; y2=seg[1]; }else if(square.getType().equals("End")){ //getting the first point it.currentSegment(seg); x1=seg[0]; y1=seg[1]; //the second point x2=point2.x; y2=point2.y; } } //the outline GeneralPath endPath=new GeneralPath(); endPath.moveTo((float)x1, (float)y1); endPath.lineTo((float)x2, (float)y2); if(endPath!=null){ //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); } } //getting the node and the two points final Element elt=(Element)square.getNode(); double x1=0, y1=0, x2=0, y2=0; try{ x1=Double.parseDouble(elt.getAttribute("x1")); y1=Double.parseDouble(elt.getAttribute("y1")); x2=Double.parseDouble(elt.getAttribute("x2")); y2=Double.parseDouble(elt.getAttribute("y2")); }catch (Exception ex){} final double initX1=x1, initY1=y1, initX2=x2, initY2=y2; //the matrix transform final SVGTransformMatrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(elt); //the path GeneralPath path=new GeneralPath(); path.moveTo((float)x1, (float)y1); path.lineTo((float)x2, (float)y2); //the transform AffineTransform af=null; //transforming the outline if(matrix!=null && matrix.getTransform()!=null){ af=matrix.getTransform(); try{ path=(GeneralPath)af.createTransformedShape(path); }catch (Exception ex){} //modifying the moved point PathIterator it=path.getPathIterator(new AffineTransform()); double[] seg=new double[6]; if(square.getType().equals("Begin")){ //the first point x1=point2.x; y1=point2.y; //getting the second point it.next(); it.currentSegment(seg); x2=seg[0]; y2=seg[1]; }else if(square.getType().equals("End")){ //getting the first point it.currentSegment(seg); x1=seg[0]; y1=seg[1]; //the second point x2=point2.x; y2=point2.y; } } //storing the new coordinates values final double fx1=x1, fy1=y1, fx2=x2, fy2=y2; //applying the modifications elt.setAttributeNS(null,"x1", format.format(x1)); elt.setAttributeNS(null,"y1", format.format(y1)); elt.setAttributeNS(null,"x2", format.format(x2)); elt.setAttributeNS(null,"y2", format.format(y2)); getSVGEditor().getSVGToolkit().setTransformMatrix(elt, new SVGTransformMatrix(1, 0, 0, 1, 0, 0)); final SVGFrame fframe=frame; //create the undo/redo action and insert it into the undo/redo stack if(getSVGEditor().getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredomodifypoint")){ public void undo(){ //sets the matrix of the node with the old matrix
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -