ImageViewer.java

This program uses the class ImagePanel


/* 
 * (C) Copyright 2000-2008, by Scott Preston and Preston Research LLC
 *
 * Project Info:  http://www.scottsbots.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *   
 */
//package org.javarobots.vision;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

import javax.swing.JFrame;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

public class ImageViewer extends JFrame {

	// to hold image
	private BufferedImage currentImage;

	public static final long serialVersionUID = 1;

	// constructor for buffered image
	public ImageViewer(BufferedImage bimg) {
		setTitle("ImageViewer");
		currentImage = bimg;
		init();
	}

	// constructor for filename
	public ImageViewer(String fileName) throws Exception {
		setTitle("ImageViewer - " + fileName);
		// get file
		FileInputStream fis = new FileInputStream(fileName);
		// convert jpec to buffered image
		JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
		currentImage = decoder.decodeAsBufferedImage();
		init();
	}

	public void init() {

		int w = currentImage.getWidth();
		int h = currentImage.getHeight();
		ImagePanel imagePanel = new ImagePanel(w, h);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// set size of the window
		setSize(w + 8, h + 35);
		// add imagePanel
		getContentPane().add(imagePanel, BorderLayout.CENTER);
		// make visible
		setVisible(true);
		// in case this is overloaded later
		imagePanel.setImage(currentImage);
	}

	public static void main(String[] args) {
		try {
		    ImageViewer imageViewer = new ImageViewer("possums1.jpg");	    
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

}


Results

Compilation warns about Sun code that may not exist in future releases.

C:\ece538\java_image>javac ImageViewer.java
ImageViewer.java:30: warning: com.sun.image.codec.jpeg.JPEGCodec is Sun propriet
ary API and may be removed in a future release
import com.sun.image.codec.jpeg.JPEGCodec;
                               ^
ImageViewer.java:31: warning: com.sun.image.codec.jpeg.JPEGImageDecoder is Sun p
roprietary API and may be removed in a future release
import com.sun.image.codec.jpeg.JPEGImageDecoder;
                               ^
ImageViewer.java:53: warning: com.sun.image.codec.jpeg.JPEGImageDecoder is Sun p
roprietary API and may be removed in a future release
                JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
                ^
ImageViewer.java:53: warning: com.sun.image.codec.jpeg.JPEGCodec is Sun propriet
ary API and may be removed in a future release
                JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
                                           ^
4 warnings


Maintained by John Loomis, updated Thu Nov 29 14:22:54 2012