⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svgpolyline.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Created on 10 mai 2004 *  =============================================                   GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 =============================================GLIPS Graffiti Editor, a SVG EditorCopyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRISThis library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USAContact : jordi.suc@itris.fr; philippe.gil@itris.fr ============================================= */package fr.itris.glips.svgeditor.shape;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;import fr.itris.glips.svgeditor.selection.*;import fr.itris.glips.svgeditor.undoredo.*;import org.w3c.dom.svg.*;import org.w3c.dom.*;import javax.swing.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.*;import java.text.*;import java.util.*;import org.apache.batik.bridge.*;import org.apache.batik.gvt.*;import fr.itris.glips.svgeditor.resources.*;/** * @author Jordi SUC * the class that allows to add, select,modify the properties, delete, transform a polyline on the canvas */public class SVGPolyline extends SVGShape{	/**	 * the reference of an object of this class	 */	private final SVGPolyline svgPolyline=this;		/**	 * the format	 */	private DecimalFormat format=null;		/**	 * the action listener used to draw the polyline	 */	private PolylineActionListener polylineAction=null;		/**	 * the constructor of the class	 * @param editor the editor	 */	public SVGPolyline(SVGEditor editor) {	    		super(editor);				ids.put("id","polyline");		ids.put("idmenuitem","Polyline");				//gets the labels from the resources		ResourceBundle bundle=SVGEditor.getBundle();				if(bundle!=null){		    			try{				labels.put("label", bundle.getString("shapepolylinelabel"));				labels.put("undoredocreate", bundle.getString("shapepolylineundoredocreate"));				labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate"));				labels.put("undoredoresize", bundle.getString("shapepolylineundoredoresize"));				labels.put("undoredomodifypoint", bundle.getString("shapepolylineundoredomodifypoint"));				labels.put("undoredorotate", bundle.getString("shapepolylineundoredorotate"));				labels.put("helpcreate", bundle.getString("shapepolylinehelpcreate"));			}catch (Exception ex){}		}				DecimalFormatSymbols symbols=new DecimalFormatSymbols();		symbols.setDecimalSeparator('.');		format=new DecimalFormat("######.#",symbols);				//the icons		icon=SVGResource.getIcon((String)ids.get("idmenuitem"), false);		disabledIcon=SVGResource.getIcon((String)ids.get("idmenuitem"), true);				//the menu item		menuitem=new JMenuItem((String)labels.get("label"), icon);		menuitem.setDisabledIcon(disabledIcon);		menuitem.setEnabled(false);		polylineAction=new PolylineActionListener();				//the toggle button		toolItem=new JToggleButton(disabledIcon);		toolItem.setEnabled(false);		toolItem.setToolTipText((String)labels.get("label"));				//adds a listener to the menu item and the toggle button		menuitem.addActionListener(polylineAction);		toolItem.addActionListener(polylineAction);				//a listener that listens to the changes of the SVGFrames		final ActionListener svgframeListener=new ActionListener(){			public void actionPerformed(ActionEvent e) {								if(getSVGEditor().getFrameManager().getFrameNumber()>0){				    					menuitem.setEnabled(true);					//menuitem.setIcon(icon);					toolItem.setEnabled(true);					toolItem.setIcon(icon);				}else{				    					menuitem.setEnabled(false);					//menuitem.setIcon(disabledIcon);					toolItem.setEnabled(false);					toolItem.setIcon(disabledIcon);				}								polylineAction.reset();			}		};				//adds the SVGFrame change listener		editor.getFrameManager().addSVGFrameChangedListener(svgframeListener);	}		/**	 * @return a map associating a menu item id to its menu item object	 */	public Hashtable getMenuItems(){				Hashtable menuItems=new Hashtable();		menuItems.put((String)ids.get("idmenuitem"), menuitem);				return menuItems;	}		/**	 * @return a map associating a tool item id to its tool item object	 */	public Hashtable getToolItems(){				Hashtable toolItems=new Hashtable();		toolItems.put((String)ids.get("idmenuitem"), toolItem);				return toolItems;	}	/**	 * draws a polyline	 * @param frame the current SVGFrame	 * @param points the array of points	 */	protected void drawPolyline(SVGFrame frame, Point2D.Double[] points){		if(frame!=null && points!=null && points.length>1){						Document doc=frame.getScrollPane().getSVGCanvas().getDocument();						if(getSVGEditor().getSVGSelection()!=null && doc!=null){			    				final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame);								if(parent!=null){					String value="";					for(int i=0;i<points.length;i++){					    						value=value.concat(format.format(points[i].getX())+","+format.format(points[i].getY())+" ");					}								//creates the polyline					final Element polyline=doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"polyline");					polyline.setAttributeNS(null,"points",value);					String colorString=getSVGEditor().getColorChooser().getColorString(getSVGEditor().getSVGColorManager().getCurrentColor());					polyline.setAttributeNS(null, "style", "stroke:".concat(colorString.concat(";fill:none;")));								//sets that the svg has been modified					frame.setModified(true);								//creates final variables					final SVGFrame fframe=frame;					// attaches the element to the svg parent element						parent.appendChild(polyline);								//create the undo/redo action and insert it into the undo/redo stack					if(getSVGEditor().getUndoRedo()!=null){						SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredocreate")){							public void undo(){							    							    parent.removeChild(polyline);							}														public void redo(){							    							    parent.appendChild(polyline);							}						};										SVGSelection selection=getSVGEditor().getSVGSelection();										if(selection!=null){						    							selection.deselectAll(frame, false, true);							selection.addUndoRedoAction(frame, action);							selection.handleNodeSelection(frame, polyline);							selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredocreate")){});							selection.refreshSelection(frame);											}else{						    							SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList((String)labels.get("undoredocreate"));							actionlist.add(action);							getSVGEditor().getUndoRedo().addActionList(frame, actionlist);						}					}				}			}		}	}		/**	 * draws what the polyline will be if the user double clicks	 * @param frame the current SVGFrame	 * @param g the graphics element	 * @param points an array of points	 */	protected void drawGhost(SVGFrame frame, Graphics2D g, Point2D.Double[] points){				if(frame!=null && points!=null && points.length>1 && g!=null){		    		    g=(Graphics2D)g.create();		    			//draws the new awt polyline to be displayed					g.setColor(GHOST_COLOR);			g.setXORMode(Color.white);			for(int i=1;i<points.length;i++){			    				g.drawLine((int)points[i-1].x, (int)points[i-1].y, (int)points[i].x, (int)points[i].y);			}						g.dispose();		}			}		/**	 * gets the nexts level after the given selection level	 * @param type a selection level	 * @return the next selection level	 */	public String getNextLevel(String type){	    		if(type!=null){		    			if(type.equals("level1")){			    			    return "level2";			    			}else if(type.equals("level2")){			    			    return "level3";			    			}else if(type.equals("level3")){			    			    return "level1";			}		}				return "level1";	}		/**	 * draws the selection for the polyline	 * @param frame the current SVGFrame	 * @param graphics the graphics 	 * @param nde the node to be selected	 * @return the list of the selection squares	 */	protected LinkedList drawModifyPointsSelection(SVGFrame frame, Graphics graphics, Node nde){		LinkedList squarelist=new LinkedList();		Element node=(Element)nde;		Graphics2D g=(Graphics2D)graphics;				if(frame!=null && g!=null && node!=null){						int sqd=5;			String pts=node.getAttributeNS(null,"points");						if(pts!=null && !pts.equals("")){			    				double px=0, py=0;				//gets the coordinates of the points				Point2D.Double[] points=null;				ArrayList list=new ArrayList();				int i=0;								pts=pts.replaceAll("[,]"," ");								//removes the first space characters from the string				if(!pts.equals("") && pts.charAt(0)==' '){				    				    pts=pts.replaceFirst("[\\s]+","");				}				 				while(! pts.equals("")){				    					try{						px=new Double(pts.substring(0,pts.indexOf(' '))).doubleValue();						pts=pts.substring(pts.indexOf(' ')+1,pts.length());												if(!pts.equals("") && pts.charAt(0)==' '){						    						    pts=pts.replaceFirst("[\\s]+","");						}												py=new Double(pts.substring(0,pts.indexOf(' '))).doubleValue();						pts=pts.substring(pts.indexOf(' ')+1,pts.length());												if(!pts.equals("") && pts.charAt(0)==' '){						    						    pts=pts.replaceFirst("[\\s]+","");						}												list.add(new Point2D.Double(px, py));					}catch (Exception ex){break;}				}								//the values of the coordinates of the points				points=new Point2D.Double[list.size()];								for(i=0;i<list.size();i++){				    				    points[i]=(Point2D.Double)list.get(i);				}								if(points!=null && points.length>0){				    					//computes the transformed points					BridgeContext ctxt=frame.getScrollPane().getSVGCanvas().getBridgeContext();										if(ctxt!=null){					    						GraphicsNode gnode=ctxt.getGraphicsNode((Element)node);												if(gnode!=null){						    							AffineTransform af=new AffineTransform();							try{af.preConcatenate(gnode.getTransform());}catch (Exception ex){}							try{af.transform(points,0,points,0,points.length);}catch (Exception e){}						}					}					

⌨️ 快捷键说明

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