📄 svgshape.java
字号:
sqx[4]=sqx[2]; sqx[5]=sqx[1]; sqx[6]=sqx[0]; sqx[7]=sqx[0]; sqy[0]=(int)y-2*sqd; sqy[3]=(int)(y+h/2)-sqd; sqy[4]=(int)(y+h); sqy[1]=sqy[0]; sqy[2]=sqy[0]; sqy[5]=sqy[4]; sqy[6]=sqy[4]; sqy[7]=sqy[3]; //the ids of the selection squares String[] types=new String[8]; types[0]="NW"; types[1]="N"; types[2]="NE"; types[3]="E"; types[4]="SE"; types[5]="S"; types[6]="SW"; types[7]="W"; //an array of indices int i; int[] tin=null; if(w>2*sqd && h>2*sqd){ int[] tmp=new int[8]; for(i=0;i<8;i++){ tmp[i]=i; } tin=tmp; }else if((w<=2*sqd && h>2*sqd) || (w>2*sqd && h<=2*sqd) || (w<=2*sqd && h<=2*sqd)){ int[] tmp={1,3,5,7}; tin=tmp; } //draws the graphic elements Shape shape=null; GradientPaint gradient=null; for(i=0;i<tin.length;i++){ shape=getArrow(new Point2D.Double(sqx[tin[i]]+sqd, sqy[tin[i]]+sqd), types[tin[i]], REGULAR_SELECTION); if(shape!=null){ gradient=new GradientPaint(sqx[tin[i]], sqy[tin[i]], LOCK_COLOR, sqx[tin[i]]+2*sqd, sqy[tin[i]]+2*sqd, Color.white, true); g.setPaint(gradient); g.fill(shape); g.setColor(LINE_SELECTION_COLOR); g.draw(shape); } } } } return null; } /** * * @return the table of the selection squares * @param frame the current SVGFrame */ public Hashtable<Node, java.util.List<SVGSelectionSquare>> getSelectionSquares(SVGFrame frame){ return selectionSquaresTable.get(frame); } /** * 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_FILL_COLOR); g.fill(shape); } } /** * the method to translate a node * @param frame the current SVGFrame * @param list the list of the nodes to be translated * @param translationValues the values of the translation */ /*public void translateNodes(SVGFrame frame, final LinkedList<Node> list, final Point2D.Double translationValues){ if(frame!=null && list!=null && list.size()>0 && translationValues!=null){ //gets the paintlistener associated with the frame CanvasPaintListener paintListener=null; for(SVGFrame f : new LinkedList<SVGFrame>(translateFrameTable.keySet())){ try{ paintListener=translateFrameTable.get(f); }catch (Exception ex){paintListener=null;} //removes the paint listener if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, true); translateFrameTable.remove(frame); } } //for each node, sets the new matrix transform frame.enqueue(new Runnable(){ public void run() { //final Hashtable<Node, AffineTransform> transformMap=new Hashtable<Node, AffineTransform>(); SVGTransformMatrix matrix; AffineTransform af=null; for(Node node : list){ if(node!=null){ af=AffineTransform.getTranslateInstance(translationValues.x, translationValues.y); //transformMap.put(node, af); if(! af.isIdentity()){ //gets, modifies and sets the matrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node); matrix.concatenateTransform(af); getSVGEditor().getSVGToolkit().setTransformMatrix(node, matrix); } } } } }); } }*/ /** * the method to translate a node * @param frame the current SVGFrame * @param list the list of the nodes to be translated * @param translationValues the values of the translation */ public void translateNodes(SVGFrame frame, LinkedList<Node> list, Point2D.Double translationValues){ if(frame!=null && list!=null && list.size()>0 && translationValues!=null){ //gets the paintlistener associated with the frame CanvasPaintListener paintListener=translateFrameTable.get(frame); if(translationValues!=null){ Shape outline=null; Node node=null; final LinkedList<Shape> outlines=new LinkedList<Shape>(); //for each node, creates the outline for(Iterator it=list.iterator(); it.hasNext();){ try{node=(Node)it.next();}catch (Exception ex){node=null;} if(node!=null){ outline=frame.getTransformedOutline((Element)node, AffineTransform.getTranslateInstance(translationValues.x, translationValues.y)); outlines.add(outline); } //removes the paint listener if(paintListener!=null){ translateFrameTable.remove(frame); frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); } } //creates and sets the paint listener paintListener=new CanvasPaintListener(){ public void paintToBeDone(Graphics g) { Graphics2D g2=(Graphics2D)g; for(Shape outln : outlines){ if(outln!=null){ fillShape(g2, outln); } } } }; frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.DRAW_LAYER, paintListener, true); translateFrameTable.put(frame, paintListener); } } } /** * validates the translateNode method * @param frame the current SVGFrame * @param list the list of the nodes to be translated * @param translationValues the values of the translation */ public void validateTranslateNodes(SVGFrame frame, LinkedList<Node> list, Point2D.Double translationValues){ if(frame!=null && list!=null && list.size()>0 && translationValues!=null){ //gets the paintlistener associated with the frame CanvasPaintListener paintListener=null; for(SVGFrame f : new LinkedList<SVGFrame>(translateFrameTable.keySet())){ try{ paintListener=translateFrameTable.get(f); }catch (Exception ex){paintListener=null;} //removes the paint listener if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, true); translateFrameTable.remove(frame); } } final Hashtable<Node, AffineTransform> transformMap=new Hashtable<Node, AffineTransform>(); SVGTransformMatrix matrix; AffineTransform af=null; //for each node, sets the new matrix transform for(Node node : list){ if(node!=null){ af=AffineTransform.getTranslateInstance(translationValues.x, translationValues.y); transformMap.put(node, af); if(! af.isIdentity()){ //gets, modifies and sets the matrix matrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node); matrix.concatenateTransform(af); getSVGEditor().getSVGToolkit().setTransformMatrix(node, matrix); } } } //creates the undo/redo action and insert it into the undo/redo stack if(editor.getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction(labels.get("undoredotranslate")){ @Override public void undo(){ SVGTransformMatrix fmatrix=null; AffineTransform faf=null; Node node=null; for(Iterator it=transformMap.keySet().iterator(); it.hasNext();){ try{node=(Node)it.next();}catch (Exception ex){node=null;} if(node!=null){ faf=transformMap.get(node); if(faf!=null && ! faf.isIdentity()){ //gets, modifies and sets the matrix fmatrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node); try{fmatrix.concatenateTransform(faf.createInverse());}catch (Exception ex){} getSVGEditor().getSVGToolkit().setTransformMatrix(node, fmatrix); } } } //notifies that the selection has changed if(getSVGEditor().getSVGSelection()!=null){ getSVGEditor().getSVGSelection().selectionChanged(true); } } @Override public void redo(){ SVGTransformMatrix fmatrix=null; AffineTransform faf=null; Node node=null; for(Iterator it=transformMap.keySet().iterator(); it.hasNext();){ try{node=(Node)it.next();}catch (Exception ex){node=null;} if(node!=null){ faf=transformMap.get(node); if(faf!=null && ! faf.isIdentity()){ //gets, modifies and sets the matrix fmatrix=getSVGEditor().getSVGToolkit().getTransformMatrix(node); fmatrix.concatenateTransform(faf); getSVGEditor().getSVGToolkit().setTransformMatrix(node, fmatrix); } } } //notifies that the selection has changed if(getSVGEditor().getSVGSelection()!=null){ getSVGEditor().getSVGSelection().selectionChanged(true); } } }; //gets or creates the undo/redo list and adds the action into it SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(labels.get("undoredotranslate")); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); actionlist=null; /*if(getSVGEditor().getSVGSelection()!=null){ getSVGEditor().getSVGSelection().addUndoRedoAction(frame, action); }else{ }*/ } } } /** * resizes a node * @param frame the current SVGFrame * @param square the Selection square that is used by the user to resize the node * @param point1 the first point clicked * @param point2 the second point clicked */ public void resizeNode(SVGFrame frame, SVGSelectionSquare square, Point2D.Double point1, Point2D.Double point2){ if(frame!=null && square!=null && square.getNode()!=null && point1!=null && point2!=null){ //gets the paintlistener associated with the frame CanvasPaintListener paintListener=null; try{ paintListener=resizeFrameTable.get(frame); }catch (Exception ex){paintListener=null;} Node node=square.getNode(); //the outline and the bounds of the node Point2D.Double diff=new Point2D.Double(point2.x-point1.x, point2.y-point1.y); //computes the scale and translate values taking the type of the selection square into account Rectangle2D bounds=frame.getNodeGeometryBounds((Element)node); if(bounds!=null){ double sx=1.0, sy=1.0, tx=0, ty=0; if(square.getType().equals("NW")){ sx=1-diff.x/bounds.getWidth(); sy=1-diff.y/bounds.getHeight(); tx=(bounds.getX()+bounds.getWidth())*(1-sx); ty=(bounds.getY()+bounds.getHeight())*(1-sy); }else if(square.getType().equals("N")){ sy=1-diff.y/bounds.getHeight(); ty=(bounds.getY()+bounds.getHeight())*(1-sy); }else if(square.getType().equals("NE")){ sx=1+diff.x/bounds.getWidth(); sy=1-diff.y/bounds.getHeight(); tx=(bounds.getX())*(1-sx); ty=(bounds.getY()+bounds.getHeight())*(1-sy); }else if(square.getType().equals("E")){ sx=1+diff.x/bounds.getWidth(); tx=bounds.getX()*(1-sx); }else if(square.getType().equals("SE")){ sx=1+diff.x/bounds.getWidth(); sy=1+diff.y/bounds.getHeight(); tx=bounds.getX()*(1-sx); ty=bounds.getY()*(1-sy); }else if(square.getType().equals("S")){ sy=1+diff.y/bounds.getHeight(); ty=bounds.getY()*(1-sy); }else if(square.getType().equals("SW")){ sx=1-diff.x/bounds.getWidth(); sy=1+diff.y/bounds.getHeight(); tx=(bounds.getX()+bounds.getWidth())*(1-sx); ty=(bounds.getY())*(1-sy); }else if(square.getType().equals("W")){ sx=1-diff.x/bounds.getWidth(); tx=(bounds.getX()+bounds.getWidth())*(1-sx); } AffineTransform af=new AffineTransform(); //concatenates the transforms to draw the outline af.preConcatenate(AffineTransform.getScaleInstance(sx, sy)); af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty)); final Shape outline=frame.getTransformedOutline((Element)node, af); //removes the paint listener if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); } if(outline!=null){ //creates and sets the paint listener paintListener=new CanvasPaintListener(){ public void paintToBeDone(Graphics g) { Graphics2D g2=(Graphics2D)g; fillShape(g2, outline); } }; frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.DRAW_LAYER, paintListener, true); resizeFrameTable.put(frame, paintListener); } } } } /** * validates the resize transform * @param frame the current SVGFrame * @param square the Selection square that is used by the user to resize the node *@param point1 the first point clicked * @param point2 the second point clicked */ public void validateResizeNode(SVGFrame frame, SVGSelectionSquare square, Point2D.Double point1, Point2D.Double point2){ if(frame!=null && square!=null && square.getNode()!=null && point1!=null && point2!=null){ //gets the paintlistener associated with the frame CanvasPaintListener paintListener=null; SVGFrame f=null; for(Iterator it=new LinkedList<SVGFrame>(resizeFrameTable.keySet()).iterator(); it.hasNext();){ try{ f=(SVGFrame)it.next(); paintListener=resizeFrameTable.get(f); }catch (Exception ex){paintListener=null;} if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, true); resizeFrameTable.remove(frame); } } final Node node=square.getNode(); Point2D.Double diff=new Point2D.Double(point2.x-point1.x, point2.y-point1.y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -