📄 svgimage.java
字号:
/* * Created on 26 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.io.*;import java.util.*;import java.net.*;import fr.itris.glips.svgeditor.resources.*;/** * @author Jordi SUC * the class that allows to add, select,modify the properties, delete, transform an image on the canvas */public class SVGImage extends SVGShape{ /** * the reference of an object of this class */ private final SVGImage svgImage=this; /** * the action listener used to draw the image */ private ImageActionListener imageAction=null; /** * used to convert numbers into a string */ private DecimalFormat format; /** * the constructor of the class * @param editor the editor */ public SVGImage(SVGEditor editor) { super(editor); ids.put("id","image"); ids.put("idmenuitem","Image"); //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labels.put("label", bundle.getString("shapeimagelabel")); labels.put("undoredocreate", bundle.getString("shapeimageundoredocreate")); labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate")); labels.put("undoredoresize", bundle.getString("shapeimageundoredoresize")); labels.put("undoredorotate", bundle.getString("shapeimageundoredorotate")); labels.put("helpcreate", bundle.getString("shapeimagehelpcreate")); labels.put("filefilter", bundle.getString("shapeimagefilefilter")); }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); imageAction=new ImageActionListener(); //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(imageAction); toolItem.addActionListener(imageAction); //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); } imageAction.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 image * @param frame the current SVGFrame * @param bounds the bounds of the element * @param imagePath the path of the image */ protected void drawImage(SVGFrame frame, Rectangle2D.Double bounds, String imagePath){ if(frame!=null && bounds!=null && imagePath!=null && ! imagePath.equals("")){ Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); if(getSVGEditor().getSVGSelection()!=null && doc!=null){ final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame); if(parent!=null){ // creates the image element Element image = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(), "image"); image.setAttributeNS(SVGToolkit.xmlnsXLinkNS,"xlink:href", imagePath); image.setAttributeNS(null,"x", format.format(bounds.x)); image.setAttributeNS(null,"y", format.format(bounds.y)); image.setAttributeNS(null,"width", format.format(bounds.width==0?1:bounds.width)); image.setAttributeNS(null,"height", format.format(bounds.height==0?1:bounds.height)); image.setAttributeNS(null,"preserveAspectRatio", "none meet"); //sets that the svg has been modified frame.setModified(true); //creates final variables final Document fdoc=doc; final Node fimage=image; final SVGFrame fframe=frame; //attaches the image to the svg root element parent.appendChild(fimage); //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(fimage); } public void redo(){ parent.appendChild(fimage); } }; SVGSelection selection=getSVGEditor().getSVGSelection(); if(selection!=null){ selection.deselectAll(frame, false, true); selection.addUndoRedoAction(frame, action); selection.handleNodeSelection(frame, image); 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 image will be if the user releases the mouse button * @param frame the current SVGFrame * @param g the graphics * @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 rectangle outline 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(imageAction!=null){ toolItem.removeActionListener(imageAction); toolItem.setSelected(false); toolItem.addActionListener(imageAction); imageAction.cancelActions(); } } /** * * @author Jordi SUC * the class allowing to get the position and size of the future drawn image */ protected class ImageActionListener implements ActionListener{ /** * the hashtable associating a frame to its mouse adapter */ private final Hashtable mouseAdapterFrames=new Hashtable(); /** * an instance of this class */ private final ImageActionListener action=this; /** * the cursor used when creating a rectangle */ private Cursor createCursor; /** * the source component */ private Object source=null; private boolean isActive=false; /** * the constructor of the class */ protected ImageActionListener(){ createCursor=getSVGEditor().getCursors().getCursor("image"); } /** * resets the listener */ protected void reset(){ if(isActive){ Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -