⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svgresourceimagemanager.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param image the image representing the resource     */    protected void setImage(SVGFrame frame, String resourceId, Image image){                if(frame!=null && resourceId!=null && image!=null){                    	Map<String, ImageRepresentation> idToImageMap=null;                        if(frameToIdToImages.containsKey(frame)){                                idToImageMap=frameToIdToImages.get(frame);                            }else{                                idToImageMap=new Hashtable<String, ImageRepresentation>();                frameToIdToImages.put(frame, idToImageMap);            }                        synchronized(this){idToImageMap.put(resourceId, new ImageRepresentation(image));}        }    }        /**     * getting the image representing a resource given the id of a resource     * @param frame a frame      * @param resourceId the id of the resource from which the image has been created     * @return the image representation object representing the resource     */    public ImageRepresentation getResourceImage(SVGFrame frame, String resourceId){                ImageRepresentation imageRepresentation=null;                if(frame!=null && resourceId!=null && ! resourceId.equals("")){                    	Map<String, ImageRepresentation> idToImageMap=null;                        if(frameToIdToImages.containsKey(frame)){                                idToImageMap=frameToIdToImages.get(frame);                                if(idToImageMap.containsKey(resourceId)){                                    	imageRepresentation=idToImageMap.get(resourceId);                }            }        }                return imageRepresentation;    }        /**     * getting the image representing a resource given the id of a resource     * @param frame a frame      * @param resourceId the id of the resource from which the image has been created     * @param useSmallImage whether the returned image should be small or large     * @return the image representing the resource     */    public Image getImage(SVGFrame frame, String resourceId, boolean useSmallImage){                Image image=null;                if(frame!=null && resourceId!=null && ! resourceId.equals("")){                    	Map<String, ImageRepresentation> idToImageMap=null;                        if(frameToIdToImages.containsKey(frame)){                                idToImageMap=frameToIdToImages.get(frame);                                if(idToImageMap.containsKey(resourceId)){                                    	ImageRepresentation imageRepresentation=idToImageMap.get(resourceId);                	                	if(imageRepresentation!=null){                		                		if(useSmallImage){                			                			image=imageRepresentation.getSmallImage();                			                		}else{                			                			image=imageRepresentation.getLargeImage();                		}                	}                }            }        }                return image;    }        /**     * the class of the panel displaying a representation of a resource     *      * @author Jordi SUC     */    public class ResourceRepresentation extends JPanel{    	    	/**    	 * a frame    	 */    	private SVGFrame frame;    	    	/**    	 * the id of a resource    	 */    	private String resourceId="";    	    	/**    	 * the image of the representation of the resource    	 */    	public Image resourceImage=null;    	    	/**    	 * whether this resource representation should display a large or a small image    	 */    	private boolean useSmallImage=false;    	    	/**    	 * the constructor of the class    	 * @param frame a frame    	 * @param resourceId the id of a resource    	 * @param useSmallImage whether this resource representation should display a large or a small image    	 */    	protected ResourceRepresentation(SVGFrame frame, String resourceId, boolean useSmallImage){    		    		this.frame=frame;    		this.resourceId=resourceId;    		this.useSmallImage=useSmallImage;            setBackground(Color.white);            setLayout(null);                        if(useSmallImage){            	                setPreferredSize(new Dimension(smallImageSize.width+2, smallImageSize.height+2));            	            }else{            	                setPreferredSize(new Dimension(imageSize.width+2, imageSize.height+2));            }                        ImageRepresentation imageRepresentation=getResourceImage(frame, resourceId);            Image image=null;                        if(imageRepresentation!=null){            	                if(useSmallImage){                    image=new ImageIcon(imageRepresentation.getSmallImage()).getImage();                	                }else{                    image=new ImageIcon(imageRepresentation.getLargeImage()).getImage();                }            }            if(image==null){                resourceRepresentationList.add(this);                            }else{                                synchronized(this){resourceImage=image;}            }    	}    	    	/**    	 * refreshes the representation    	 */    	protected void refreshRepresentation(){    		ImageRepresentation imageRepresentation=getResourceImage(frame, resourceId);    		    		if(imageRepresentation!=null){    		            		if(useSmallImage && imageRepresentation.getSmallImage()!=null){        			        			synchronized(this){resourceImage=new ImageIcon(imageRepresentation.getSmallImage()).getImage();}        			        		}else if(imageRepresentation.getLargeImage()!=null){        			        			synchronized(this){resourceImage=new ImageIcon(imageRepresentation.getLargeImage()).getImage();}        		}        		if(resourceImage!=null){ 			    				refreshParents();        		}    		}    	}        /**         * @return Returns the resourceImage.         */        public Image getImage() {            return resourceImage;        }                /**         * disposes this image representation         */        public void dispose(){        	        	frame=null;        	resourceImage=null;        }                /**		 * @return the frame		 */		public SVGFrame getFrame() {			return frame;		}            	/**    	 * refreshes the parents    	 */    	protected void refreshParents(){    				    if(getParent()!=null){		        		        if(getParent().getParent()!=null){		            		            if(getParent().getParent().getParent()!=null){		                			            if(getParent().getParent().getParent().getParent()!=null){			                					        getParent().getParent().getParent().repaint();					        			            }else{			                					        getParent().getParent().getParent().repaint();			            }		            }else{		                		                getParent().getParent().repaint();		            }		            		        }else{		          		            getParent().repaint();		        }		    }    	}    	    	@Override		protected void paintComponent(Graphics g) {			super.paintComponent(g);			if(resourceImage!=null){				                g.drawImage(resourceImage, 1, 1, this);			}		}    }        /**     * the class containing the images representing     *      * @author Jordi SUC     */    protected class ImageRepresentation{    	    	/**    	 * the large image    	 */    	private Image largeImage=null;    	    	/**    	 * the small image    	 */    	private Image smallImage=null;    	    	/**    	 * the constructor of the class    	 * @param image an image    	 */    	protected ImageRepresentation(Image image){    		    		this.largeImage=image;    		this.smallImage=image.getScaledInstance(smallImageSize.width, smallImageSize.height, Image.SCALE_SMOOTH);    	}		/**		 * @return Returns the largeImage.		 */		protected Image getLargeImage() {			return largeImage;		}				/**		 * @return Returns the smallImage.		 */		protected Image getSmallImage() {			return smallImage;		}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -