Solved

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

Posted on 2002-07-17
2
2,033 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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

749 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