Solved

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

Posted on 2002-07-17
2
2,094 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

688 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