java獲取資源文件

[代碼] [Java]代碼 package com.lin.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.geom.Ge http://www.fpzhuhai.com/linked/20130301.do; neralPath;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.ImageIcon;

/**
 * 
 * 功用:讀取資源文件類 
* 時刻:2010-11-24
* 補白:
* * @author Lin.~ * */ public abstract class ResourceUtil { /** * 獲得資源文件 * * @param path * 資源文件的途徑 * @return */ protected final URL getURL(String path) { // 先從當時目選取(打成jar包的情況下) URL url = getClass().getResource(path); // 如果沒取到,則從根目選取(打成jar包的情況下) if (url == null) url = getClass().getResource("/" path); // 從當時線程的地址取 if (null == url) url = Thread.currentThread().getContextClassLoader() .getResource(path); // 以上代碼都是對於swing的。下面代碼對於eclipse中情況 if (url == null) { try { String rootPath = System.getProperty("user.dir"); // 對於在eclipse中,用Java Application運轉的。 File webFile = new File(rootPath "/" path); if (webFile.exists()) { url = webFile.toURI().toURL(); } else { // 對於eclipse頂用web運轉的。 webFile = new File(Thread.currentThread().getContextClassLoader() .getResource("/") "/" path); url = webFile.toURI().toURL(); } // 真實不行了,死馬當活馬醫吧 if(null ==url) url = new File(new File("").getAbsoluteFile() "/" path).toURI().toURL(); } catch (MalformedURLException e) { e.printStackTrace(); } } if(null == url) throw new NullPointerException("對不住,一直沒有找到【" path "】資源"); return url; } /** * 獲得資源文件讀取流 * * @param filePath * 資源文件途徑 * @return 資源文件流 * @throws IOException */ private InputStream getJarIO(String filePath) throws IOException { String JarPath = URLDecoder.decode(getClass().getProtectionDomain() .getCodeSource().getLocation().getFile(), "UTF-8"); if (JarPath.startsWith("/")) JarPath = JarPath.substring(1); JarFile cJar = new JarFile(JarPath); JarEntry util = cJar.getJarEntry(filePath); return cJar.getInputStream(util); } /** * 修正資源文件 * * @param filePath * 資源文件途徑 * @return 資源文件寫入流 * @throws Exception */ protected final OutputStream getJarOut(String filePath) throws IOException { throw new IOException("沒有完結修正Jar包內部的文件的辦法"); } /** * 讀取資源文件流 * * @param resourceName * @return */ protected final InputStream getIO(String filePath) { URL url = getURL(filePath); try { return url != null ? url.openStream() : getJarIO(filePath); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 獲得圖像 * * @param path * 圖像路江 * @return */ public ImageIcon getImageIcon(String path) { return new ImageIcon(getURL(path)); } /** * 縮放圖像 * * @param icon * 要縮放的圖像 * @param width * 縮放寬度 * @param height * 縮放高度 * @return 縮放後的圖像 */ public static ImageIcon zoomImg(ImageIcon icon, int width, int height) { Image img = icon.getImage(); Image newImg = img.getScaledInstance(width, height, 1); return new ImageIcon(newImg); } /** * 製做圓角圖像 * * @param icon * 要製做的原圖 * @param width * 製做圓角寬 * @param height * 製做圓角高 * @return 製做完結的圖像 */ public static BufferedImage getCircularImage(ImageIcon icon, int width,int height) { BufferedImage buff = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT); Image i = icon.getImage(); Graphics2D g = (Graphics2D) buff.getGraphics(); buff = g.getDeviceConfiguration().createCompatibleImage( icon.getIconWidth(), icon.getIconHeight(), Transparency.TRANSLUCENT); g.dispose(); g = buff.createGraphics(); System.out.println(g.getColor()); g.setColor(Color.RED); g.setStroke(new BasicStroke(1)); RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, icon.getIconWidth(), icon.getIconHeight(), width, height); GeneralPath p = new GeneralPath(); p.append(rect, false); g.setClip(p); g.drawImage(i, 0, 0, null); return buff; } } http://www.fpfuzhou.com/linked/20130301.do;
相關文章
相關標籤/搜索