A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


  1. package main;
  2. /**
  3. * 资源加载工具类
  4. * 【方法接口】
  5. *         public static ImageIcon getImageIcon(String imagePath)
  6. *                                                 返回imagePath路径下图像文件创建的ImageIcon对象
  7. *         public static Clip getClip(String soundPath)
  8. *                                                 返回soundPath路径下音频文件创建的Clip对象
  9. * */

  10. import java.io.*;
  11. import javax.swing.*;
  12. import javax.sound.sampled.*;


  13. public  class ResourceLoading
  14. {

  15.         private static ResourceLoading instance=new ResourceLoading();
  16.        
  17. /**
  18. * 私有化构造函数,不让创建对象
  19. * */
  20.         private ResourceLoading(){}
  21.        
  22.         /**
  23.          *
  24.          * 返回
  25.          *
  26.          * */
  27.         public static ImageIcon getImageIcon(String imagePath)
  28.                 {
  29.                        
  30.                         InputStream imageIn=instance.getClass().getClassLoader().getResourceAsStream(imagePath);
  31.                         byte[] imageDate=new byte[1024*1024];
  32.                         try
  33.                         {
  34.                                 imageIn.read(imageDate);
  35.                         }catch(IOException e)
  36.                         {
  37.                                 e.printStackTrace();
  38.                                 throw new RuntimeException("图像"+imagePath+"载入错误!!!");
  39.                         }finally{
  40.                                 try
  41.                                 {imageIn.close();}catch(IOException e){}
  42.                         }
  43.                         return new ImageIcon (imageDate);
  44.                 }
  45.        
  46.        
  47.        
  48.         public static Clip getClip(String soundPath)
  49.         {
  50.                         Clip clip=null;
  51.                         try
  52.                         {
  53.                                 InputStream soundIn=instance.getClass().getClassLoader().getResourceAsStream(soundPath);
  54.                                 //System.out.println("found sound at"+soundIn);
  55.                                 Line.Info linfo=new Line.Info(Clip.class);
  56.                                 Line line =AudioSystem.getLine(linfo);
  57.                                 clip=(Clip)line;
  58.                                 AudioInputStream ais=AudioSystem.getAudioInputStream(soundIn);
  59.                                 clip.open(ais);
  60.                                 soundIn.close();
  61.                                 ais.close();
  62.                         }catch (Exception e){
  63.                                 e.printStackTrace();
  64.                         }
  65.                         return clip;

  66.                 }
  67.        

  68. }

复制代码
以上是从类加载路径下加载资源的代码,有没有更好更优化的方法,可以使打包成jar包的时候也能加载资源?

评分

参与人数 1技术分 +1 收起 理由
猫腻 + 1

查看全部评分

1 个回复

正序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马