📄 svgrectangle.java
字号:
/* * Created on 2 avr. 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 fr.itris.glips.svgeditor.resources.*;/** * @author Jordi SUC * the class that allows to add, select,modify the properties, delete, transform a rectangle on the canvas */public class SVGRectangle extends SVGShape{ /** * the reference of an object of this class */ private final SVGRectangle svgRect=this; /** * used to convert numbers into a string */ private DecimalFormat format; /** * the action listener used to draw the rectangle */ private RectActionListener rectAction; /** * the constructor of the class * @param editor the editor */ public SVGRectangle(SVGEditor editor) { super(editor); ids.put("id","rect"); ids.put("idmenuitem","Rectangle"); //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labels.put("label", bundle.getString("shaperectanglelabel")); labels.put("undoredocreate", bundle.getString("shaperectangleundoredocreate")); labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate")); labels.put("undoredoresize", bundle.getString("shaperectangleundoredoresize")); labels.put("undoredorotate", bundle.getString("shaperectangleundoredorotate")); labels.put("helpcreate", bundle.getString("shaperectanglehelpcreate")); }catch (Exception ex){} } DecimalFormatSymbols symbols=new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); format=new DecimalFormat("######.#",symbols); //the icons icon=SVGResource.getIcon(ids.get("idmenuitem"), false); disabledIcon=SVGResource.getIcon(ids.get("idmenuitem"), true); //the menu item menuitem=new JMenuItem(labels.get("label"), icon); menuitem.setDisabledIcon(disabledIcon); menuitem.setEnabled(false); rectAction=new RectActionListener(); //the toggle button toolItem=new JToggleButton(disabledIcon); toolItem.setEnabled(false); toolItem.setToolTipText(labels.get("label")); //adds a listener to the menu item and the toggle button menuitem.addActionListener(rectAction); toolItem.addActionListener(rectAction); //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); rectAction.reset(); }else{ menuitem.setEnabled(false); toolItem.setEnabled(false); toolItem.setIcon(disabledIcon); } } }; //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(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(ids.get("idmenuitem"), toolItem); return toolItems; } /** * draws a rectangle * @param frame the current SVGFrame * @param bounds the bounds of the element */ protected void drawRectangle(SVGFrame frame, Rectangle2D.Double bounds){ if(frame!=null && bounds!=null){ Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); //normalizing the bounds of the element if(bounds.width<=0){ bounds.width=1; } if(bounds.height<=0){ bounds.height=1; } if(getSVGEditor().getSVGSelection()!=null && doc!=null){ final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame); if(parent!=null){ // creates the rectangle final Element rectangle=doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"rect"); rectangle.setAttributeNS(null,"x", format.format(bounds.x)); rectangle.setAttributeNS(null,"y", format.format(bounds.y)); rectangle.setAttributeNS(null,"width", format.format(bounds.width==0?1:bounds.width)); rectangle.setAttributeNS(null,"height", format.format(bounds.height==0?1:bounds.height)); String colorString=SVGEditor.getColorChooser(). getColorString(SVGColorManager.getCurrentColor()); rectangle.setAttributeNS(null, "style", "fill:".concat(colorString.concat(";"))); //sets that the svg has been modified frame.setModified(true); //attaches the element to the svg parent element parent.appendChild(rectangle); //create the undo/redo action and insert it into the undo/redo stack if(editor.getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredocreate")){ public void undo(){ parent.removeChild(rectangle); } public void redo(){ parent.appendChild(rectangle); } }; SVGSelection selection=editor.getSVGSelection(); if(selection!=null){ selection.deselectAll(frame, false, true); selection.addUndoRedoAction(frame, action); selection.handleNodeSelection(frame, rectangle); selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredocreate")){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList((String)labels.get("undoredocreate")); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); } } } } } } /** * draws what the rectangle 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(g!=null && frame!=null && bounds!=null){ g=(Graphics2D)g.create(); //draws the new awt rectangle to be displayed g.setColor(GHOST_COLOR); g.setXORMode(Color.white); g.drawRect((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 a rectangle when the user clicks on the menu item */ public void cancelActions(){ if(rectAction!=null){ toolItem.removeActionListener(rectAction); toolItem.setSelected(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -