📄 svgpolyline.java
字号:
//gets or creates the undo/redo list and adds the action into it SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList((String)labels.get("undoredomodifypoint")); actionlist.add(action); getSVGEditor().getUndoRedo().addActionList(frame, actionlist); actionlist=null; } } } } } /** * used to remove the listener added to draw a polyline when the user clicks on the menu item */ public void cancelActions(){ if(polylineAction!=null) { toolItem.removeActionListener(polylineAction); toolItem.setSelected(false); toolItem.addActionListener(polylineAction); polylineAction.cancelActions(); } } /** * * @author Jordi SUC * the class allowing to get the position and size of the future drawn polyline */ protected class PolylineActionListener implements ActionListener{ /** * the hashtable associating a frame to its mouse adapter */ private final Hashtable mouseAdapterFrames=new Hashtable(); /** * an instance of this class */ private final PolylineActionListener action=this; /** * the cursor used when creating a rectangle */ private Cursor createCursor; private boolean isActive=false; /** * the source component */ private Object source=null; /** * the constructor of the class */ protected PolylineActionListener(){ createCursor=getSVGEditor().getCursors().getCursor("polyline"); } /** * resets the listener */ protected void reset(){ if(isActive){ Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; LinkedList toBeRemoved=new LinkedList(); Object mouseListener=null; //removes all the motion adapters from the frames for(it=mouseAdapterFrames.keySet().iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null && ! frames.contains(frm)){ try{ mouseListener=mouseAdapterFrames.get(frm); frm.getScrollPane().getSVGCanvas().removeMouseListener((MouseAdapter)mouseListener); frm.getScrollPane().getSVGCanvas().removeMouseMotionListener((MouseMotionListener)mouseListener); }catch (Exception ex){} toBeRemoved.add(frm); } } //removes the frames that have been closed for(it=toBeRemoved.iterator(); it.hasNext();){ try{mouseAdapterFrames.remove(it.next());}catch (Exception ex){} } PolylineMouseListener pml=null; //adds the new motion adapters for(it=frames.iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null && ! mouseAdapterFrames.containsKey(frm)){ pml=new PolylineMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(pml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(pml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, pml); } } } } /** * used to remove the listener added to draw a rectangle when the user clicks on the menu item */ protected void cancelActions(){ if(isActive){ //removes the listeners Iterator it; SVGFrame frm=null; LinkedList toBeRemoved=new LinkedList(); Object mouseListener=null; //removes all the motion adapters from the frames for(it=mouseAdapterFrames.keySet().iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null){ //resets the information displayed frm.getStateBar().setSVGW(""); frm.getStateBar().setSVGH(""); frm.getScrollPane().getSVGCanvas().setSVGCursor(frm.getSVGEditor().getCursors().getCursor("default")); try{ mouseListener=mouseAdapterFrames.get(frm); frm.getScrollPane().getSVGCanvas().removeMouseListener((MouseAdapter)mouseListener); frm.getScrollPane().getSVGCanvas().removeMouseMotionListener((MouseMotionListener)mouseListener); if(mouseListener!=null && ((PolylineMouseListener)mouseListener).paintListener!=null){ //removes the paint listener frm.getScrollPane().getSVGCanvas().removePaintListener(((PolylineMouseListener)mouseListener).paintListener, true); } }catch (Exception ex){} toBeRemoved.add(frm); } } //removes the frames that have been closed for(it=toBeRemoved.iterator(); it.hasNext();){ try{mouseAdapterFrames.remove(it.next());}catch (Exception ex){} } isActive=false; } } /** * the method called when an event occurs * @param evt the event */ public void actionPerformed(ActionEvent evt){ if((evt.getSource() instanceof JMenuItem && ! toolItem.isSelected()) || (evt.getSource() instanceof JToggleButton)){ getSVGEditor().cancelActions(false); if(getSVGEditor().getFrameManager().getCurrentFrame()!=null){ toolItem.removeActionListener(polylineAction); toolItem.setSelected(true); toolItem.addActionListener(polylineAction); //the listener is active isActive=true; source=evt.getSource(); Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; PolylineMouseListener pml=null; //adds the new motion adapters for(it=frames.iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null){ pml=new PolylineMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(pml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(pml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, pml); } } } } } protected class PolylineMouseListener extends MouseAdapter implements MouseMotionListener{ /** * the list containing the points of the polyline */ private LinkedList<Point2D.Double> points=new LinkedList<Point2D.Double>(); /** * the frame */ private SVGFrame frame; /** * the paint listener */ private CanvasPaintListener paintListener=null; /** * the last clicked point */ private Point2D.Double lastPoint=null; /** * the constructor of the class * @param frame a frame */ public PolylineMouseListener(SVGFrame frame){ this.frame=frame; final SVGFrame fframe=frame; //adds a paint listener paintListener=new CanvasPaintListener(){ public void paintToBeDone(Graphics g) { if(points.size()>1){ //draws the shape of the path that will be created if the user released the mouse button Point2D.Double[] scPts=new Point2D.Double[points.size()]; Point2D.Double pt=null; for(int i=0;i<scPts.length;i++){ pt=points.get(i); scPts[i]=fframe.getScaledPoint(new Point2D.Double(pt.x, pt.y), false); } if(g!=null && scPts!=null){ svgPolyline.drawGhost(fframe, (Graphics2D)g, scPts); } } } }; frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.DRAW_LAYER, paintListener, false); } /** * the method called when an event occurs * @param evt the event */ public void mouseDragged(MouseEvent evt) { } /** * the method called when an event occurs * @param evt the event */ public void mouseMoved(MouseEvent evt) { if(points.size()>0){ Point2D.Double point=frame.getAlignedWithRulersPoint(evt.getPoint()); if(evt.isControlDown()){ point=SVGToolkit.computeLinePointWhenCtrlDown(points.get(points.size()-2), point); } points.set(points.size()-1, point); } //asks the canvas to be repainted to draw the shape of the future polygon frame.getScrollPane().getSVGCanvas().delayedRepaint(); } /** * the method called when an event occurs * @param evt the event */ public void mouseClicked(MouseEvent evt) { Point2D.Double point=frame.getAlignedWithRulersPoint(evt.getPoint()); if(evt.isControlDown() && points.size()>1){ point=SVGToolkit.computeLinePointWhenCtrlDown(points.get(points.size()-2), point); } if(point!=null){ //checking if the click is a double click boolean isDoubleClick=false; if(lastPoint!=null && Math.abs(lastPoint.getX()-evt.getPoint().getX())<2 && Math.abs(lastPoint.getY()-evt.getPoint().getY())<2){ isDoubleClick=true; } lastPoint=new Point2D.Double(evt.getPoint().x, evt.getPoint().y); if(isDoubleClick){ if(points.size()>1){ points.removeLast(); final Point2D.Double[] pointsTab=points.toArray(new Point2D.Double[0]); Runnable runnable=new Runnable(){ public void run(){ svgPolyline.drawPolyline(frame, pointsTab); } }; frame.enqueue(runnable); getSVGEditor().cancelActions(true); //clears the array list points.clear(); lastPoint=null; } }else{ if(points.size()==0){ points.add(point); } points.set(points.size()-1, point); points.add(point); } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -