[代码] [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;
转载于:https://my.oschina.net/u/947963/blog/111254