Java Swing - Comics Viewer
package jlib5.app; 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 java.io.IOException; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; 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 jlib5.java.UnZipApi; import jlib5.swing.Dock; import jlib5.swing.Drop; import jlib5.swing.ImageView; import jlib5.swing.Swing; import net.infonode.docking.View; public final class ComicsViewer { boolean useDock = false; Dock dock; javax.swing.JFrame frame; JLabel statusText = new JLabel("Ready"); String imageTitle = "imageView"; ImageView imageView; UnZipApi images = new UnZipApi(); int imagesIndex = 0; JComboBox pageSelect = Swing.getComboBox(160, this::pageSelectHandler); public static void main(String[] args) throws Exception { FlatLightLaf.setup(); SwingUtilities.invokeLater(new Runnable() { public void run() { new ComicsViewer(); } }); } ComicsViewer() { createViews(); showFrame(); } final void createViews() { imageView = new ImageView(); if( useDock ) { dock = new Dock(); 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); if( useDock ) { frame.getContentPane().add(dock.getRootWindow(), BorderLayout.CENTER); } else { frame.getContentPane().add(imageView.getLayout(), BorderLayout.CENTER); } frame.getContentPane().add(statusText, BorderLayout.SOUTH); frame.setJMenuBar(createMenuBar()); frame.setSize(900, 700); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } //--------------------------------------------------------------------------- public void setImage(int index) { try { if( images != null && index >= 0 && index < images.size() ) { imagesIndex = index; imageView.setImage(images.unzipEntry(index)); statusText.setText(" [ " + (index+1) + " / " + images.size() + " ] "); } } catch (IOException ignore) { } } void gotoPage(int page) { pageSelect.setSelectedIndex(page); } void firstPage() { gotoPage(0); } void lastPage() { if( images != null ) gotoPage(images.size()-1); } void prevPage() { gotoPage(imagesIndex-1); } void nextPage() { gotoPage(imagesIndex+1); } void pageSelectHandler() { setImage(pageSelect.getSelectedIndex()); } void loadComicsFile(File file) { if( file.isFile() ) { try { images.load(file.getAbsolutePath(), "EUC_KR"); pageSelect.removeAllItems(); for( int i = 0; i < images.size(); i++ ) { pageSelect.addItem( "[ " + (i+1) + " ] "+ images.getName(i)); } firstPage(); } catch (IOException igonre) { } } } boolean fileDropHandler(Object...obj) { java.util.List<File> files = (java.util.List<File>) obj[0]; loadComicsFile(files.get(0)); return true; } void fileOpen() { loadComicsFile(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.addSeparator(); toolBar.add( pageSelect ); toolBar.add( getButton("|<",null, this::firstPage ) ); toolBar.add( getButton("<",null, this::prevPage ) ); toolBar.add( getButton(">",null, this::nextPage ) ); toolBar.add( getButton(">|",null, this::lastPage ) ); toolBar.addSeparator(); 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; } }
댓글 없음:
댓글 쓰기