注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

享受编码

    的乐趣

 
 
 

日志

 
 

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

2010-01-10 10:16:45|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

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图片了。

  评论这张
 
阅读(644)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017