Solved

How to display a "PGM" image file in java?

Posted on 2002-07-17
2
1,964 Views
Last Modified: 2008-01-09
How I can display a "PGM" (Portable Gray Map) file in java? Please write some actual code.

regards,
Subhasis
0
Comment
Question by:mitra_subhasis
2 Comments
 
LVL 2

Accepted Solution

by:
flumpman earned 50 total points
ID: 7161689
Using the JAI the code is simple - see below.

--
flumpman

import javax.media.jai.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DisplayPGM {

    public static void main(String[] args) {

        JFrame f = new JFrame("PGM");

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // create a JPanel to use as the content pane
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());

        f.setContentPane(p);

        // load the image using JAI
        PlanarImage pgmImage = JAI.create("fileload", "c:\\temp\\test.pgm");

        // create an ImageIcon from the image
        ImageIcon icon = new ImageIcon(pgmImage.getAsBufferedImage());

        JLabel l = new JLabel(icon);

        // add the image to the panel
        p.add(l, BorderLayout.CENTER);

        // set the size and show the frame
        f.setSize(600, 400);
        f.setVisible(true);
    }
}
0
 

Author Comment

by:mitra_subhasis
ID: 7171006
Sorry for being late.

regards,
Subhasis.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now