many questions about displaying a PPM image in a Swing component

M

Marteno Rodia

Hello everyone,
I'm building a GUI for an application and I want to display an image
as a part of a JFrame. Which component should I use for this purpose?
I thought I must implement my own component and overload its paint()
method, but I've found ImageIcon. Of course, first I need to read in
an image from a file to an Image object. It is possible to use
BufferedImage class and its read() method... But I must use an
uncompressed image in the PPM format (don't ask why, it's a
requitement). Apparently, the read() method won't recognize this
format. So what should I do?

Unfortunately, I must implement reading the file by myself (it's not a
very complicated task). I think it's perhaps possible to "pull out"
the graphics context for the Image object and then to drow an image
pixel by pixel... But how to do it in detail?

MR
 
A

Albert

Marteno Rodia a écrit :
Hello everyone,
I'm building a GUI for an application and I want to display an image
as a part of a JFrame. Which component should I use for this purpose?
I thought I must implement my own component and overload its paint()
method, but I've found ImageIcon. Of course, first I need to read in
an image from a file to an Image object. It is possible to use
BufferedImage class and its read() method... But I must use an
uncompressed image in the PPM format (don't ask why, it's a
requitement). Apparently, the read() method won't recognize this
format. So what should I do?

Unfortunately, I must implement reading the file by myself (it's not a
very complicated task). I think it's perhaps possible to "pull out"
the graphics context for the Image object and then to drow an image
pixel by pixel... But how to do it in detail?

Well, with 2 for loops :

BufferedImage bi = new BufferedImage(width, height, TYPE_INT_RGB);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bi.setRGB(r, g, b);

You try to set the pixel in the image while reading the file (best) or
read all the data of the file in some arraylist, and after that setting
the pixel in the image from the arraylist. If you don't know the
direction of the pixel in the file, you should find the PPM specs (or
try changing the loops until you succeded).
 
J

John B. Matthews

Albert said:
Marteno Rodia a écrit :

Well, with 2 for loops :

BufferedImage bi = new BufferedImage(width, height, TYPE_INT_RGB);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bi.setRGB(r, g, b);

ITYM the method setRGB(int x, int y, int rgb) in BufferedImage:

<http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html>

Also, it's sometimes convenient to modify the underlying raster, as
shown here:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top