import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import javax.imageio.*; import javax.swing.*; public class ImageCompositer implements ActionListener{ public static void main(String[] args){ ImageCompositer ic=new ImageCompositer(); ic.show(); } private static final FileFilter FILTER=new Filter(); private JFrame wind; private JFileChooser dirC; private ArrayList files; private DefaultListModel listModelW; private JList listW; private DefaultListModel listModelF; private JList listF; private File wDir; private LinkedList imgs; private JCheckBox immediate; private BufferedImage res; private JProgressBar bar; private JLabel imgL; private JFileChooser saver; public ImageCompositer(){ wind=new JFrame(); wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initData(); initGUI(); } private void initGUI(){ Container cp=wind.getContentPane(); cp.setLayout(new BorderLayout()); JPanel tp=new JPanel(); tp.setLayout(new GridLayout(1,2)); JSplitPane spp = new JSplitPane(JSplitPane.VERTICAL_SPLIT); listW=new JList(listModelW); JScrollPane lsp=new JScrollPane(listW); spp.setTopComponent(lsp); listF=new JList(listModelF); JScrollPane fsp=new JScrollPane(listF); spp.setBottomComponent(fsp); JPanel btnP=new JPanel(); btnP.setLayout(new BoxLayout(btnP,BoxLayout.Y_AXIS)); String[] btnName=Command.getAll(); for(String c:btnName){ JButton bt=new JButton(c); bt.addActionListener(this); bt.setActionCommand(c); btnP.add(bt); } immediate=new JCheckBox("すぐに反映"); immediate.setSelected(true); bar=new JProgressBar(); bar.setValue(0); btnP.add(immediate); btnP.add(bar); tp.add(spp); tp.add(btnP); cp.add(tp,BorderLayout.EAST); imgL=new JLabel(); cp.add(imgL,BorderLayout.CENTER); wind.pack(); } private void initData(){ dirC=new JFileChooser(); dirC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); saver=new JFileChooser(); files=new ArrayList(); imgs=new LinkedList(); listModelW=new DefaultListModel(); listModelF=new DefaultListModel(); res=null; } public void show(){ wind.setVisible(true); } private void getFileList(){ int fres=dirC.showOpenDialog(null); if(fres!=JFileChooser.APPROVE_OPTION){ return; } wDir=dirC.getSelectedFile(); listModelF.removeAllElements(); files.clear(); File[] fls=wDir.listFiles(FILTER); for(File f:fls){ listModelF.addElement(f.getName()); files.add(f); } saver.setSelectedFile(new File(wDir,"img.png")); } private void addImage(){ int ids[]=listF.getSelectedIndices(); if(ids.length<1){ return; } for(int index:ids){ File file=files.get(index); BufferedImage img=null; try { img=ImageIO.read(file); } catch (Exception e) { return; } if(img==null){ return; } imgs.add(img); listModelW.addElement(file.getName()); } if(immediate.isSelected()){ composite(); } } private void composite(){ int w=0,h=0; if(listModelW.size()<1){ return; } for(BufferedImage b:imgs){ if(b.getHeight()>h){ h=b.getHeight(); } if(b.getWidth()>w){ w=b.getWidth(); } } res=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB); bar.setMaximum(h); float[] temp=new float[4]; float[] pix=new float[4]; int len=imgs.size(); for(int y=0;y=listModelW.size()||index+next<0){ return; } BufferedImage tempi=imgs.remove(index); String temps=(String) listModelW.remove(index); imgs.add(index+next,tempi); listModelW.add(index+next,temps); if(immediate.isSelected()){ composite(); } listW.setSelectedIndex(index+next); } private void save(){ if(res==null){ return; } File sfile=null; while(true){ int fres=saver.showSaveDialog(null); if(fres!=JFileChooser.APPROVE_OPTION){ return; } sfile=saver.getSelectedFile(); if(sfile.exists()){ if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(null,"ファイルが既に存在します\n上書きしますか?")){ break; } }else{ break; } } try { ImageIO.write(res,"png",sfile); } catch (IOException e) { JOptionPane.showMessageDialog(null,"保存に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); } JOptionPane.showMessageDialog(null,"保存しました。"); } private void showImg() { int index=listF.getSelectedIndex(); if(index<0){ return; } File file=files.get(index); BufferedImage img=null; try { img=ImageIO.read(file); } catch (Exception e) { return; } if(img==null){ return; } int res=JOptionPane.showConfirmDialog(null,new ImageIcon(img),"画像を追加する",JOptionPane.YES_NO_OPTION,JOptionPane.DEFAULT_OPTION); if(res==JOptionPane.YES_OPTION){ addImage(); } } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand()==Command.SEL_DIR.getCommand()){ getFileList(); } if(e.getActionCommand()==Command.ADD_IMG.getCommand()){ addImage(); } if(e.getActionCommand()==Command.COMPOSITE.getCommand()){ composite(); } if(e.getActionCommand()==Command.REMOVE.getCommand()){ remove(); } if(e.getActionCommand()==Command.REMOVEALL.getCommand()){ removeAll(); } if(e.getActionCommand()==Command.SAVE.getCommand()){ save(); } if(e.getActionCommand()==Command.UPPER.getCommand()){ flip(1); } if(e.getActionCommand()==Command.LOWER.getCommand()){ flip(-1); } if(e.getActionCommand()==Command.SHOW.getCommand()){ showImg(); } } } class Filter implements FileFilter{ @Override public boolean accept(File f) { return f.isFile(); } } enum Command{ SEL_DIR("ディレクトリを選択"), SHOW("画像を見る"), ADD_IMG("画像を追加"), REMOVE("画像を削除"), REMOVEALL("画像をすべて削除"), LOWER("画像を下に移動"), UPPER("画像を上に移動"), COMPOSITE("合成する"), SAVE("保存する"); private String command; Command(String c){ command=c; } public String getCommand(){ return command; } public static String[] getAll(){ Command[] cms=Command.values(); String[] ret=new String[cms.length]; for(int i=0;i>>8)&0x000000ff)/255.0f; ret[0]=((co>>>16)&0x000000ff)/255.0f; ret[3]=((co>>>24)&0x000000ff)/255.0f; } public static int f2i(float[] co){ if(co[0]>1){ co[0]=1; } if(co[1]>1){ co[1]=1; } if(co[2]>1){ co[2]=1; } if(co[3]>1){ co[3]=1; } if(co[0]<0){ co[0]=0; } if(co[1]<0){ co[1]=0; } if(co[2]<0){ co[2]=0; } if(co[3]<0){ co[3]=0; } int b=(int)(co[2]*255); int g=(int)(co[1]*255); int r=(int)(co[0]*255); int a=(int)(co[3]*255); int ret=(a<<24)|(r<<16)|(g<<8)|(b); return ret; } }