📄 svgframe.java
字号:
} editor.getDesktop().repaint(); } /** * @return the internal frame if the multi windowed mode is activated */ public JInternalFrame getInternalFrame(){ return internalFrame; } /** * the method used to get the point correponding to the given point when aligned with the rulers * @param point the point * @return the point correponding to the given point when aligned with the rulers */ public Point2D.Double getAlignedWithRulersPoint(Point point){ if(point!=null){ return getScrollPane().getAlignedWithRulersPoint(new Point2D.Double(point.x, point.y)); } return null; } /** * the method used to get the point correponding to the given point with integer coordinates when aligned with the rulers * @param point the point * @return the point correponding to the given point when aligned with the rulers */ public Point getAlignedWithRulersIntegerPoint(Point point){ if(point!=null){ Point2D.Double point2D=getAlignedWithRulersPoint(point); if(point2D!=null){ return new Point((int)point2D.x, (int)point2D.y); } } return null; } /** * computes the position and the size of a node on the canvas * @param shape the node whose position and size is to be computed * @return a rectangle representing the position and size of the given node */ public Rectangle2D getNodeBounds(Element shape){ Rectangle2D bounds=new Rectangle(); if(shape!=null){ //gets the bridge context BridgeContext ctxt=getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ //gets the graphics node corresponding to the given node GraphicsNode gnode=null; try{gnode=ctxt.getGraphicsNode(shape);}catch (Exception e){gnode=null;} if(gnode!=null){ bounds=gnode.getGeometryBounds(); AffineTransform affine=new AffineTransform(); if(gnode.getTransform()!=null){ affine.preConcatenate(gnode.getTransform()); } if(scrollpane.getSVGCanvas().getViewingTransform()!=null){ affine.preConcatenate(scrollpane.getSVGCanvas().getViewingTransform()); } if(scrollpane.getSVGCanvas().getRenderingTransform()!=null){ affine.preConcatenate(scrollpane.getSVGCanvas().getRenderingTransform()); } try{ bounds=affine.createTransformedShape(bounds).getBounds2D(); }catch (Exception ex) {} } } } return bounds; } /** * computes the position and the size of a node on the canvas * @param shape the node whose position and size is to be computed * @return a rectangle representing the position and size of the given node */ public Rectangle2D.Double getNodeGeometryBounds(Element shape){ Rectangle2D.Double bounds=new Rectangle2D.Double(); if(shape!=null){ //gets the bridge context BridgeContext ctxt=getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ //gets the graphics node corresponding to the given node GraphicsNode gnode=null; try{gnode=ctxt.getGraphicsNode(shape);}catch (Exception e){gnode=null;} if(gnode!=null){ Rectangle2D bounds2D=gnode.getTransformedBounds(new AffineTransform()); if(bounds2D!=null) { bounds=new Rectangle2D.Double( bounds2D.getX(), bounds2D.getY(), bounds2D.getWidth(), bounds2D.getHeight()); } } } } return bounds; } /** * computes the outline of a node on the canvas that has the node's transform applied * @param shapeNode a shape node * @param af an affine transform * @return the outline of the given node */ public Shape getTransformedOutline(Element shapeNode, AffineTransform af){ Shape shape=new Rectangle(); if(shapeNode!=null){ if(af==null){ af=new AffineTransform(); } //gets the bridge context BridgeContext ctxt=getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ //gets the graphics node corresponding to the given node GraphicsNode gnode=null; try{gnode=ctxt.getGraphicsNode(shapeNode);}catch (Exception e){gnode=null;} if(gnode!=null){ shape=gnode.getOutline(); //transforming the shape if(shape!=null){ AffineTransform affine=new AffineTransform(); if(gnode.getTransform()!=null){ affine.preConcatenate(gnode.getTransform()); } affine.preConcatenate(af); if(scrollpane.getSVGCanvas().getViewingTransform()!=null){ affine.preConcatenate(scrollpane.getSVGCanvas().getViewingTransform()); } if(scrollpane.getSVGCanvas().getRenderingTransform()!=null){ affine.preConcatenate(scrollpane.getSVGCanvas().getRenderingTransform()); } shape=affine.createTransformedShape(shape); } } } } return shape; } /** * computes the transform of a node on the canvas that has the node's transform applied * @param shapeNode a shape node * @return the transform of the given node */ public AffineTransform getTransform(Node shapeNode){ AffineTransform af=null; if(shapeNode!=null && shapeNode instanceof Element){ //gets the bridge context BridgeContext ctxt=getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ //gets the graphics node corresponding to the given node GraphicsNode gnode=null; try{gnode=ctxt.getGraphicsNode((Element)shapeNode);}catch (Exception e){gnode=null;} if(gnode!=null){ af=gnode.getTransform(); } } } return af; } /** * computes the outline of a node on the canvas * @param node the node * @return the outline of the given node */ public Shape getOutline(Node node){ Shape shape=new Rectangle(); if(node!=null && node instanceof Element){ //gets the bridge context BridgeContext ctxt=getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ //gets the graphics node corresponding to the given node GraphicsNode gnode=null; try{gnode=ctxt.getGraphicsNode((Element)node);}catch (Exception e){gnode=null;} if(gnode!=null){ AffineTransform affine=new AffineTransform(); if(gnode.getTransform()!=null){ affine.preConcatenate(gnode.getTransform()); } try{ shape=affine.createTransformedShape(getGeometryShape(gnode)); }catch (Exception ex){} } } } return shape; } /** * returns the shape of the given graphics node * @param graphicsNode a graphics node * @return the shape of the given graphics node */ protected Shape getGeometryShape(GraphicsNode graphicsNode){ Shape shape=new Rectangle(); if(graphicsNode!=null){ shape=graphicsNode.getOutline(); } return shape; } /** * returns the nodes at the given point * @param parent the parent node * @param point the point on which a mouse event has been done * @return the node on which a mouse event has been done */ public Node getNodeAt(Node parent, Point2D.Double point){ Node pointNode=null; if(parent!=null && point!=null){ int diff=6; GraphicsNode gpointNode=null; //getting the graphics node of the parent node BridgeContext ctxt=getScrollPane().getSVGCanvas().getBridgeContext(); if(ctxt!=null){ GraphicsNode gparentNode=null; try{gparentNode=ctxt.getGraphicsNode((Element)parent);}catch (Exception e){gparentNode=null;} if(gparentNode!=null){ //setting the selection mode for the parent and its children gparentNode.setPointerEventType(GraphicsNode.VISIBLE_PAINTED); //retrieving the node corresponding to the given point and its associated area gpointNode=gparentNode.nodeHitAt(point); if(gpointNode==null){ for(int i=-diff/2; i<=diff/2; i++){ gpointNode=gparentNode.nodeHitAt(new Point2D.Double(point.x-diff/2, point.y+i)); if(gpointNode!=null){ break; } gpointNode=gparentNode.nodeHitAt(new Point2D.Double(point.x+diff/2, point.y+i)); if(gpointNode!=null){ break; } gpointNode=gparentNode.nodeHitAt(new Point2D.Double(point.x+i, point.y-diff/2)); if(gpointNode!=null){ break; } gpointNode=gparentNode.nodeHitAt(new Point2D.Double(point.x+i, point.y+diff/2)); if(gpointNode!=null){ break; } } } if(gpointNode!=null){ //getting the graphics node corresponding to the found graphics node and that is //a parent of the parent graphics node while(gpointNode!=null && ! gpointNode.getParent().equals(gparentNode)){ gpointNode=gpointNode.getParent(); } if(gpointNode!=null){ //retrieving the element corresponding to this node pointNode=ctxt.getElement(gpointNode); //checks the type of the nodes if( pointNode!=null && (pointNode.getNodeName().equals("defs") || pointNode.getNodeName().equals("svg"))){ pointNode=null; } } } } } } return pointNode; } /** * find the accurate id for a node * @param baseString the base of the id * @return an id */ public String getId(String baseString){ Document doc=getScrollPane().getSVGCanvas().getDocument(); if(doc!=null){ LinkedList<String> ids=new LinkedList<String>(); Node current=null; String attId=""; //adds to the list all the ids found among the children of the root element for(NodeIterator it=new NodeIterator(doc.getDocumentElement()); it.hasNext();){ current=it.next(); if(current!=null && current instanceof Element){ attId=((Element)current).getAttribute("id"); if(attId!=null && ! attId.equals("")){ ids.add(attId); } } } int i=0; //tests for each integer string if it is already an id while(ids.contains(baseString.concat(i+""))){ i++; } return new String(baseString.concat(i+"")); } return ""; } /** * checks whether the given id already exists or not among the children of the given root node * @param id an id to be checked * @return true if the id does not already exists */ public boolean checkId(String id){ Document doc=getScrollPane().getSVGCanvas().getDocument(); if(doc!=null){ LinkedList<String> ids=new LinkedList<String>(); Node current=null; String attId=""; //adds to the list all the ids found among the children of the root element for(NodeIterator it=new NodeIterator(doc.getDocumentElement()); it.hasNext();){ current=it.next(); if(current!=null && current instanceof Element){ attId=((Element)current).getAttribute("id"); if(attId!=null && ! attId.equals("")){ ids.add(attId); } } } //tests for each integer string if it is already an id if(ids.contains(id)){ return false; } return true; } return false; } /** * @return the "defs" node in a svg document */ public Element getDefsElement(){ Element defs=null; Document doc=getScrollPane().getSVGCanvas().getDocument(); if(doc!=null){ Element root=doc.getDocumentElement(); if(root!=null){ final NodeList defsNodes=doc.getElementsByTagName("defs"); if(defsNodes!=null && defsNodes.getLength()>0){ defs=(Element)defsNodes.item(0); //getting the list of the additionnal defs if(defsNodes.getLength()>1) { ArrayList<Element> additionalDefs=new ArrayList<Element>(); Element defs2=null; //inserting each resource that could be found in other "defs" nodesto the first "defs" node for(int i=1; i<defsNodes.getLength(); i++) { defs2=(Element)defsNodes.item(i); additionalDefs.add(defs2); while (defs2.hasChildNodes()){ defs.appendChild(defs2.removeChild(defs2.getFirstChild())); } } //for each extra "defs" found nodes for(Element defs3 : additionalDefs) { defs3.getParentNode().removeChild(defs3); } } } if(defs==null){ //adds a "defs" node to the svg document defs=doc.createElementNS(null, "defs");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -