パネル上で編集したイメージを保存する

戻る





::::::::::::::
DrawingTest.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;

import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
* $Id: SavingImage.html,v 1.1 2009/06/22 16:11:56 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class DrawingTest extends JFrame {

    public DrawingTest () {
        super( "画像のファイルへの書き出しテスト" );

        try {
            // 外観を設定します
            UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
            // 外観を変更します
            SwingUtilities.updateComponentTreeUI( this );
        } catch ( Exception e ) {
            e.printStackTrace();
            System.exit( -1 );
        }

        setBounds( 0, 0, 640, 480 );

        // お絵かきパネルを追加
        MyDrawingPanel panel = new MyDrawingPanel();
        panel.setBounds( 0, 0, 640, 480 );
        Container container = getContentPane();
        container.add( panel );

        setVisible( true );

        addWindowListener( new WindowAdapter() {
                               public void windowClosing( WindowEvent ev ) {
                                   dispose();
                                   System.exit( 0 );
                               }
                           }
                         );

    }

    private static void createAndShowGUI() {
        JFrame frame = new DrawingTest();
    }

    public static void main( String[] args ) {
        javax.swing.SwingUtilities.invokeLater( new Runnable() {
                                                    public void run() {
                                                        createAndShowGUI();
                                                    }
                                                }
                                              );

    }

}

::::::::::::::
MyDrawingPanel.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;

import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;

import javax.imageio.*;
import java.awt.image.*;

import java.io.*;

/**
* $Id: SavingImage.html,v 1.1 2009/06/22 16:11:56 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class MyDrawingPanel extends JPanel implements ActionListener {

    private BufferedImage offImage = null;
    private Point currentPoint;

    public MyDrawingPanel() {
        super();

        setLayout( null );

        JButton saveButton = new JButton( "画像を保存" );
        saveButton.setLocation( 500, 350 );
        saveButton.setSize( 100, 50 );
        saveButton.setActionCommand( "SAVE" );
        saveButton.addActionListener( this );
        this.add( saveButton );

        this.addMouseMotionListener( new MouseMotionAdapter() {
                                         public void mouseDragged( MouseEvent e ) {
                                             Point p = e.getPoint();
                                             if ( offImage == null ) {
                                                 offImage = ( BufferedImage ) createImage( getWidth(), getHeight() );
                                             }
                                             Graphics2D g2d = ( Graphics2D ) offImage.createGraphics();

                                             g2d.setStroke( new BasicStroke( 3.0F ) );
                                             g2d.setPaint( Color.black );
                                             g2d.drawLine( currentPoint.x, currentPoint.y, p.x, p.y );
                                             g2d.dispose();

                                             repaint();
                                             currentPoint = e.getPoint();
                                         }
                                     }
                                   );

        this.addMouseListener( new MouseAdapter() {
                                   public void mousePressed( MouseEvent e ) {
                                       currentPoint = e.getPoint();
                                   }
                               }
                             );

    }

    public void paintComponent( Graphics g ) {
        super.paintComponent( g );

        if ( offImage != null ) {
            ( ( Graphics2D ) g ).drawImage( offImage, 0, 0, this );
        }
    }

    public void actionPerformed( ActionEvent event ) {
        String command = event.getActionCommand();
        if ( "SAVE".equals( command ) ) {
            System.out.println( "画像をファイルに保存しています..." );
            try {
                boolean result =
                    ImageIO.write( offImage, "png", new File( "mytest.png" ) );
                System.out.println( "画像をファイルに保存しました!" );
            } catch ( Exception e ) {
                e.printStackTrace();
            }

        }
    }

}
戻る inserted by FC2 system