Jarファイル内のリソースをストリームとして読み込む

戻る
Jarアーカイブ内に仕込んだ画像ファイルを読み込む方法です。


::::::::::::::
BunreiTemplateFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;

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

import java.awt.image.*;

import javax.imageio.*;

import java.util.*;
import java.net.*;
import java.io.*;

public class BunreiTemplateFrame extends JFrame {

    public BunreiTemplateFrame() {
        super( "文例テンプレート" );

        ImagePanel panel = new ImagePanel( "BunreiTemplate.png" );

        Container container = getContentPane();
        container.add( panel );

        setVisible( true );
        // setBounds( 0,0, 300,400);
        pack();

        this.addWindowListener( new WindowAdapter() {
                                    public void windowClosing( WindowEvent e ) {
                                        dispose();
                                    }
                                }
                              );

    }

    public static void main ( String[] args ) {
        new BunreiTemplateFrame();

    }
}
::::::::::::::
DataIFFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;

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

import java.awt.image.*;

import javax.imageio.*;

import java.util.*;
import java.net.*;
import java.io.*;

public class DataIFFrame extends JFrame {

    public DataIFFrame() {
        super( "データI/F" );

        ImagePanel panel = new ImagePanel( "DataIF.png" );

        Container container = getContentPane();
        container.add( panel );

        setVisible( true );
        // setBounds( 0,0, 300,400);
        pack();

        this.addWindowListener( new WindowAdapter() {
                                    public void windowClosing( WindowEvent e ) {
                                        dispose();
                                    }
                                }
                              );

    }

    public static void main ( String[] args ) {
        new DataIFFrame();

    }
}
::::::::::::::
DocEntryMain.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* $Id: Resource1.html,v 1.1 2009/06/22 16:11:54 kishi Exp kishi $
*/

public class DocEntryMain extends JFrame {

    public DocEntryMain() {

        // タイトルの表示
        setTitle( "文書の作成および編集" );

        try {
            // Windows風の外観に設定します
            UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );

            // 外観を変更します
            SwingUtilities.updateComponentTreeUI( this );
        } catch ( Exception e ) {
            e.printStackTrace();
            System.exit( -1 );
        }

        //-------------------------------------------------------
        // メニュー
        //-------------------------------------------------------
        MyMenuBar menuBar = new MyMenuBar();

        //-------------------------------------------------------
        // 作業をするためのメインのPanel
        //-------------------------------------------------------
        WorkPanel wPanel = new WorkPanel();

        //-----------------------------------------------------
        // 全ての部品をContainerに配置する
        //-----------------------------------------------------
        Container content = this.getContentPane();

        content.add( menuBar, BorderLayout.NORTH );
        content.add( wPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {

        DocEntryMain editor = new DocEntryMain();

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

        editor.setBounds( 0, 0, 960, 720 );

        editor.setVisible( true );
    }

}


::::::::::::::
GazouIFFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;

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

import java.awt.image.*;

import javax.imageio.*;

import java.util.*;
import java.net.*;
import java.io.*;

public class GazouIFFrame extends JFrame {

    public GazouIFFrame() {
        super( "画像I/F" );

        ImagePanel panel = new ImagePanel( "GazouIF.png" );

        Container container = getContentPane();
        container.add( panel );

        setVisible( true );
        // setBounds( 0,0, 300,400);
        pack();

        this.addWindowListener( new WindowAdapter() {
                                    public void windowClosing( WindowEvent e ) {
                                        dispose();
                                    }
                                }
                              );

    }

    public static void main ( String[] args ) {
        new GazouIFFrame();

    }
}
::::::::::::::
GazouTemplateFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;

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

import java.awt.image.*;

import javax.imageio.*;

import java.util.*;
import java.net.*;
import java.io.*;

public class GazouTemplateFrame extends JFrame {

    public GazouTemplateFrame() {
        super( "画像テンプレート" );

        ImagePanel panel = new ImagePanel( "GazouTemplate.png" );

        Container container = getContentPane();
        container.add( panel );

        setVisible( true );
        // setBounds( 0,0, 300,400);
        pack();

        this.addWindowListener( new WindowAdapter() {
                                    public void windowClosing( WindowEvent e ) {
                                        dispose();
                                    }
                                }
                              );

    }

    public static void main ( String[] args ) {
        new GazouTemplateFrame();

    }
}
::::::::::::::
ImagePanel.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;

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

import java.awt.image.*;

import javax.imageio.*;

import java.util.*;
import java.net.*;
import java.io.*;

public class ImagePanel extends JPanel {
    private BufferedImage image = null;
    public ImagePanel( String imagePath ) {

        System.out.println( "リソース画像を読み取り中・・・" );
        try {

		
            // リソースをストリームとして読み込む
            image = ImageIO.read( getClass().getClassLoader().getResourceAsStream( imagePath ) );
		

        } catch ( IOException ex ) {
            ex.printStackTrace();
        }
        System.out.println( "完了しました!" );

        setPreferredSize( new Dimension( image.getWidth(), image.getHeight() ) );
    }

    public void paint( Graphics g ) {

        Graphics2D g2 = ( Graphics2D ) g;

        g2.drawImage( image, 0, 0, this );
    }

}

::::::::::::::
MyMenuBar.java
::::::::::::::
import java.util.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* $Id: Resource1.html,v 1.1 2009/06/22 16:11:54 kishi Exp kishi $
*/

public class MyMenuBar extends JMenuBar {

    public MyMenuBar() {
        super();

        JMenu file = new JMenu( "ファイル" );
        JMenu edit = new JMenu( "編集" );
        JMenu data = new JMenu( "データ" );
        JMenu draw = new JMenu( "描画" );

        // 各メニューに対してのアクションコマンドを指定
        Action action = new MyMenuBarAction();

        addMenuItem( file, action, "保存", "SAVE" );
        addMenuItem( file, action, "中断", "SUSPEND" );
        addMenuItem( file, action, "破棄", "DISPOSE" );
        addMenuItem( file, action, "終了", "EXIT" );

        addMenuItem( edit, action, "コピー", "COPY" );
        addMenuItem( edit, action, "貼り付け", "PASTE" );
        addMenuItem( edit, action, "削除", "DELETE" );
        addMenuItem( edit, action, "拡大", "ZOOM IN" );
        addMenuItem( edit, action, "縮小", "ZOOM OUT" );
        addMenuItem( edit, action, "元のサイズ", "ORIGINAL SIZE" );

        addMenuItem( data, action, "文例テンプレート", "SENTENCE TEMPLATE" );
        addMenuItem( data, action, "画像テンプレート", "IMAGE TEMPLATE" );
        addMenuItem( data, action, "データI/F", "DATA I/F" );
        addMenuItem( data, action, "画像I/F", "IMAGE I/F" );

        addMenuItem( draw, action, "直線", "LINE" );
        addMenuItem( draw, action, "四角", "REGTANGLE" );
        addMenuItem( draw, action, "楕円", "OVAL" ); // 楕円は円のスーパーセット
        addMenuItem( draw, action, "フリー", "FREE" );
        addMenuItem( draw, action, "太さ", "THICKNESS" );
        addMenuItem( draw, action, "色", "COLOR" );

        this.add( file );
        this.add( edit );
        this.add( data );
        this.add( draw );
    }

    private void addMenuItem( JMenu menu, Action action, String description, String commandName ) {
        JMenuItem item = new JMenuItem( description );
        menu.add( item );
        item.setActionCommand( commandName );
        item.addActionListener( action );
    }

}
::::::::::::::
MyMenuBarAction.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.net.*;

/**
* $Id: Resource1.html,v 1.1 2009/06/22 16:11:54 kishi Exp kishi $
*/

public class MyMenuBarAction extends AbstractAction {

    public MyMenuBarAction() {
        super();
    }

    public void actionPerformed( ActionEvent event ) {
        String command = event.getActionCommand();

        // 実行されたコマンドをダンプします
        System.out.println( command );

        if ( command.equals( "EXIT" ) ) {
            System.exit( 0 );
        }
        if ( "SENTENCE TEMPLATE".equals( command ) ) {
            JFrame frame = new BunreiTemplateFrame();
        }
        if ( "IMAGE TEMPLATE".equals( command ) ) {
            JFrame frame = new GazouTemplateFrame();
        }
        if ( "DATA I/F".equals( command ) ) {
            JFrame frame = new DataIFFrame();
        }
        if ( "IMAGE I/F".equals( command ) ) {
            JFrame frame = new GazouIFFrame();
        }
    }

}

::::::::::::::
WorkPanel.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* $Id: Resource1.html,v 1.1 2009/06/22 16:11:54 kishi Exp kishi $
*/

public class WorkPanel extends JPanel {

    public WorkPanel() {
    }
}
戻る inserted by FC2 system