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