I am assigned a task to build a simple xpm image viewer. I can't use any exist开发者_JS百科ing toolkit library for this. I know that xpm images are string array like this ( I can write one) -
/* XPM */
static const char *const hi[] = {
"7 5 2 1",
" c black",
". c yellow",
".. ..",
". . . .",
". . .",
". .",
". ."
};
I want to use java for this. My question is -
1. How to make a String variable (hi[]) from this xpm file so that I can use it in my main class? 2. Good way to display it in a GUI? 3. Any other dictation...Many thanks for your help
You'll have to firstly write a parser - a program/method/class/whatever that reads this file line-wise and extract the necessary data.
BufferedReader r =
new BufferedReader(new InputStreamReader(new FileInputStream(file),
"US-ASCII"));
gives you a BufferedReader, which has a readLine() method. The first some lines you throw away or handle specially, and then the main bunch of lines are the real image data. There you throw away the quotes and commas, and have the plain data in string form.
To put it in a image, look at the classes in java.awt.image - specially BufferedImage and the classes used by it (Raster/WriteableRaster, IndexColorModel).
Instead, you could also simply hold the data in your String[] form, and in the paint-method of a custom component access the individual pixels. This would be a bit slower, I think.
Don't know if this will work for you: http://www.bolthole.com/java/Xpm.html , but I reckon once it is converted into a Java image, you should able to do whatever you want in Java.
精彩评论