📄 svgrectangle.java
字号:
toolItem.addActionListener(rectAction); rectAction.cancelActions(); } } /** * @author Jordi SUC * the class allowing to get the position and size of the future drawn rectangle */ protected class RectActionListener implements ActionListener{ /** * the hashtable associating a frame to its mouse adapter */ private final Hashtable mouseAdapterFrames=new Hashtable(); /** * the cursor used when creating a rectangle */ private Cursor createCursor; private boolean isActive=false; /** * the constructor of the class */ protected RectActionListener(){ createCursor=getSVGEditor().getCursors().getCursor("rectangle"); } /** * resets the listener */ protected void reset(){ if(isActive){ Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; Object mouseListener=null; //removes all the motion adapters from the frames for(it=new LinkedList(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){} mouseAdapterFrames.remove(frm); } } RectMouseListener rml=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)){ rml=new RectMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(rml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(rml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, rml); } } } } /** * 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; Object mouseListener=null; //removes all the motion adapters from the frames for(it=new LinkedList(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 && ((RectMouseListener)mouseListener).paintListener!=null){ //removes the paint listener frm.getScrollPane().getSVGCanvas().removePaintListener(((RectMouseListener)mouseListener).paintListener, true); } }catch (Exception ex){} mouseAdapterFrames.remove(frm); } } 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(rectAction); toolItem.setSelected(true); toolItem.addActionListener(rectAction); //the listener is active isActive=true; Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; //adds the new mouse adapters for(it=frames.iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null){ RectMouseListener rml=new RectMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseMotionListener(rml); frm.getScrollPane().getSVGCanvas().addMouseListener(rml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, rml); } } } } } protected class RectMouseListener extends MouseAdapter implements MouseMotionListener{ /** * the points of the area corresponding to the future rectangle */ private Point2D.Double point1=null, point2=null; private SVGFrame frame; /** * the paint listener */ private CanvasPaintListener paintListener=null; /** * the constructor of the class * @param frame a frame */ public RectMouseListener(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 computedBounds=fframe.getScaledRectangle(rect, false); //draws the shape of the element that will be created if the user released the mouse button svgRect.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() { svgRect.drawRectangle(frame, rect); } }; frame.enqueue(runnable); } getSVGEditor().cancelActions(true); point1=null; point2=null; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -