Here you can find the source of displayImage(final BufferedImage bufferedImage, final String title)
public static void displayImage(final BufferedImage bufferedImage, final String title)
//package com.java2s; /*/*from w w w . j av a 2 s . com*/ * Copyright 2006-2017 ICEsoft Technologies Canada Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the * License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import javax.swing.*; import java.awt.*; import java.awt.image.*; public class Main { public static void displayImage(final BufferedImage bufferedImage, final String title) { if (bufferedImage == null) { return; } SwingUtilities.invokeLater(new Runnable() { public void run() { final JFrame f = new JFrame("Image - " + title); f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); final int width = (int) (bufferedImage.getWidth() * 1.2); final int height = (int) (bufferedImage.getHeight() * 1.2); JComponent image = new JComponent() { @Override public void paint(Graphics g_) { super.paint(g_); g_.setColor(Color.green); g_.fillRect(0, 0, 10000, 10000); g_.drawImage(bufferedImage, 0, 0, f); g_.setColor(Color.red); g_.drawRect(0, 0, bufferedImage.getWidth() - 2, bufferedImage.getHeight() - 2); } }; image.setPreferredSize(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight())); image.setSize(new Dimension(width, height)); JPanel test = new JPanel(); test.setPreferredSize(new Dimension(width, height)); JScrollPane tmp = new JScrollPane(image); tmp.revalidate(); f.setSize(new Dimension(width, height)); f.getContentPane().add(tmp); f.validate(); f.setVisible(true); } }); } }