📄 svgcanvas.java~1~
字号:
SwingUtilities.invokeLater(runnable); }else { runnable.run(); } } } /** * creates a new svg document * @param width the width of the new document * @param height the height of the new document */ public void newDocument(final String width, final String height){ useMonitor=false; //initializing the progress bar initializeProgressBar(scrollpane.getSVGFrame().getShortName()); handleProgressBar(0, documentCreatingLabel, false, false); SwingUtilities.invokeLater(new Runnable(){ public void run() { DOMImplementation impl=SVGDOMImplementation.getDOMImplementation(); String svgNS=SVGDOMImplementation.SVG_NAMESPACE_URI; SVGDocument doc=(SVGDocument)impl.createDocument(svgNS, "svg", null); //gets the root element (the svg element) Element svgRoot=doc.getDocumentElement(); //set the width and height attribute on the root svg element svgRoot.setAttributeNS(null, "width", width); svgRoot.setAttributeNS(null, "height", height); svgRoot.setAttribute("viewBox","0 0 "+SVGToolkit.getPixelledNumber(width)+" "+SVGToolkit.getPixelledNumber(height)); //initializing the canvas SVGToolkit.checkRtdaXmlns(doc); initializeCanvas(doc); frame.setModified(true); } }); } /** * sets the uri for the canvas * @param uri a uri */ public void setURI(final String uri){ useMonitor=true; if(uri!=null && ! uri.equals("")){ this.uri=uri; //initializing the progress bar initializeProgressBar(scrollpane.getSVGFrame().getShortName()); handleProgressBar(0, documentLoadingLabel, false, false); Thread thread=new Thread(){ @Override public void run() { if(SVGEditor.isRtdaAnimationsVersion) { projectFile=SVGEditor.getColorChooser().getProjectFile(uri); } try{ //creating the svg document corresponding to this uri SAXSVGDocumentFactory factory=new SAXSVGDocumentFactory(""); SVGDocument doc=factory.createSVGDocument(uri); if(doc!=null){ Dimension scaledCanvasSize=getScaledCanvasSize(doc.getDocumentElement()); frame.handleInitialDOMOperations(doc, scaledCanvasSize); SVGToolkit.checkRtdaXmlns(doc); initializeCanvas(doc); } }catch(Exception ex){ex.printStackTrace();handleProgressBar(0, "", true, false);} } }; thread.start(); } } /** * @return the uri of the canvas */ public String getURI(){ return uri; } /** * @return the viewing transform */ public AffineTransform getViewingTransform(){ CanvasGraphicsNode canvasGraphicsNode=getCanvasGraphicsNode(); if(canvasGraphicsNode!=null) { return canvasGraphicsNode.getViewingTransform(); } return null; } /** * @return the rendering transform */ public AffineTransform getRenderingTransform(){ return AffineTransform.getScaleInstance(scale, scale); } /** * @return the offscreen image */ public BufferedImage getOffscreen(){ return canvasOffscreenImage; } /** * @return the document of the canvas */ public Document getDocument(){ return document; } /** * setting the preferred size for this canvas * @param size the preferred size */ public void setCanvasPreferredSize(Dimension size){ setPreferredSize(size); canvas.setSize(size); paintersPanel.setSize(size); } /** * @return the bridge context */ public BridgeContext getBridgeContext(){ return ctx; } /** * @return the update manager */ public UpdateManager getUpdateManager() { return manager; } /** * paints the canvas * @param gr a graphics object */ protected void paintCanvas(Graphics gr){ Graphics2D g=(Graphics2D)gr.create(); //drawing the offscreen image and painting it if(shouldRepaintSVGContent){ //getting the gvt root GraphicsNode root=gvtRoot; if(root!=null){ Rectangle usedRectangle=null; boolean isScrollAction=false; int scrollX=0, scrollY=0; if(tmpRectangle!=null){ usedRectangle=new Rectangle(tmpRectangle); scrollX=usedRectangle.x-renderedRectangle.x; scrollY=usedRectangle.y-renderedRectangle.y; } //checking if the rendered rectangle will be changed owing to a scroll action if( usedRectangle!=null && usedRectangle.width==renderedRectangle.width && usedRectangle.height==renderedRectangle.height && Math.abs(scrollX)<usedRectangle.width && Math.abs(scrollY)<usedRectangle.height){ renderedRectangle.x=usedRectangle.x; renderedRectangle.y=usedRectangle.y; BufferedImage image=new BufferedImage(renderedRectangle.width, renderedRectangle.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2=image.createGraphics(); BufferedImage tmpImage=null; if(scrollY>0){ tmpImage=canvasOffscreenImage.getSubimage(0, scrollY, renderedRectangle.width, renderedRectangle.height-scrollY); g2.drawImage(tmpImage, 0, 0, tmpImage.getWidth(), tmpImage.getHeight(), null); }else if(scrollY<0){ tmpImage=canvasOffscreenImage.getSubimage(0, 0, renderedRectangle.width, renderedRectangle.height+scrollY); g2.drawImage(tmpImage, 0, -scrollY, tmpImage.getWidth(), tmpImage.getHeight(), null); } if(scrollX>0){ tmpImage=canvasOffscreenImage.getSubimage(scrollX, 0, renderedRectangle.width-scrollX, renderedRectangle.height); g2.drawImage(tmpImage, 0, 0, tmpImage.getWidth(), tmpImage.getHeight(), null); }else if(scrollX<0){ tmpImage=canvasOffscreenImage.getSubimage(0, 0, renderedRectangle.width+scrollX, renderedRectangle.height); g2.drawImage(tmpImage, -scrollX, 0, tmpImage.getWidth(), tmpImage.getHeight(), null); } g2.dispose(); canvasOffscreenImage=image; isScrollAction=true; }else if( ! shouldUpdateSVGContent){ if(usedRectangle!=null){ renderedRectangle.x=usedRectangle.x; renderedRectangle.y=usedRectangle.y; renderedRectangle.width=usedRectangle.width; renderedRectangle.height=usedRectangle.height; } //creating the new offscreen image canvasOffscreenImage=new BufferedImage(renderedRectangle.width, renderedRectangle.height, BufferedImage.TYPE_INT_ARGB); } if(isScrollAction){ //computing the transform AffineTransform af=AffineTransform.getScaleInstance(scale, scale); af.preConcatenate(AffineTransform.getTranslateInstance(-renderedRectangle.x, -renderedRectangle.y)); //computing the rendered rectangle in the base coordinates Rectangle2D.Double baseRectangle= getSVGFrame().getScaledRectangle(new Rectangle2D.Double(renderedRectangle.x, renderedRectangle.y, renderedRectangle.width, renderedRectangle.height), true); //computing the image dimensions in the base coordinates Rectangle2D.Double rect=getSVGFrame().getScaledRectangle( new Rectangle2D.Double(0, 0, canvasOffscreenImage.getWidth(), canvasOffscreenImage.getHeight()), true); Point2D.Double basedImageSize=new Point2D.Double(rect.getWidth(), rect.getHeight()); //computing the scrolling values in the base coordinates Point2D.Double baseScrollPoint=getSVGFrame().getScaledPoint(new Point2D.Double(scrollX, scrollY), true); double baseScrollX=baseScrollPoint.getX(), baseScrollY=baseScrollPoint.getY(); Graphics2D g2=canvasOffscreenImage.createGraphics(); g2.setTransform(af); //clearing the image g2.setColor(getBackground()); Rectangle2D.Double svgRectangle=null; if(baseScrollY>0){ svgRectangle=new Rectangle2D.Double(baseRectangle.x, baseRectangle.y+basedImageSize.y-baseScrollY, basedImageSize.x, baseScrollY); }else if(baseScrollY<0){ svgRectangle=new Rectangle2D.Double(baseRectangle.x, baseRectangle.y, basedImageSize.x, -baseScrollY); } if(baseScrollX>0){ svgRectangle=new Rectangle2D.Double(baseRectangle.x+basedImageSize.x-baseScrollX, baseRectangle.y, baseScrollX, basedImageSize.y); }else if(baseScrollX<0){ svgRectangle=new Rectangle2D.Double(baseRectangle.x, baseRectangle.y, -baseScrollX, basedImageSize.y); } if(svgRectangle!=null){ g2.clip(svgRectangle); //setting the rendering hints g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT); g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_DEFAULT); g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE); g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT); g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); //painting the image root.paint(g2); g2.dispose(); } }else{ //computing the transform AffineTransform af=AffineTransform.getScaleInstance(scale, scale); af.preConcatenate(AffineTransform.getTranslateInstance(-renderedRectangle.x, -renderedRectangle.y)); //root.setTransform(af); Graphics2D g2=canvasOffscreenImage.createGraphics(); g2.setTransform(af); //setting the rendering hints g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT); g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_DEFAULT); g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE); g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT); g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); if(shouldUpdateSVGContent) { //getting the list of the dirty areas java.util.List<Area> areas=null; synchronized(this) { areas=dirtyAreas; dirtyAreas=new LinkedList<Area>(); } if(areas!=null) { //computing the clip rectangle Area clip=null; for(Area area : areas) { if(area!=null) { if(clip==null) { clip=area; }else { clip.add(area); } } } if(clip!=null) { g2.setClip(clip); //painting the background g2.setColor(Color.white); g2.fill(clip); } } } //painting the image root.paint(g2); g2.dispose(); } } synchronized(this) { tmpRectangle=null; shouldRepaintSVGContent=false; shouldUpdateSVGContent=false; } } g.drawRenderedImage(canvasOffscreenImage, AffineTransform.getTranslateInstance(renderedRectangle.x, renderedRectangle.y)); g.dispose(); } /** * setting the zoom factor * @param scale the zoom factor */ public void setZoomFactor(double scale){ this.scale=scale; setCanvasPreferredSize(getScaledCanvasSize()); } /** * setting the new canvas size * @param newSize the new size */ public void setCanvasSize(Point2D.Double newSize){ CanvasGraphicsNode canvasGraphicsNode=getCanvasGraphicsNode(); if(canvasGraphicsNode.getPositionTransform()==null){ canvasGraphicsNode.setPositionTransform(new AffineTransform()); } Element root=document.getDocumentElement(); String width=SVGEditor.getFormat().format(newSize.x), height=SVGEditor.getFormat().format(newSize.y); root.setAttribute("width", width); root.setAttribute("height", height); root.setAttribute("viewBox", "0 0 "+width+" "+height); setCanvasPreferredSize(getScaledCanvasSize()); requestRepaintContent(); } /** * @return SVGScrollPane the scrollpane that contains the canvas */ public SVGScrollPane getScrollPane(){ return scrollpane; } /** * @return Returns the frame. */ public SVGFrame getSVGFrame() { return frame; } /** * @return the editor */ public SVGEditor getSVGEditor(){ return editor; } /** * @return the rendering rectangle */ public Rectangle getRenderedRectangle() { return renderedRectangle; } /** * sets the new rendered rectangle * @param rect a rendered rectangle * @param reinitialize whether the stored information about the rendered picture should be erased or not * @param forceReinitialize whether the stored information about the rendered picture should be erased or not, forcing it if necessary */ public void setRenderedRectangle(Rectangle rect, boolean reinitialize, boolean forceReinitialize){ if(forceReinitialize){ renderedRectangle=new Rectangle(0, 0, 1, 1); tmpRectangle=rect; shouldRepaintSVGContent=true; }else if(reinitialize) { if(rect!=null && ! renderedRectangle.equals(rect) && reinitialize){ renderedRectangle=new Rectangle(0, 0, 1, 1); tmpRectangle=rect; shouldRepaintSVGContent=true; } }else if(rect!=null && ! renderedRectangle.equals(rect) && renderedRectangle.width>0 && renderedRectangle.height>0){ tmpRectangle=rect; shouldRepaintSVGContent=true; } } /** * requests that the svg content should be repainted */ public void requestRepaintContent(){ synchronized(this){ shouldRepaintSVGContent=true; } } /** * requests that the svg content should be updates */ @SuppressWarnings(value="all") public void requestUpdateContent(){ if(graphicsNodeChangeAdapter!=null) { synchronized(this){ Area dArea=graphicsNodeChangeAdapter.getDirtyArea(); if(dArea!=null) { dirtyAreas.add(dArea); } shouldUpdateSVGContent=true; shouldRepaintSVGContent=true; } } } /** * @return Returns the projectFile. */ public File getProjectFile() { return projectFile; } /** * @param projectFile The projectFile to set. */ public void setProjectFile(File projectFile) { this.projectFile = projectFile; } /** * sets the current cursor * @param cursor the current cursor */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -