问题描述
我被分配了一个任务来构建一个简单的xpm图像查看器。我不能使用任何现有的工具包库。
我知道xpm图像是这样的字符串数组(我可以写一个) -
I am assigned a task to build a simple xpm image viewer. I can't use any existing 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",
".. ..",
". . . .",
". . .",
". .",
". ."
};
我想用java来做这件事。我的问题是 -
1.如何从这个xpm文件中创建一个String变量(hi []),以便我可以在我的主类中使用它?
2.好方法在GUI中显示它?
3.任何其他听写...
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...
非常感谢您的帮助
推荐答案
你必须首先编写一个解析器 - 一个程序/方法/类/无论是按行读取这个文件还是提取必要的数据。
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"));
给你一个BufferedReader,它有一个readLine()方法。
你扔掉或专门处理的第一行,然后主要的一行就是真实的图像数据。在那里你丢弃引号和逗号,并以字符串形式包含普通数据。
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.
要将它放在图像中,请查看java.awt.image中的类 - 特别是BufferedImage及其使用的类(Raster / WriteableRaster,IndexColorModel)。
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).
相反,您也可以简单地将数据保存在String []表单中,并且自定义组件的paint-method访问各个像素。我认为这会慢一些。
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.
这篇关于如何阅读简单的xpm图像并使用Java显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!