Here you can find the source of showImage(BufferedImage image, String title, boolean fitToScreen)
public static JFrame showImage(BufferedImage image, String title, boolean fitToScreen)
//package com.java2s; /*/*from w w w .j av a 2 s . c o m*/ * ========================================================================= * This file is part of NITRO * ========================================================================= * * (C) Copyright 2004 - 2010, MDA Information Systems LLC * * NITRO is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 3 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, If not, * see <http://www.gnu.org/licenses/>. */ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Image; import java.awt.Toolkit; import java.awt.Window; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; public class Main { public static JFrame showImage(BufferedImage image, String title) { return showImage(image, title, true); } public static JFrame showImage(BufferedImage image, String title, boolean fitToScreen) { JFrame frame = new JFrame(title != null ? title : ""); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Image im = image; if (fitToScreen) { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); if (imageHeight > screen.height || imageWidth > screen.width) { double hRatio = (imageHeight - screen.height) / screen.height; double wRatio = (imageWidth - screen.width) / screen.width; int w = -1; int h = -1; if (hRatio > wRatio) h = screen.height; else w = screen.width; im = image.getScaledInstance(w, h, Image.SCALE_DEFAULT); } } JLabel label = new JLabel(new ImageIcon(im)); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); centerWindow(frame); frame.setVisible(true); return frame; } public static void centerWindow(Window w) { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension window = w.getSize(); if (window.width == 0) return; int left = screen.width / 2 - window.width / 2; int top = (screen.height - window.height) / 4; if (top < 0) top = 0; w.setLocation(left, top); } }