2022년 2월 19일 토요일

Java Swing - ImageView

 Java Swing - ImageView


package jlib5.swing;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
 
public class ImageView {
 
    JScrollPane scroll;
    JLabel label;
    BufferedImage image;
    String scaleType = "original";
    float zoom = 1.0f;
    
    public ImageView() {
        label = new JLabel();
        scroll = new JScrollPane(label); 
        DragScrollHandler.createDragScrollHandlerFor(label);    

        scroll.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                if( scaleType.equals("fit") || scaleType.equals("width") || scaleType.equals("height") ) {
                    updateImage(scaleType);
                } else {
                    updateImage(zoom);
                }
            }
        });        
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new LineBorder(Color.WHITE, 1));              
    }
    
    public ImageView(File file) {
        this();
        loadImage( file );
    }
    
    public JScrollPane getLayout() { return scroll; }
    public JLabel getCtrl() { return label; }
    
    public void showVScrollBar() {
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    }
    public void hideVScrollBar() {
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    }
    public void showHScrollBar() {
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }
    public void hideHScrollBar() {
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }
    
    void updateImage(float ratio) {
        System.out.println("updateImage " + ratio);
        this.zoom = ratio;
        if( image != null ) {
            Image dimg = image.getScaledInstance((int)(ratio*image.getWidth()), (int)(ratio*image.getHeight()), Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(dimg);
            label.setIcon(icon);
        }   
    }
    public void updateImage(String scaleType) {
        this.scaleType = scaleType;
        float ratio_w = (float)(scroll.getWidth()-20) / image.getWidth();
        float ratio_h = (float)scroll.getHeight() / image.getHeight();
        if( this.scaleType.equals("fit") ) { zoom = ratio_w > ratio_h ? ratio_h : ratio_w; }
        if( this.scaleType.equals("width") ) { zoom = ratio_w ; }
        if( this.scaleType.equals("height") ) { zoom = ratio_h; }
        if( this.scaleType.equals("original") ) { zoom = 1.0f; }
        if( this.scaleType.equals("increase") ) { zoom += 0.1f; }
        if( this.scaleType.equals("decrease") ) { zoom -= 0.1f; }        
        updateImage(zoom);
    }
    
    public void loadImage(File file) {
        try {
            image = ImageIO.read(file);
            updateImage(zoom);
        } catch (IOException ignore) {
        }
    }

    public void setImage(BufferedImage image) {
        this.image = image;
        this.scaleType = "";
        updateImage(zoom);
    }
    
    public void setImage(byte[] image) {
        try {
            setImage(ImageIO.read(new ByteArrayInputStream(image)));
        } catch (IOException ignore) {
        }
    }
        
    public static void main(String[] args) throws Exception {
        ImageView imageView = new ImageView(new File("d:\\a.png"));
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(imageView.scroll);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}


class DragScrollHandler extends MouseAdapter {
 
    private JComponent component;
    private Point pressed, here;
    private Rectangle visiRect;

    public DragScrollHandler(JComponent component) {
        this.component = component;
        component.addMouseListener(this);
        component.addMouseMotionListener(this);
    }

    public static void createDragScrollHandlerFor(JComponent component) {
        new DragScrollHandler(component);
    }

    public void dispose() {
        component.removeMouseListener(this);
        component.removeMouseMotionListener(this);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        pressed = e.getPoint();
        visiRect = component.getVisibleRect();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        here = e.getPoint();
        visiRect.x += (pressed.x - here.x);
        visiRect.y += (pressed.y - here.y);
        component.scrollRectToVisible(visiRect);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Rectangle newRect = component.getVisibleRect();
                pressed.x += newRect.x - visiRect.x;
                pressed.y += newRect.y - visiRect.y;
                visiRect = newRect;
            }
        });
    }
}

댓글 없음:

댓글 쓰기