Crazy Java Practice第3章画像ブラウザ
15864 ワード
package com.chenjo.viewer;
import javax.swing.JFrame;
/**
*
* @author chenjo
*
*/
public class Main {
public static void main(String args[]){
ViewerFrame f = new ViewerFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package com.chenjo.viewer;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import com.chenjo.viewer.action.Action;
/**
* Action
*
* @author chenjo
*
*/
@SuppressWarnings("serial")
public class ViewerAction extends AbstractAction{
private String actionName = "";
private ViewerFrame frame = null;
// AbstractAction com.chenjo.viewer.action Action
private Action action = null;
/**
*
*
*/
public ViewerAction(){
//
super();
}
/**
*
*
* @param icon
* ImageIcon
* @param name
* Action
*/
public ViewerAction(ImageIcon icon, String actionName, ViewerFrame frame){
//
super("", icon);
this.actionName = actionName;
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent e) {
ViewerService service = ViewerService.getInstance();
Action action = getAction(this.actionName);
action.execute(service, frame);
}
/**
* actionName
* @param actionName
* @return
*/
private Action getAction(String actionName){
try{
if(this.action == null){
// Action
Action action = (Action)Class.forName(actionName).newInstance();
this.action = action;
}
return this.action;
}catch(Exception e){
return null;
}
}
}
package com.chenjo.viewer;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.File;
/**
*
* @author Jointech
*
*/
public class ViewerFileChooser extends JFileChooser{
/**
* ImageFileChooser
*/
public ViewerFileChooser(){
super();
setAcceptAllFileFilterUsed(false);
addFilter();
}
/**
* ViewerFileChooser
*
*/
public ViewerFileChooser(String currentDirectoryPath){
super(currentDirectoryPath);
setAcceptAllFileFilterUsed(false);
addFilter();
}
/**
*
*/
private void addFilter(){
this.addChoosableFileFilter(new MyFileFilter(new String[] {".BMP"}, "BMP (*.BMP)"));
this.addChoosableFileFilter(new MyFileFilter(new String[] {".JPG", ".JPEG", ".JPE", ".JFIF"}, "JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)"));
this.addChoosableFileFilter(new MyFileFilter(new String[] {".GIF"}, "GIF (*.GIF)"));
this.addChoosableFileFilter(new MyFileFilter(new String[] {".TIF", ".TIFF"}, "TIFF (*.TIF;*.TIFF)"));
this.addChoosableFileFilter(new MyFileFilter(new String[] {".PNG"}, "PNG (*.PNG)"));
this.addChoosableFileFilter(new MyFileFilter(new String[] {".ICO"}, "ICO (*.ICO)"));
this.addChoosableFileFilter(new MyFileFilter(new String[] {".BMP",
".JPG",".JPEG",".JPE",".JFIF",".GIF",".TIF", ".TIFF",
".PNG", ".ICO"}, " "));
}
class MyFileFilter extends FileFilter{
//
String[] suffArr;
String description;
public MyFileFilter(){
super();
}
/**
* MyFileFilter
*/
public MyFileFilter(String[] suffArr, String description){
super();
this.suffArr = suffArr;
this.description = description;
}
/**
*
*/
@Override
public boolean accept(File f) {
// , true
for(String s : suffArr){
if(f.getName().toUpperCase().endsWith(s)){
return true;
}
}
// , true, false
return f.isDirectory();
}
/**
*
*/
@Override
public String getDescription() {
return this.description;
}
}
}
package com.chenjo.viewer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/*
*
*
*/
@SuppressWarnings("serial")
public class ViewerFrame extends JFrame {
//
private int width = 800;
private int height = 600;
// JLabel
JLabel label = new JLabel();
ViewerService service = ViewerService.getInstance();
//
ActionListener menuListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
service.menuDo(ViewerFrame.this, e.getActionCommand());
}
};
/**
*
*/
public ViewerFrame(){
super();
// JFrame
init();
}
public void init(){
//
this.setTitle(" ");
//
this.setPreferredSize(new Dimension(width, height));
//
createMenuBar();
//
JPanel toolBar = createToolPanel();
// JFrame
this.add(toolBar, BorderLayout.NORTH);
this.add(new JScrollPane(label), BorderLayout.CENTER);
//
this.setVisible(true);
this.pack();
}
public JLabel getLabel(){
return this.label;
}
/**
*
*
* @return JPanel
*/
public JPanel createToolPanel(){
// JPanel
JPanel panel = new JPanel();
// " "
JToolBar toolBar = new JToolBar(" ");
//
toolBar.setFloatable(false);
//
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
//
String[] toolArr = {"com.chenjo.viewer.action.OpenAction",
"com.chenjo.viewer.action.LastAction",
"com.chenjo.viewer.action.NextAction",
"com.chenjo.viewer.action.BigAction",
"com.chenjo.viewer.action.SmallAction"};
for(int i=0; i<toolArr.length; i++){
ViewerAction action = new ViewerAction(new ImageIcon("img/"
+ toolArr[i] + ".gif"), toolArr[i], this);
// button
JButton button = new JButton(action);
// button
toolBar.add(button);
}
panel.add(toolBar);
return panel;
}
/**
*
*/
public void createMenuBar(){
// JMenuBar
JMenuBar menuBar = new JMenuBar();
// , menuItemArr
String[] menuArr = {" (F)", " (T)", " (H)"};
//
String[][] menuItemArr = {{" (O)", "-", " (X)"}, {" (M)", " (O)", "-", " (X)", " (P)"}, {" ", " "}};
// menuArr menuItemArr
for(int i=0; i<menuArr.length; i++){
// JMenu
JMenu menu = new JMenu(menuArr[i]);
for(int j=0; j<menuItemArr[i].length; j++){
// menuItemArr[i][j] "-"
if(menuItemArr[i][j].equals("-")){
//
menu.addSeparator();
}else{
// JMenuItem
JMenuItem menuItem = new JMenuItem(menuItemArr[i][j]);
menuItem.addActionListener(menuListener);
// JMenu
menu.add(menuItem);
}
}
// JMenuBar
menuBar.add(menu);
}
// JMenubar
this.setJMenuBar(menuBar);
}
}
package com.chenjo.viewer;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import java.util.List;
import java.util.ArrayList;
/**
*
* @author Jointech
*
*/
public class ViewerService {
private static ViewerService service = null;
// ViewerFileChooser
private ViewerFileChooser fileChooser = new ViewerFileChooser();
//
private double range = 0.2;
//
private File currentDirectory = null;
//
private List<File> currentFiles = null;
//
private File currentFile = null;
/*
*
*/
private ViewerService(){
}
/**
*
*/
public static ViewerService getInstance(){
if(service == null){
service = new ViewerService();
}
return service;
}
/**
*
*/
public void open(ViewerFrame frame){
//
if(fileChooser.showOpenDialog(frame) == ViewerFileChooser.APPROVE_OPTION){
//
this.currentFile = fileChooser.getSelectedFile();
//
String name = this.currentFile.getPath();
//
File cd = fileChooser.getCurrentDirectory();
//
if(cd != this.currentDirectory || this.currentDirectory == null){
// fileChooser FileFilter
FileFilter[] fileFilters = fileChooser.getChoosableFileFilters();
File[] files = cd.listFiles();
this.currentFiles = new ArrayList<File>();
for(File file : files){
for(FileFilter filter : fileFilters){
if(filter.accept(file)){
//
if(!currentFiles.contains(file)){
// currentFiles
this.currentFiles.add(file);
}
}
}
}
}
ImageIcon icon = new ImageIcon(name);
frame.getLabel().setIcon(icon);
}
}
/**
*
*/
public void zoom(ViewerFrame frame, boolean isEnlarge){
//
double enlargeRange = isEnlarge ? (1+range) : (1-range);
//
ImageIcon icon = (ImageIcon)frame.getLabel().getIcon();
if(icon != null){
//
int width = (int)(icon.getIconWidth() * enlargeRange);
ImageIcon newIcon = new ImageIcon(icon.getImage()
.getScaledInstance(width, -1, Image.SCALE_DEFAULT));
//
frame.getLabel().setIcon(newIcon);
}
}
public void last(ViewerFrame frame){
//
if(this.currentFiles != null && !this.currentFiles.isEmpty()){
int index = this.currentFiles.indexOf(this.currentFile);
//
if(index > 0){
File file = (File)this.currentFiles.get(index - 1);
ImageIcon icon = new ImageIcon(file.getPath());
frame.getLabel().setIcon(icon);
this.currentFile = file;
}
}
}
public void next(ViewerFrame frame){
//
if(this.currentFiles != null && !this.currentFiles.isEmpty()){
int index = this.currentFiles.indexOf(this.currentFile);
//
if(index+1 < this.currentFiles.size()){
File file = (File)this.currentFiles.get(index + 1);
ImageIcon icon = new ImageIcon(file.getPath());
frame.getLabel().setIcon(icon);
this.currentFile = file;
}
}
}
/**
*
* @param frame
* @param cmd
*/
public void menuDo(ViewerFrame frame, String cmd){
if(cmd.equals(" (O)")){
open(frame);
}
if(cmd.equals(" (M)")){
zoom(frame, true);
}
if(cmd.equals(" (O)")){
zoom(frame, false);
}
if(cmd.equals(" (X)")){
last(frame);
}
if(cmd.equals(" (P)")){
next(frame);
}
if(cmd.equals(" (X)")){
System.exit(0);
}
}
}
package com.chenjo.viewer.action;
import com.chenjo.viewer.ViewerFrame;
import com.chenjo.viewer.ViewerService;
/**
* Action
* @author Jointech
*
*/
public interface Action {
/**
*
* @param service
* @param frame
*/
void execute(ViewerService service, ViewerFrame frame);
}
package com.chenjo.viewer.action;
import com.chenjo.viewer.ViewerFrame;
import com.chenjo.viewer.ViewerService;
/**
* Action
* @author Jointech
*
*/
public class BigAction implements Action {
@Override
public void execute(ViewerService service, ViewerFrame frame) {
service.zoom(frame, true);
}
}
package com.chenjo.viewer.action;
import com.chenjo.viewer.ViewerFrame;
import com.chenjo.viewer.ViewerService;
/**
* Action
* @author Jointech
*
*/
public class LastAction implements Action {
public void execute(ViewerService service, ViewerFrame frame){
service.last(frame);
}
}
package com.chenjo.viewer.action;
import com.chenjo.viewer.ViewerFrame;
import com.chenjo.viewer.ViewerService;
/**
* Action
* @author Jointech
*
*/
public class NextAction implements Action {
@Override
public void execute(ViewerService service, ViewerFrame frame) {
service.next(frame);
}
}
package com.chenjo.viewer.action;
import com.chenjo.viewer.ViewerFrame;
import com.chenjo.viewer.ViewerService;
/**
* Action
* @author Jointech
*
*/
public class OpenAction implements Action {
public void execute(ViewerService service, ViewerFrame frame){
service.open(frame);
}
}
package com.chenjo.viewer.action;
import com.chenjo.viewer.ViewerFrame;
import com.chenjo.viewer.ViewerService;
/**
* Action
* @author Jointech
*
*/
public class SmallAction implements Action {
@Override
public void execute(ViewerService service, ViewerFrame frame) {
service.zoom(frame, false);
}
}