📄 svgellipse.java
字号:
private final EllipseActionListener 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 EllipseActionListener(){ createCursor=getSVGEditor().getCursors().getCursor("ellipse"); } /** * 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){} } EllipseMouseListener eml=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)){ eml=new EllipseMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(eml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(eml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, eml); } } } } /** * 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 && ((EllipseMouseListener)mouseListener).paintListener!=null){ //removes the paint listener frm.getScrollPane().getSVGCanvas().removePaintListener(((EllipseMouseListener)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(ellipseAction); toolItem.setSelected(true); toolItem.addActionListener(ellipseAction); //the listener is active isActive=true; source=evt.getSource(); Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; EllipseMouseListener eml=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){ eml=new EllipseMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(eml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(eml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, eml); } } } } } protected class EllipseMouseListener extends MouseAdapter implements MouseMotionListener{ /** * the points of the area corresponding to the future rectangle */ private Point2D.Double point1=null, point2=null; /** * the paint listener */ private CanvasPaintListener paintListener=null; private SVGFrame frame; /** * the constructor of the class * @param frame a frame */ public EllipseMouseListener(SVGFrame frame){ this.frame=frame; final SVGFrame fframe=frame; //adds a paint listener paintListener=new CanvasPaintListener(){ public void paintToBeDone(Graphics g) { if(point1!=null && point2!=null){ Rectangle2D.Double rect=getSVGEditor().getSVGToolkit().getComputedRectangle(point1, point2); Rectangle2D.Double rect2=new Rectangle2D.Double(rect.x, rect.y, rect.width, rect.height); Rectangle2D.Double computedBounds=fframe.getScaledRectangle(rect2, false); //draws the shape of the element that will be created if the user released the mouse button svgEllipse.drawGhost(fframe, (Graphics2D)g,computedBounds); } } }; 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) { //sets the second point of the element point2=frame.getAlignedWithRulersPoint(evt.getPoint()); //asks the canvas to be repainted to draw the shape of the future element frame.getScrollPane().getSVGCanvas().delayedRepaint(); } /** * the method called when an event occurs * @param evt the event */ public void mouseMoved(MouseEvent evt) { } /** * the method called when an event occurs * @param evt the event */ public void mousePressed(MouseEvent evt){ //sets the first point of the area corresponding to the future element point1=frame.getAlignedWithRulersPoint(evt.getPoint()); } /** * the method called when an event occurs * @param evt the event */ public void mouseReleased(MouseEvent evt){ Point2D.Double point=frame.getAlignedWithRulersPoint(evt.getPoint()); //creates the element in the SVG document if(point1!=null && point1.x>=0 && point1.y>=0 && point!=null){ final Rectangle2D.Double rect=getSVGEditor().getSVGToolkit().getComputedRectangle(point1, point); Runnable runnable=new Runnable(){ public void run() { svgEllipse.drawEllipse(frame, rect); } }; frame.enqueue(runnable); } getSVGEditor().cancelActions(true); point1=null; point2=null; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -