//************************************************************* // ImageIOのとりあえずの使い方 // 「ImageOutputTest」 // //          作 成 者:ルート高菜    //          作成開始月:2009/11 //          最終更新月:2009/11 [TN124/J114] //************************************************************* import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; public class ImageOutputTest extends JPanel implements ActionListener,MouseMotionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; //キャンパスとなる画像 BufferedImage image=null; Graphics imageG=null; // JButton buttonMakeBack=new JButton("背景パネルを作成"); JButton buttonInputImage=new JButton("背景画像を選択"); JButton buttonOutputImage=new JButton("JPEGで画像保存"); // //------------------------------------------------------------------ // public static void main(String[] args) { JFrame frame=new JFrame(); frame.getContentPane().add(new ImageOutputTest()); frame.setTitle("ImageOutputTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setSize(500,500); frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } //------------------------------------------------------------------コンストラクタ // public ImageOutputTest() { // add(buttonMakeBack); add(buttonInputImage); add(buttonOutputImage); // buttonMakeBack.addActionListener(this); buttonInputImage.addActionListener(this); buttonOutputImage.addActionListener(this); addMouseMotionListener(this); } //------------------------------------------------------------------描画 // public void paintComponent(Graphics g){ super.paintComponent(g); //キャンパスとなる画像を置くだけ if(image!=null){ g.drawImage(image, 0, 0, this); } } //------------------------------------------------------------------ボタンイベント // public void actionPerformed(ActionEvent e) { // Object object=e.getSource(); // if(object==buttonMakeBack){ //キャンパスとなる画像を自分で作成(真っ黒になるようだ) image=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB); imageG=image.getGraphics(); // repaint(); }else if(object==buttonInputImage){ //キャンパスとなる画像をロードする JFileChooser chooser=new JFileChooser(); if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){ File file=chooser.getSelectedFile(); try{ image=ImageIO.read(file); imageG=image.getGraphics(); }catch(IOException ioe){ image=null; imageG=null; } // repaint(); } }else if(object==buttonOutputImage){ //キャンパスをJPEG画像として保存 JFileChooser chooser=new JFileChooser(); if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){ File file=chooser.getSelectedFile(); try{ ImageIO.write(image, "jpeg", file); }catch(IOException ioe){ } } } } //------------------------------------------------------------------マウスイベント // public void mouseDragged(MouseEvent e) { if(image!=null){ //キャンパスとなる画像に直接描画する(ドラッグしたら赤い線を引く) imageG.setColor(Color.RED); imageG.fillOval(e.getX(), e.getY(), 4, 4); repaint(); } } public void mouseMoved(MouseEvent arg0) { } }