showImage.javaimport java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class showImage extends JFrame {
BufferedImage img;
String title;
double scl;
public showImage(BufferedImage img, String title) {
this.img = img;
setup(title);
}
public showImage(String filename) {
readImage(filename);
setup(filename);
}
private void setup(String title) {
setScale();
this.title = title;
setTitle(title);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new ImagePanel());
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void setScale()
{
int w = img.getWidth();
int h = img.getHeight();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getScreenSize();
//System.out.format("screen size: %d x %d %n",dim.width,dim.height);
//System.out.format("image size %d x %d%n",w,h);
scl = 1.0;
if (w<128) {
scl = (double) 128/w;
}
else {
scl = Math.min((double) 800/w, (double) 600/h);
if (scl>1.0) scl = 1.0;
}
//System.out.format("scale %g%n",scl);
}
public void info() {
System.out.println("Image: " + title);
System.out.format("size: %d x %d%n",img.getWidth(),img.getHeight());
}
public void readImage(String filename) {
try {
img = ImageIO.read(new File(filename));
} catch (IOException e) {
//System.out.println(e); // e.getMessage());
System.out.println(filename + " not found");
System.exit(-1);
}
}
public void writeImage(String filename) {
writeImage(filename,"jpg");
}
public void writeImage(String filename, String format) {
File file = new File(filename + "." + format);
try {
ImageIO.write(img, format, file);
} catch (IOException e) {
System.out.println("image write failed");
System.out.println(e);
System.exit(-1);
}
}
public Dimension getPreferredSize() {
int w,h;
w = h = 100;
if (img != null) {
w = (int) (scl*img.getWidth())+16;
h = (int) (scl*img.getHeight())+38;
}
//System.out.format("preferred size: %d x %d%n",w,h);
return new Dimension(w,h);
}
class ImagePanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
if (scl==1.0) g2.drawImage(img, 0, 0, null);
else g2.drawImage(img, AffineTransform.getScaleInstance(scl, scl), null);
}
}
public static void main(String[] args) {
String filename = "binary_blobs.jpg";
if (args.length>0) filename = args[0];
showImage f1 = new showImage(filename);
f1.info();
f1.setLocation(100,100);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Maintained by John Loomis, updated Wed Oct 16 20:47:46 2013