⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageviewer.java

📁 Java版本的屏幕截图 工具,可以自己放到系统托盘使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    private void updateZoomActionState() {
        setZoomInEnabled(getImageComponent().canZoomIn());
        setZoomOutEnabled(getImageComponent().canZoomOut());
    }

    private boolean isImageModified() {
        return imageModified;
    }

    private void setImageModified(boolean imageModified) {
        this.imageModified = imageModified;
    }

    private boolean confirmSaveImage() {
        String title = resource.getString(ImageViewer.class.getName() + ".Confirm.SaveImageDialog.Title");
        String message = resource.getString(ImageViewer.class.getName() + ".Confirm.SaveImageDialog.Message");
        int selectedValue = JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_CANCEL_OPTION);
        switch (selectedValue) {
            case JOptionPane.YES_OPTION:
                return save();
            case JOptionPane.NO_OPTION:
                return true;
            default:
                return false;
        }
    }

    public void setImage(BufferedImage image) {
        getImageComponent().setImage(image);
        setImageModified(true);
    }

    public boolean setImage(File imageFile) {
        try {
            getImageComponent().setImage(imageFile);
            if (imageFile != null) {
                this.imageFile = imageFile;
                updateTitle(imageFile.getName());
            }
        } catch (Exception e) {
            String title = resource.getString(ImageViewer.class.getName() + ".Error.OpenImageDialog.Title");
            String message = resource.getString(ImageViewer.class.getName() + ".Error.OpenImageDialog.Message");
            JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
            return false;
        }

        setImageModified(false);
        return true;
    }

    public void setZoomInEnabled(boolean zoomInEnabled) {
        boolean oldValue = this.zoomInEnabled;
        this.zoomInEnabled = zoomInEnabled;
        firePropertyChange(ZOOM_IN_PROPERTY, oldValue, this.zoomInEnabled);
    }

    public void setZoomOutEnabled(boolean zoomOutEnabled) {
        boolean oldValue = this.zoomOutEnabled;
        this.zoomOutEnabled = zoomOutEnabled;
        firePropertyChange(ZOOM_OUT_PROPERTY, oldValue, this.zoomOutEnabled);
    }

    public boolean close() {
        if (isImageModified() && !confirmSaveImage())
            return false;

        getImageComponent().dispose();
        setVisible(false);
        dispose();

        return true;
    }

    public boolean save() {
        ImageFileChooser fileChooser = new ImageFileChooser(this);
        String imageFileName = imageFile != null && imageFile.exists() ? imageFile.getAbsolutePath() : null;
        File imageFile = fileChooser.saveImageFile(imageFileName);
        if (imageFile != null) {
            try {
                getImageComponent().saveImage(imageFile);
                setImageModified(false);

                ApplicationSettings applicationSettings = ApplicationSettings.getInstance();
                int templateNumber = applicationSettings.getTemplateNumber();
                applicationSettings.setTemplateNumber(++templateNumber);

                return true;
            } catch (Exception e) {
                String title = resource.getString(ImageViewer.class.getName() + ".Error.SaveImageDialog.Title");
                String message = resource.getString(ImageViewer.class.getName() + ".Error.SaveImageDialog.Message");
                JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        return false;
    }

    private class AboutAction extends Actions.ApplicationAction {
        private AboutAction() {
            super("About", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    AboutDialog aboutDialog = AboutDialog.getInstance();
                    aboutDialog.setLocationRelativeTo(ImageViewer.this);
                    aboutDialog.setModal(true);
                    aboutDialog.setVisible(true);
                    aboutDialog.toFront();
                }
            });
        }
    }

    private class SettingsAction extends Actions.ApplicationAction {
        private SettingsAction() {
            super("Settings", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    SettingsDialog settingsDialog = SettingsDialog.getInstance();
                    settingsDialog.setLocationRelativeTo(ImageViewer.this);
                    settingsDialog.setModal(true);
                    settingsDialog.setVisible(true);
                    settingsDialog.toFront();
                }
            });
        }
    }

    private class CloseAction extends Actions.ApplicationAction {
        private CloseAction() {
            super("Close", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            close();
        }
    }

    private class SaveAction extends Actions.ApplicationAction {
        private SaveAction() {
            super("Save", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    save();
                }
            });
        }
    }

    private class CopyAction extends Actions.ApplicationAction {
        private CopyAction() {
            super("Copy", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            CaptureOperations.getInstance().copyToClipboard(getImageComponent().getImage());
        }
    }

    private class ZoomInAction extends Actions.ApplicationAction {
        private ZoomInAction() {
            super("ZoomIn", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            ImageComponent component = getImageComponent();
            if (component.canZoomIn()) component.zoomIn();
            updateZoomActionState();
        }
    }

    private class ZoomOutAction extends Actions.ApplicationAction {
        private ZoomOutAction() {
            super("ZoomOut", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            ImageComponent component = getImageComponent();
            if (component.canZoomOut()) component.zoomOut();
            updateZoomActionState();
        }
    }

    private class ZoomToAction extends Actions.ApplicationAction {
        private ZoomToAction() {
            super("ZoomTo", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            zoomToPopupMenu.show(zoomToButton, 0, zoomToButton.getBounds().height);
        }
    }

    private class ActualSizeAction extends Actions.ApplicationAction {
        private ActualSizeAction() {
            super("ActualSize", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            getImageComponent().actualSize();
            updateZoomActionState();
        }
    }

    private class FitToWindowAction extends Actions.ApplicationAction {
        private FitToWindowAction() {
            super("FitToWindow", ImageViewer.class);
        }

        public void actionPerformed(ActionEvent e) {
            ImageComponent component = getImageComponent();
            Dimension parentContainerSize = component.getParent().getSize();
            component.fitToWindow(parentContainerSize);
        }
    }

    private class PercentAction extends AbstractAction {
        private int index = 0;

        public PercentAction(int percent, int index) {
            this.index = index;
            putValue(javax.swing.Action.NAME, percent + " %");
            putValue(javax.swing.Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(String.valueOf(index + 1)));
        }

        public void actionPerformed(ActionEvent e) {
            getImageComponent().zoomTo(index);
            updateZoomActionState();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -