Java BufferedImage Display display(final BufferedImage image)

Here you can find the source of display(final BufferedImage image)

Description

display

License

Open Source License

Declaration

public static void display(final BufferedImage image) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.BorderLayout;

import java.awt.Frame;

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main {
    public static void display(final BufferedImage image) {
        final JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());

        final ImageIcon imageIcon = new ImageIcon(image);
        JLabel imageLabel = new JLabel(imageIcon);
        frame.getContentPane().add(new JScrollPane(imageLabel), BorderLayout.CENTER);
        //frame.getContentPane().add(new JLabel(new ImageIcon(img)));

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1, 4));
        JButton zoomInHButton = new JButton("+H");
        JButton zoomOutHButton = new JButton("-H");
        JButton zoomInVButton = new JButton("+V");
        JButton zoomOutVButton = new JButton("-V");
        buttonPanel.add(zoomInHButton);/*from w w  w.j  a v  a2  s.  co  m*/
        buttonPanel.add(zoomOutHButton);
        buttonPanel.add(zoomInVButton);
        buttonPanel.add(zoomOutVButton);

        final AtomicInteger zoomX = new AtomicInteger(0);
        final AtomicInteger zoomY = new AtomicInteger(0);

        zoomInHButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                zoomX.set(zoomX.get() + 1);
                refreshViewer(image, zoomX, zoomY, imageIcon, frame);
            }
        });
        zoomOutHButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                zoomX.set(zoomX.get() - 1);
                refreshViewer(image, zoomX, zoomY, imageIcon, frame);
            }
        });
        zoomInVButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                zoomY.set(zoomY.get() + 1);
                refreshViewer(image, zoomX, zoomY, imageIcon, frame);
            }
        });
        zoomOutVButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                zoomY.set(zoomY.get() - 1);
                refreshViewer(image, zoomX, zoomY, imageIcon, frame);
            }
        });

        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        frame.invalidate();
    }

    private static void refreshViewer(Image img, AtomicInteger zoomX, AtomicInteger zoomY, ImageIcon icon,
            Frame frame) {
        //System.err.println(zoomX);
        //System.err.println(zoomY);
        Image newImage;
        if (zoomX.get() == 1 && zoomY.get() == 1) {
            newImage = img;
        } else {
            newImage = img.getScaledInstance((int) (img.getWidth(frame) * Math.pow(2, zoomX.get())),
                    (int) (img.getHeight(frame) * Math.pow(2, zoomY.get())), Image.SCALE_SMOOTH);
        }
        icon.setImage(newImage);
        frame.repaint();
    }
}

Related

  1. displayImage(BufferedImage image)
  2. displayImage(final BufferedImage bufferedImage, final String title)
  3. displayImage(final String windowTitle, final BufferedImage image)
  4. displayImageInWindow(BufferedImage image)