svgcanvas.java

来自「完全基于java开发的svg矢量绘图工具」· Java 代码 · 共 1,742 行 · 第 1/3 页

JAVA
1,742
字号
  }  /**   * sets the current cursor   * @param cursor the current cursor   */  public void setSVGCursor(Cursor cursor) {    if (cursor != null && !cursor.equals(getCursor()) &&        !waitCursor.equals(getCursor())) {      lastCursor = getCursor();      setCursor(cursor);    }  }  /**   * returns to the last cursor   */  public void returnToLastCursor() {    if (enableWaitCursor) {      if (lastCursor == null ||          (lastCursor != null && lastCursor.equals(waitCursor))) {        setCursor(defaultCursor);      }      else if (lastCursor != null) {        setCursor(lastCursor);      }    }  }  /**   * displays the wait cursor   */  public void displayWaitCursor() {    if (enableWaitCursor && waitCursor != null && !waitCursor.equals(getCursor())) {      lastCursor = getCursor();      setCursor(waitCursor);    }  }  /**   * enables or disables the display of the wait cursor   * @param enable true to enable the display of the wait cursor   */  public void setEnableWaitCursor(boolean enable) {    enableWaitCursor = enable;  }  /**   * @return the canvas' size   */  public Point2D.Double getGeometryCanvasSize() {    //gets the root element    if (document != null) {      Element root = document.getDocumentElement();      if (root != null) {        double w = SVGToolkit.getPixelledNumber(root.getAttributeNS(null,            "width"));        double h = SVGToolkit.getPixelledNumber(root.getAttributeNS(null,            "height"));        return new Point2D.Double(w, h);      }    }    return new Point2D.Double(0, 0);  }  /**   * @param root the root element   * @return the scaled canvas' size   */  public Dimension getScaledCanvasSize(Element root) {    Dimension scaledSize = new Dimension(0, 0);    if (root != null) {      double w = 0, h = 0;      try {        w = SVGToolkit.getPixelledNumber(root.getAttributeNS(null, "width")) *            scale;        h = SVGToolkit.getPixelledNumber(root.getAttributeNS(null, "height")) *            scale;        scaledSize.width = (int) w;        scaledSize.height = (int) h;      }      catch (Exception ex) {}    }    return scaledSize;  }  /**   * @return the scaled canvas' size   */  public Dimension getScaledCanvasSize() {    Dimension scaledSize = new Dimension(0, 0);    //gets the root element    if (document != null) {      Element root = document.getDocumentElement();      if (root != null) {        double w = 0, h = 0;        try {          w = SVGToolkit.getPixelledNumber(root.getAttributeNS(null, "width")) *              scale;          h = SVGToolkit.getPixelledNumber(root.getAttributeNS(null, "height")) *              scale;          scaledSize.width = (int) w;          scaledSize.height = (int) h;        }        catch (Exception ex) {}      }    }    return scaledSize;  }  /**   * @return Returns the repaintEnabled.   */  public boolean isRepaintEnabled() {    return repaintEnabled;  }  /**   * @param repaintEnabled The repaintEnabled to set.   */  public synchronized void setRepaintEnabled(boolean repaintEnabled) {    this.repaintEnabled = repaintEnabled;    if (repaintEnabled) {      shouldRepaint = true;    }  }  /**   * adds a grid paint listener   * @param type the integer representing the layer at which the painting should be done   * @param l the grid paint listener to be added   * @param makeRepaint the boolean telling to make a repaint after the paint listener was added or not   */  public void addLayerPaintListener(int type, CanvasPaintListener l,                                    boolean makeRepaint) {    if (l != null) {      Set<CanvasPaintListener> set = paintListeners.get(type);      if (set != null) {        set.add(l);      }      if (isRepaintEnabled() && makeRepaint) {        synchronized (this) {          shouldRepaint = true;        }      }    }  }  /**   * removes a paint listener   * @param l the paint listener to be removed   * @param makeRepaint the boolean telling to make a repaint after the paint listener was removed or not   */  public void removePaintListener(CanvasPaintListener l, boolean makeRepaint) {    paintListeners.get(GRID_LAYER).remove(l);    paintListeners.get(BOTTOM_LAYER).remove(l);    paintListeners.get(SELECTION_LAYER).remove(l);    paintListeners.get(DRAW_LAYER).remove(l);    paintListeners.get(TOP_LAYER).remove(l);    if (isRepaintEnabled() && makeRepaint) {      synchronized (this) {        shouldRepaint = true;      }    }  }  /**   * notifies the paint listeners when a paint action is done   *@param g2 the graphics   */  protected void drawPainters(Graphics2D g2) {    if (isRepaintEnabled()) {      //setting the rendering hints      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                          RenderingHints.VALUE_ANTIALIAS_OFF);      g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,                          RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);      g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,                          RenderingHints.VALUE_COLOR_RENDER_SPEED);      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_SPEED);      g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,                          RenderingHints.VALUE_STROKE_NORMALIZE);      for (CanvasPaintListener listener :           new HashSet<CanvasPaintListener> (paintListeners.get(GRID_LAYER))) {        listener.paintToBeDone(g2);      }      for (CanvasPaintListener listener :           new HashSet<CanvasPaintListener> (paintListeners.get(BOTTOM_LAYER))) {        listener.paintToBeDone(g2);      }      for (CanvasPaintListener listener :           new HashSet<CanvasPaintListener> (paintListeners.get(SELECTION_LAYER))) {        listener.paintToBeDone(g2);      }      for (CanvasPaintListener listener :           new HashSet<CanvasPaintListener> (paintListeners.get(DRAW_LAYER))) {        listener.paintToBeDone(g2);      }      for (CanvasPaintListener listener :           new HashSet<CanvasPaintListener> (paintListeners.get(TOP_LAYER))) {        listener.paintToBeDone(g2);      }    }  }  /**   * removes all paint listeners   */  public void removeAllPaintListeners() {    paintListeners.get(GRID_LAYER).clear();    paintListeners.get(BOTTOM_LAYER).clear();    paintListeners.get(SELECTION_LAYER).clear();    paintListeners.get(DRAW_LAYER).clear();    paintListeners.get(TOP_LAYER).clear();  }  /**   * asks the repaint manager to repaint the canvas   */  public synchronized void delayedRepaint() {    if (isRepaintEnabled()) {      shouldRepaint = true;    }  }  /**   * Builds the InputMap of this canvas with a set of predefined   * Actions.   */  protected void installKeyboardActions() {  }  /**   * gets the canvas' current scale   * @return the canvas' current scale   */  public double getScale() {    return scale;  }  /**   * @return the gvt root   */  public GraphicsNode getGraphicsNode() {    return getCanvasGraphicsNode();  }  /**   * @return the canvas graphics node for this canvas   */  private CanvasGraphicsNode getCanvasGraphicsNode() {    java.util.List children = gvtRoot.getChildren();    if (children.size() == 0) {      return null;    }    GraphicsNode gn = (GraphicsNode) children.get(0);    if (! (gn instanceof CanvasGraphicsNode)) {      return null;    }    return (CanvasGraphicsNode) gn;  }  /**   * the class of the thread handling the painting of the svg canvas   * @author Jordi SUC   */  protected class CanvasRepaintManager      extends Thread {    /**     * the current paint runnable     */    protected PaintRunnable paintRunnable = null;    @Override    public void run() {      while (!isDisposing) {        try {          sleep(25);        }        catch (Exception ex) {}        if ( (shouldRepaint || shouldRepaintSVGContent) &&            (paintRunnable == null ||             (paintRunnable != null && paintRunnable.isExecuted()))) {          final boolean fshouldRepaint = shouldRepaint,              fshouldRepaintSVGContent = shouldRepaintSVGContent;          paintRunnable = new PaintRunnable() {            @Override            public void run() {              if (fshouldRepaintSVGContent) {                canvas.repaint();              }              else if (fshouldRepaint) {                paintersPanel.repaint();              }              super.run();            }          };          SwingUtilities.invokeLater(paintRunnable);          synchronized (SVGCanvas.this) {            shouldRepaint = false;          }        }      }    }    /**     * the class of the runnables used to paint     * @author Jordi SUC     */    protected abstract class PaintRunnable        implements Runnable {      /**       * whether this boolean has been executed       */      protected boolean executed = false;      /**       * the run method       */      public void run() {        executed = true;      }      /**       * @return Returns the executed.       */      public boolean isExecuted() {        return executed;      }    }  }  /**   * the extended class of a JSVGCanvas that will be used to provide useful information   * @author Jordi SUC   */  protected class ExtendedJSVGCanvas      extends JSVGCanvas {    /**     * @return the bridge context     */    public BridgeContext getBridgeContext() {      return bridgeContext;    }  }  /**   * the class of the listener to the changes on the graphic nodes   * @author Jordi SUC   */  protected class CanvasGraphicsNodeChangeListener      implements GraphicsNodeChangeListener {    /**     * the map of the initial areas     */    private Map<GraphicsNode, Area> initialAreas = new HashMap<GraphicsNode,        Area> ();    /**     * the map associating a graphics node to a rectangle     */    private Map<GraphicsNode, Area> changedAreas = new HashMap<GraphicsNode,        Area> ();    /**     * @see org.apache.batik.gvt.event.GraphicsNodeChangeListener#changeStarted(org.apache.batik.gvt.event.GraphicsNodeChangeEvent)     */    public void changeStarted(GraphicsNodeChangeEvent gnce) {      gnce.consume();      GraphicsNode gnode = gnce.getChangeSrc();      if (gnode == null) {        gnode = gnce.getGraphicsNode();      }      if (! (gnode instanceof CanvasGraphicsNode) &&          !initialAreas.containsKey(gnode)) {        Rectangle2D rect = gnode.getBounds();        if (rect == null) {          rect = gnode.getGeometryBounds();        }        if (gnode.getTransform() != null) {          rect = gnode.getTransform().createTransformedShape(rect).getBounds2D();        }        if (rect != null) {          rect.setRect(rect.getX() - 2, rect.getY() - 2, rect.getWidth() + 4,                       rect.getHeight() + 4);          initialAreas.put(gnode, new Area(rect));        }      }    }    /**     * @see org.apache.batik.gvt.event.GraphicsNodeChangeListener#changeCompleted(org.apache.batik.gvt.event.GraphicsNodeChangeEvent)     */    public void changeCompleted(GraphicsNodeChangeEvent gnce) {      gnce.consume();      GraphicsNode gnode = gnce.getChangeSrc();      if (gnode == null) {        gnode = gnce.getGraphicsNode();      }      if (! (gnode instanceof CanvasGraphicsNode) &&          !changedAreas.containsKey(gnode) && initialAreas.containsKey(gnode)) {        //getting the old rectangle        Area oldArea = initialAreas.get(gnode);        Rectangle2D rect = gnode.getBounds();        if (rect == null) {          rect = gnode.getGeometryBounds();        }        if (gnode.getTransform() != null) {          rect = gnode.getTransform().createTransformedShape(rect).getBounds2D();        }        rect.setRect(rect.getX() - 2, rect.getY() - 2, rect.getWidth() + 4,                     rect.getHeight() + 4);        if (oldArea != null) {          oldArea.add(new Area(rect));        }        else {          oldArea = new Area(rect);        }        if (oldArea != null) {          changedAreas.put(gnode, oldArea);        }      }    }    /**     * @return the dirty area     */    public Area getDirtyArea() {      Area dirtyArea = null;      Area initialArea = null, changedArea = null, currentArea = null;      //computing the dirty area      for (GraphicsNode gnode : initialAreas.keySet()) {        initialArea = initialAreas.get(gnode);        changedArea = changedAreas.get(gnode);        currentArea = (changedArea == null) ? initialArea : changedArea;        if (dirtyArea == null) {          dirtyArea = currentArea;        }        else {          dirtyArea.add(currentArea);        }      }      //clearing the maps      initialAreas.clear();      changedAreas.clear();      return dirtyArea;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?