📄 svgimage.java
字号:
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){} } ImageMouseListener iml=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)){ iml=new ImageMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(iml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(iml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, iml); } } } } /** * 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 && ((ImageMouseListener)mouseListener).paintListener!=null){ //removes the paint listener frm.getScrollPane().getSVGCanvas().removePaintListener(((ImageMouseListener)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 action to be done * @param e the event */ public void actionPerformed(ActionEvent e){ if((e.getSource() instanceof JMenuItem && ! toolItem.isSelected()) || (e.getSource() instanceof JToggleButton)){ getSVGEditor().cancelActions(false); if(getSVGEditor().getFrameManager().getCurrentFrame()!=null){ toolItem.removeActionListener(imageAction); toolItem.setSelected(true); toolItem.addActionListener(imageAction); //the listener is active isActive=true; source=e.getSource(); Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; ImageMouseListener iml=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){ iml=new ImageMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(iml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(iml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, iml); } } } } } protected class ImageMouseListener 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 the current frame */ public ImageMouseListener(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 svgImage.drawGhost(fframe, (Graphics2D)g, computedBounds); } } }; frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.DRAW_LAYER, paintListener, false); } /** * @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(); } /** * @param evt the event */ public void mouseMoved(MouseEvent evt) { } /** * @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()); } /** * @param evt the event */ public void mouseReleased(MouseEvent evt){ //the dialog box to specify the image file String uri; JFileChooser fileChooser=new JFileChooser(); if(getSVGEditor().getResource().getCurrentDirectory()!=null){ fileChooser.setCurrentDirectory(getSVGEditor().getResource().getCurrentDirectory()); } String canvasPath=""; try{ canvasPath=new URI(frame.getName()).toString(); }catch (Exception ex){canvasPath="";} final String fcanvasPath=canvasPath; //the file filter fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter(){ public boolean accept(File f) { String name=f.getName(); name=name.toLowerCase(); if( f.isDirectory() || (name.endsWith(SVGToolkit.SVG_FILE_EXTENSION) && ! fcanvasPath.equals(f.toURI().toString())) || name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".gif")){ return true; } else return false; } public String getDescription() { return (String)labels.get("filefilter"); } }); fileChooser.setMultiSelectionEnabled(false); int returnVal=fileChooser.showOpenDialog(frame.getSVGEditor().getParent()); if(returnVal==JFileChooser.APPROVE_OPTION) { getSVGEditor().getResource().setCurrentDirectory(fileChooser.getCurrentDirectory()); URI docUri=null; try{ docUri=new URI(frame.getName()); }catch (Exception ex){docUri=null;} if(docUri!=null){ //relativizes the uri URI rUri=null; URI u=fileChooser.getSelectedFile().toURI(); try{ File docFile=new File(docUri); docUri=docFile.getParentFile().toURI(); rUri=docUri.relativize(u); }catch (Exception ex){} uri=rUri.toString(); }else{ try{ uri=fileChooser.getSelectedFile().toURI().toString(); }catch (Exception ex) {uri=null;} } Point2D.Double point=frame.getAlignedWithRulersPoint(evt.getPoint()); //creates the image in the SVG document if(uri!=null && ! uri.equals("") && point1!=null && point1.x>=0 && point1.y>=0 && point!=null){ final Rectangle2D.Double rect=getSVGEditor().getSVGToolkit().getComputedRectangle(point1, point); final String furi=new String(uri); Runnable runnable=new Runnable(){ public void run() { svgImage.drawImage(frame, rect, furi); } }; frame.enqueue(runnable); } } getSVGEditor().cancelActions(true); point1=null; point2=null; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -