Rex Mao

分享java技术与生活

java中怎样把图片存放在代码中

 

有时候不想把图片资源放在目录里,让用户看到,我用的方法是将图片的像素矩阵存在代码里。可能比较笨,具体做法是:

MediaTracker mt = new MediaTracker(aFrame); //媒体跟踪器

Image img = Toolkit.getDefaultToolkit().createImage("画刷.jpg"); //得到图片

mt.addImage(img, 1);

try {

      mt.waitForAll();

   }

catch (InterruptedException ex) {

   }

BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);

bi.getGraphics().drawImage(img, 0, 0, 16, 16, f);

int[] rbg = new int[bi.getWidth() * bi.getHeight()];

bi.getRGB(0, 0, 16, 16, rbg, 0, 16); //利用BufferedImage得到像素矩阵

for (int i = 0; i < rbg.length; i++) {

      System.out.print(rbg[i] + ","); //打印

   }

 

再在代码中创建静态对象

private static int[] penPixels = {

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215, -16777216, -16777216, -16777216, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, -16777216, -2031616, -2031616, -2031616, -16777216, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, -16777216, -2031616, -1, 16777215, -16777216, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      -16777216, -16777216, -2031616, -1, -16777216, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      -16777216, -256, -16777216, -16777216, -16777216, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, -16777216,

      -256, -1, -1842205, -16777216, 16777215, 16777215, 16777215, 16777215,

      16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, -16777216, -1,

      -256, -16777216, -16777216, 16777215, 16777215, 16777215, 16777215,

      16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, -16777216, 16777215,

      -256, -1842205, -16777216, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, -16777216, -256, -1,

      -16777216, -16777216, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215,

      16777215, 16777215, 16777215, 16777215, -16777216, -256, -1, -1842205,

      -16777216, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215,

      16777215, 16777215, 16777215, 16777215, -16777216, -16777216, -256,

      -16777216, -16777216, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, -16777216, -16777216, -16777216,

      -16777216, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, -16777216, -16777216, -16777216,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, -16777216, -16777216, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, -16777216, 16777215, 16777215,

      16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215,

      16777215, 16777215

 };//打印出来的矩阵

private static MemoryImageSource misPen = new MemoryImageSource(16, 16,

      penPixels, 0, 16);

public static Image imgPen = Toolkit.getDefaultToolkit().createImage(misPen);

这样就可以直接使用imgPen图片了。

posted on 2007-05-26 10:15 Rex Mao 阅读(1417) 评论(2)  编辑  收藏 所属分类: 技术

评论

# re: java中怎样把图片存放在代码中[未登录] 2007-10-05 13:57 king

牛人。。。。  回复  更多评论   

# re: java中怎样把图片存放在代码中[未登录] 2008-08-22 14:47 gogo

我晕~~  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: