- 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.GeneralPath;
- 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;
- /**
- *
- * 功能:读取资源文件类 <br>
- * 时间:2010-11-24 <br>
- * 备注: <br>
- *
- * @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.codesnippet.cn/detail/260520149658.html
来源: http://www.codesnippet.cn/detail/260520149658.html