📄 svgline.java
字号:
/* * Created on 6 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.resources.*;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.*;/** * @author Jordi SUC * the class that allows to add, select,modify the properties, delete, transform a line on the canvas */public class SVGLine extends SVGShape{ /** * the reference of an object of this class */ private final SVGLine svgLine=this; /** * the action listener used to draw the line */ private LineActionListener lineAction=null; /** * the format */ private DecimalFormat format=null; /** * the constructor of the class * @param editor the editor */ public SVGLine(SVGEditor editor) { super(editor); ids.put("id","line"); ids.put("idmenuitem","Line"); //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labels.put("label", bundle.getString("shapelinelabel")); labels.put("toolitemlabel", bundle.getString("shapelinetoolitemlabel")); labels.put("undoredocreate", bundle.getString("shapelineundoredocreate")); labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate")); labels.put("undoredoresize", bundle.getString("shapelineundoredoresize")); labels.put("undoredomodifypoint", bundle.getString("shapelineundoredomodifypoint")); labels.put("undoredorotate", bundle.getString("shapelineundoredorotate")); labels.put("helpcreate", bundle.getString("shapelinehelpcreate")); }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); lineAction=new LineActionListener(); //the toggle button toolItem=new JToggleButton(disabledIcon); toolItem.setEnabled(false); toolItem.setToolTipText(labels.get("toolitemlabel")); //adds a listener to the menu item and the toggle button menuitem.addActionListener(lineAction); toolItem.addActionListener(lineAction); //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); } lineAction.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(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 line * @param frame the current SVGFrame * @param point1 the first point * @param point2 the second point */ protected void drawLine(SVGFrame frame, Point2D.Double point1, Point2D.Double point2){ if(frame!=null && point1!=null && point2!=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 line Element line = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"line"); line.setAttributeNS(null,"x1", format.format(point1.x)); line.setAttributeNS(null,"y1", format.format(point1.y)); line.setAttributeNS(null,"x2", format.format(point2.x)); line.setAttributeNS(null,"y2", format.format(point2.y)); String colorString=SVGEditor.getColorChooser().getColorString(SVGColorManager.getCurrentColor()); line.setAttributeNS(null, "style", "stroke:".concat(colorString.concat(";"))); //sets that the svg has been modified frame.setModified(true); //creates final variables final Node fline=line; //attaches the circle to the svg root element parent.appendChild(fline); //create the undo/redo action and insert it into the undo/redo stack if(getSVGEditor().getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction(labels.get("undoredocreate")){ public void undo(){ parent.removeChild(fline); } public void redo(){ // attaches the circle to the svg root element parent.appendChild(fline); } }; SVGSelection selection=getSVGEditor().getSVGSelection(); if(selection!=null){ selection.deselectAll(frame, false, true); selection.addUndoRedoAction(frame, action); selection.handleNodeSelection(frame, line); 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 line will be if the user releases the mouse button * @param frame the current SVGFrame * @param g the graphics element * @param point1 the first point * @param point2 the second point */ protected void drawGhost(SVGFrame frame, Graphics2D g, Point2D.Double point1, Point2D.Double point2){ if(frame!=null && g!=null && point1!=null && point2!=null){ g=(Graphics2D)g.create(); //draws the new awt line to be displayed g.setColor(GHOST_COLOR); g.setXORMode(Color.white); g.drawLine((int)point1.x, (int)point1.y, (int)point2.x, (int)point2.y); g.dispose(); } } /** * gets the nexts level after the given selection level * @param type a selection level * @param clicknb the number of mouse clicks * @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 around the line * @param frame the current SVGFrame * @param graphics the graphics * @param node the node to be selected * @return the list of the selection squares */ protected LinkedList drawRotateSelection(SVGFrame frame, Graphics graphics, Node node){ LinkedList squarelist=new LinkedList(); Graphics2D g=(Graphics2D)graphics; if(g!=null && node!=null){ int sqd=5; Rectangle2D rect=frame.getNodeGeometryBounds((Element)node); if(rect!=null){ //computes and draws the new awt line to be displayed Rectangle2D.Double rect2=new Rectangle2D.Double(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight()); Rectangle2D.Double scRect=frame.getScaledRectangle(rect2, false); double sx1=scRect.getX(), sy1=scRect.getY(); //the coordinates of the selection point int[] sqx=new int[1]; int[] sqy=new int[1]; sqx[0]=(int)(sx1+scRect.getWidth()/2)-sqd; sqy[0]=(int)(sy1+scRect.getHeight()/2)-sqd; //the id for the selection point String[] types=new String[1]; types[0]="C"; //the cursor associated with the selection point Cursor[] cursors=new Cursor[1]; cursors[0]=new Cursor(Cursor.HAND_CURSOR); //draws the graphic elements Shape shape=null; GradientPaint gradient=null; for(int i=0;i<1;i++){ if(editor.getSVGSelection()!=null){ squarelist.add( new SVGSelectionSquare(node,types[i], new Rectangle2D.Double(sqx[i],sqy[i],2*sqd,2*sqd), cursors[i])); } shape=getArrow(new Point2D.Double(sqx[i]+sqd, sqy[i]+sqd), types[i], ROTATE_SELECTION); if(shape!=null){ gradient=new GradientPaint(sqx[i], sqy[i], SQUARE_SELECTION_COLOR1, sqx[i]+2*sqd, sqy[i]+2*sqd, SQUARE_SELECTION_COLOR2, true); g.setPaint(gradient); g.fill(shape); g.setColor(LINE_SELECTION_COLOR); g.draw(shape); } } } } return squarelist; } /** * draws the selection for the line * @param frame the current SVGFrame * @param graphics the graphics * @param nde the node to be selected * @return the list of the selection squares */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -