📄 svgpath.java
字号:
/* * Created on 11 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.menutool.*;import fr.itris.glips.svgeditor.selection.*;import fr.itris.glips.svgeditor.undoredo.*;import org.apache.batik.ext.awt.geom.*;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 path on the canvas */public class SVGPath extends SVGShape{ /** * the reference of an object of this class */ private final SVGPath svgPath=this; /** * the action listener used to draw the path */ private PathActionListener pathActionQuadratic=null, pathActionCubic=null; /** * the menu items */ private JMenuItem quadratic, cubic, union, subtraction, intersection, convert; /** * the listeners to the menu items */ private ActionListener quadraticListener, cubicListener, unionListener, subtractionListener, intersectionListener, convertListener; /** * the tool items */ private JToggleButton quadraticTool, cubicTool; /** * the icons */ private ImageIcon quadraticIcon, quadraticDisabledIcon, cubicIcon, cubicDisabledIcon, convertIcon, convertDisabledIcon, unionIcon, unionDisabledIcon, subtractionIcon, subtractionDisabledIcon, intersectionIcon, intersectionDisabledIcon; /** * the nodes that are currently selected */ private LinkedList selectedNodes=new LinkedList(); /** * the format */ private DecimalFormat format=null; /** * the constructor of the class * @param editor the editor */ public SVGPath(SVGEditor editor) { super(editor); ids.put("id","path"); ids.put("idmenupathoperations","PathOperations"); ids.put("idconvert","ConvertToPath"); ids.put("idunion","PathUnion"); ids.put("idsubtraction","PathSubtraction"); ids.put("idintersection","PathIntersection"); ids.put("idquadratic","QuadraticBezier"); ids.put("idcubic","CubicBezier"); //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labels.put("label", bundle.getString("shapepathlabel")); labels.put("labelpathoperations", bundle.getString("shapepathlabeloperations")); labels.put("labelquadratic", bundle.getString("shapepathlabelquadratic")); labels.put("labelcubic", bundle.getString("shapepathlabelcubic")); labels.put("labelunion", bundle.getString("shapepathlabelunion")); labels.put("labelsubtraction", bundle.getString("shapepathlabelsubtraction")); labels.put("labelintersection", bundle.getString("shapepathlabelintersection")); labels.put("labelconvert", bundle.getString("shapepathlabelconvert")); labels.put("undoredocreatequadratic", bundle.getString("shapepathundoredocreatequadratic")); labels.put("undoredocreatecubic", bundle.getString("shapepathundoredocreatecubic")); labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate")); labels.put("undoredoresize", bundle.getString("shapepathundoredoresize")); labels.put("undoredomodifypoint", bundle.getString("shapepathundoredomodifypoint")); labels.put("undoredorotate", bundle.getString("shapepathundoredorotate")); labels.put("undoredounion", bundle.getString("shapepathundoredounion")); labels.put("undoredosubtraction", bundle.getString("shapepathundoredosubtraction")); labels.put("undoredointersection", bundle.getString("shapepathundoredointersection")); labels.put("undoredoconvert", bundle.getString("shapepathundoredoconvert")); labels.put("helpcreatequadratic", bundle.getString("shapepathhelpcreatequadratic")); labels.put("helpcreatecubic", bundle.getString("shapepathhelpcreatecubic")); }catch (Exception ex){} } //the object used to convert double values into strings DecimalFormatSymbols symbols=new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); format=new DecimalFormat("######.#",symbols); //the icons quadraticIcon=SVGResource.getIcon(ids.get("idquadratic"), false); quadraticDisabledIcon=SVGResource.getIcon(ids.get("idquadratic"), true); cubicIcon=SVGResource.getIcon(ids.get("idcubic"), false); cubicDisabledIcon=SVGResource.getIcon(ids.get("idcubic"), true); convertIcon=SVGResource.getIcon(ids.get("idconvert"), false); convertDisabledIcon=SVGResource.getIcon(ids.get("idconvert"), true); unionIcon=SVGResource.getIcon(ids.get("idunion"), false); unionDisabledIcon=SVGResource.getIcon(ids.get("idunion"), true); subtractionIcon=SVGResource.getIcon(ids.get("idsubtraction"), false); subtractionDisabledIcon=SVGResource.getIcon(ids.get("idsubtraction"), true); intersectionIcon=SVGResource.getIcon(ids.get("idintersection"), false); intersectionDisabledIcon=SVGResource.getIcon(ids.get("idintersection"), true); //the menu items quadratic=new JMenuItem((String)labels.get("labelquadratic"), quadraticIcon); quadratic.setDisabledIcon(quadraticDisabledIcon); quadratic.setEnabled(false); cubic=new JMenuItem((String)labels.get("labelcubic"), cubicIcon); cubic.setDisabledIcon(cubicDisabledIcon); cubic.setEnabled(false); //the toggle buttons quadraticTool=new JToggleButton(quadraticDisabledIcon); quadraticTool.setEnabled(false); quadraticTool.setToolTipText((String)labels.get("labelquadratic")); cubicTool=new JToggleButton(cubicDisabledIcon); cubicTool.setEnabled(false); cubicTool.setToolTipText((String)labels.get("labelcubic")); //the listeners pathActionQuadratic=new PathActionListener(PathActionListener.QUADRATIC_BEZIER); pathActionCubic=new PathActionListener(PathActionListener.CUBIC_BEZIER); quadratic.addActionListener(pathActionQuadratic); cubic.addActionListener(pathActionCubic); quadraticTool.addActionListener(pathActionQuadratic); cubicTool.addActionListener(pathActionCubic); final SVGEditor feditor=editor; //a listener that listens to the changes of the SVGFrames final ActionListener svgframeListener=new ActionListener(){ /** * a listener on the selection changes */ private ActionListener selectionListener=null; /** * the current selection module */ private SVGSelection selection=null; public void actionPerformed(ActionEvent e) { //clears the list of the selected items selectedNodes.clear(); if(getSVGEditor().getFrameManager().getFrameNumber()>0){ convert.setEnabled(true); quadratic.setEnabled(true); quadraticTool.setEnabled(true); quadraticTool.setIcon(quadraticIcon); cubic.setEnabled(true); cubicTool.setEnabled(true); cubicTool.setIcon(cubicIcon); }else{ convert.setEnabled(false); quadratic.setEnabled(false); quadraticTool.setEnabled(false); quadraticTool.setIcon(quadraticDisabledIcon); cubic.setEnabled(false); cubicTool.setEnabled(false); cubicTool.setIcon(cubicDisabledIcon); } pathActionQuadratic.reset(); pathActionCubic.reset(); final SVGFrame frame=feditor.getFrameManager().getCurrentFrame(); //if a selection listener is already registered on a selection module, it is removed if(selection!=null && selectionListener!=null){ selection.removeSelectionListener(selectionListener); } //gets the current selection module if(frame!=null){ selection=feditor.getSVGSelection(); } if(frame!=null && selection!=null){ manageSelection(); //the listener of the selection changes selectionListener=new ActionListener(){ public void actionPerformed(ActionEvent e) { manageSelection(); } }; //adds the selection listener if(selectionListener!=null){ selection.addSelectionListener(selectionListener); } } } /** * updates the selected items and the state of the menu items */ protected void manageSelection(){ //disables the menuitems convert.setEnabled(false); union.setEnabled(false); subtraction.setEnabled(false); intersection.setEnabled(false); LinkedList list=null; //gets the currently selected nodes list if(selection!=null){ list=selection.getCurrentSelection(feditor.getFrameManager().getCurrentFrame()); } selectedNodes.clear(); //refresh the selected nodes list if(list!=null){ selectedNodes.addAll(list); } if(selectedNodes.size()>0){ convert.setEnabled(true); if(selectedNodes.size()>1){ union.setEnabled(true); intersection.setEnabled(true); if(selectedNodes.size()==2){ subtraction.setEnabled(true); } } } } }; //adds the SVGFrame change listener editor.getFrameManager().addSVGFrameChangedListener(svgframeListener); //the menuitems convert=new JMenuItem((String)labels.get("labelconvert"), convertIcon); convert.setDisabledIcon(convertDisabledIcon); convert.setEnabled(false); convertListener=new ActionListener(){ public void actionPerformed(ActionEvent e){ getSVGEditor().cancelActions(true); if(selectedNodes.size()>0){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); Runnable runnable=new Runnable(){ public void run() { convertToPath(getSVGEditor().getFrameManager().getCurrentFrame(), selectedNodes); } }; frame.enqueue(runnable); } } }; convert.addActionListener(convertListener); union=new JMenuItem((String)labels.get("labelunion"), unionIcon); union.setDisabledIcon(unionDisabledIcon); union.setEnabled(false); unionListener=new ActionListener(){ public void actionPerformed(ActionEvent e){ getSVGEditor().cancelActions(true); if(selectedNodes.size()>1){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); Runnable runnable=new Runnable(){ public void run() { union(getSVGEditor().getFrameManager().getCurrentFrame(), selectedNodes); } }; frame.enqueue(runnable); } } }; union.addActionListener(unionListener); subtraction=new JMenuItem((String)labels.get("labelsubtraction"), subtractionIcon); subtraction.setDisabledIcon(subtractionDisabledIcon); subtraction.setEnabled(false); subtractionListener=new ActionListener(){ public void actionPerformed(ActionEvent e){ getSVGEditor().cancelActions(true); if(selectedNodes.size()==2){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); Runnable runnable=new Runnable(){ public void run() { subtraction(getSVGEditor().getFrameManager().getCurrentFrame(), selectedNodes); } }; frame.enqueue(runnable); } } }; subtraction.addActionListener(subtractionListener); intersection=new JMenuItem((String)labels.get("labelintersection"), intersectionIcon); intersection.setDisabledIcon(intersectionDisabledIcon); intersection.setEnabled(false); intersectionListener=new ActionListener(){ public void actionPerformed(ActionEvent e){ getSVGEditor().cancelActions(true); if(selectedNodes.size()>0){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); Runnable runnable=new Runnable(){ public void run() { intersection(getSVGEditor().getFrameManager().getCurrentFrame(), selectedNodes); } }; frame.enqueue(runnable); } } }; intersection.addActionListener(intersectionListener); //the menu allowing to make unions or intersections of paths menuitem=new JMenu((String)labels.get("labelpathoperations")); menuitem.add(convert); menuitem.add(union); menuitem.add(subtraction); menuitem.add(intersection); } /** * @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("idmenupathoperations"), menuitem); menuItems.put((String)ids.get("idquadratic"), quadratic); menuItems.put((String)ids.get("idcubic"), cubic);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -