我这里有半工作代码。我画得不好,图像没有正确绘制。我正在读取数组 RGB 值(ppm 格式)。我不确定我做错了什么,但这是我的代码和图片(它应该是红色的 Lancia Stratos):
http://oi60.tinypic.com/20h91kk.jpg
package ppmHomework;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ReadImage {
public ReadImage() {
}
public static void main(String[] args) throws FileNotFoundException {
int width, height, maxRGB;
File file = new File("ppmImage.ppm");
Scanner kb = new Scanner(file);
kb.next();
width = kb.nextInt();
height = kb.nextInt();
maxRGB = kb.nextInt();
JFrame frame;
BufferedImage img;
int[] arrayImage = new int[width * height * 3];
int j=0;
while (kb.hasNextInt()) {
arrayImage[j] = kb.nextInt();
j++;
}
img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
int i = 0;
for (int k = 0; k < width; k++) {
for (int p = 0; p < height; p++) {
System.out.println(arrayImage[i] + " " + arrayImage[i+1] + " " + arrayImage[i+2] + " " + i);
int col = new Color(arrayImage[i], arrayImage[i+1], arrayImage[i+2]).getRGB();
img.setRGB(k, p, col);
i+=3;
}
}
frame = new JFrame("WINDOW");
frame.setVisible(true);
frame.add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
非常感谢任何帮助...谢谢