Java Swing - ImageViewer
package jlib5.app; import jlib5.swing.Dock; import jlib5.swing.Drop; import jlib5.swing.ImageView; import com.formdev.flatlaf.FlatLightLaf; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.Image; import java.awt.event.ActionEvent; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import net.infonode.docking.View; public final class ImageViewer { Dock dock; javax.swing.JFrame frame; String imageTitle = "imageView"; ImageView imageView; public static void main(String[] args) throws Exception { FlatLightLaf.setup(); SwingUtilities.invokeLater(new Runnable() { public void run() { new ImageViewer(); } }); } ImageViewer() { createViews(); showFrame(); } final void createViews() { dock = new Dock(); imageView = new ImageView(); View imgview = dock.addToViewMap(imageTitle, null, imageView.getLayout()); dock.createRootWindow(); dock.getRootWindow().setWindow(dock.getTab(imgview)); imageView.getCtrl().setTransferHandler(new Drop(this::fileDropHandler) ); } void showFrame() { frame = new JFrame("ImageViewer"); frame.getContentPane().add(createToolBar(), BorderLayout.NORTH); frame.getContentPane().add(dock.getRootWindow(), BorderLayout.CENTER); frame.getContentPane().add(new JLabel("Ready"), BorderLayout.SOUTH); frame.setJMenuBar(createMenuBar()); frame.setSize(900, 700); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } //--------------------------------------------------------------------------- boolean fileDropHandler(Object...obj) { java.util.List<File> files = (java.util.List<File>) obj[0]; imageView.loadImage(files.get(0)); return true; } void fileOpen() { imageView.loadImage(fileOpenDialog(frame)); } //--------------------------------------------------------------------------- public File fileOpenDialog(Frame frame) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); int result = fileChooser.showOpenDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { return fileChooser.getSelectedFile(); } return null; } JButton getButton(String text, Image image, Runnable handler) { JButton button = new JButton(); if( text != null ) { button.setText(text); } if( image != null ) { button.setIcon(new ImageIcon(image)); } if( handler != null ) { button.addActionListener( (e) -> handler.run() ); } return button; } JToolBar createToolBar() { JToolBar toolBar = new JToolBar(); toolBar.add( getButton("Open",null,this::fileOpen) ); toolBar.add( getButton("(0)",null,() -> { imageView.updateImage("original"); } ) ); toolBar.add( getButton("(+)",null,() -> { imageView.updateImage("increase"); } ) ); toolBar.add( getButton("(-)",null,() -> { imageView.updateImage("decrease"); } ) ); toolBar.add( getButton("(F)",null,() -> { imageView.updateImage("fit"); } ) ); toolBar.add( getButton("(W)",null,() -> { imageView.updateImage("width"); } ) ); toolBar.add( getButton("(H)",null,() -> { imageView.updateImage("height"); } ) ); return toolBar; } JMenuBar createMenuBar() { JMenuBar menu = new JMenuBar(); menu.add(createFileMenu()); return menu; } JMenu createFileMenu() { JMenu menu = new JMenu("File"); JMenuItem item = new JMenuItem("Exit"); menu.add(item).addActionListener((ActionEvent e) -> { System.exit(0); }); return menu; } }
댓글 없음:
댓글 쓰기