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

📄 svgellipse.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.apache.batik.util.*;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 fr.itris.glips.svgeditor.resources.*;/** * @author suc * * @author Jordi SUC * the class that allows to add, select,modify the properties, delete, transform a ellipse on the canvas */public class SVGEllipse extends SVGShape{	/**	 * the reference of an object of this class	 */	private final SVGEllipse svgEllipse=this;		/**	 * the action listener used to draw the ellipse	 */	private EllipseActionListener ellipseAction=null;		/**	 * used to convert numbers into a string	 */	private DecimalFormat format;		/**	 * the constructor of the class	 * @param editor the editor	 */	public SVGEllipse(SVGEditor editor) {		super(editor);				ids.put("id","ellipse");		ids.put("idmenuitem","Ellipse");				//gets the labels from the resources		ResourceBundle bundle=SVGEditor.getBundle();				if(bundle!=null){		    			try{				labels.put("label", bundle.getString("shapeellipselabel"));				labels.put("undoredocreate", bundle.getString("shapeellipseundoredocreate"));				labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate"));				labels.put("undoredoresize", bundle.getString("shapeellipseundoredoresize"));				labels.put("undoredorotate", bundle.getString("shapeellipseundoredorotate"));				labels.put("helpcreate", bundle.getString("shapeellipsehelpcreate"));			}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);		ellipseAction=new EllipseActionListener();				//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(ellipseAction);		toolItem.addActionListener(ellipseAction);		//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);					toolItem.setEnabled(true);					toolItem.setIcon(icon);									}else{				    					menuitem.setEnabled(false);					toolItem.setEnabled(false);					toolItem.setIcon(disabledIcon);				}								ellipseAction.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 an ellipse	 * @param frame the current SVGFrame	 * @param bounds the bounds of the element	 */	protected void drawEllipse(SVGFrame frame, Rectangle2D.Double bounds){				if(frame!=null && bounds!=null){						Document doc=frame.getScrollPane().getSVGCanvas().getDocument();						if(getSVGEditor().getSVGSelection()!=null && doc!=null){			    				final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame);								if(parent!=null){								//creates the ellipse					Element ellipse = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"ellipse");					ellipse.setAttributeNS(null,"cx", format.format(bounds.x+bounds.width/2));					ellipse.setAttributeNS(null,"cy", format.format(bounds.y+bounds.height/2));					ellipse.setAttributeNS(null,"rx", format.format(bounds.width/2));					ellipse.setAttributeNS(null,"ry", format.format(bounds.height/2));					String colorString=SVGEditor.getColorChooser().getColorString(SVGColorManager.getCurrentColor());					ellipse.setAttributeNS(null, "style", "fill:".concat(colorString.concat(";")));								//sets that the svg has been modified					frame.setModified(true);								//creates final variables					final Node fellipse=ellipse;					//attaches the ellipse to the svg root element					parent.appendChild(fellipse);								//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(fellipse);							}							public void redo(){							    							    parent.appendChild(fellipse);							}						};										SVGSelection selection=getSVGEditor().getSVGSelection();										if(selection!=null){						    							selection.deselectAll(frame, false, true);							selection.addUndoRedoAction(frame, action);							selection.handleNodeSelection(frame, ellipse);							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 ellipse will be if the user releases the mouse button	 * @param frame the current SVGFrame	 * @param g a graphics element	 * @param bounds the bounds of the element	 */	protected void drawGhost(SVGFrame frame, Graphics2D g, Rectangle2D.Double bounds){				if(frame!=null && g!=null && bounds!=null){					    g=(Graphics2D)g.create();		    			//draws the new awt ellipse to be displayed			g.setColor(GHOST_COLOR);			g.setXORMode(Color.white);			g.drawOval((int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height);			Rectangle2D.Double scaleBounds=frame.getScaledRectangle(bounds, true);			frame.getStateBar().setSVGW(SVGEditor.getDisplayFormat().format(scaleBounds.width));			frame.getStateBar().setSVGH(SVGEditor.getDisplayFormat().format(scaleBounds.height));			g.dispose();		}			}		/**	 * used to remove the listener added to draw an ellipse when the user clicks on the menu item	 */	public void cancelActions(){	    		if(ellipseAction!=null){		    			toolItem.removeActionListener(ellipseAction);			toolItem.setSelected(false);			toolItem.addActionListener(ellipseAction);					    ellipseAction.cancelActions();		}	}		/**	 * 	 * @author Jordi SUC	 * the class allowing to get the position and size of the future drawn ellipse 	 */	protected class EllipseActionListener implements ActionListener{		/**		 * the hashtable associating a frame to its mouse adapter		 */		private final Hashtable mouseAdapterFrames=new Hashtable();				/**		 * an instance of this class		 */

⌨️ 快捷键说明

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