Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 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

12 Experts available now in Live!

Get 1:1 Help Now