JLayeredPaneをJScrollPaneにはめ込む

戻る



::::::::::::::
BGPanel.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import java.awt.image.*;

import javax.imageio.*;

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

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

/**
* $Id: L-S.html,v 1.1 2009/06/22 16:11:48 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class BGPanel extends JPanel {

    private BufferedImage image = null;
    private Color bgColor = null;

    public BGPanel() {
        super();
        bgColor = new Color( 0xcc, 0xcc, 0xff );

        System.out.println( "画像URLを読み取り中・・・" );
        image = null;
        try {
            image = ImageIO.read( new URL( "http://img.yahoo.co.jp/i/jp/my/top7.gif" ) );

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

    }

    public void paint( Graphics g ) {

        int width = getWidth();
        int height = getHeight();

        g.setColor( bgColor );
        // 背景色でこのコンポーネントを全て塗りつぶす
        g.fillRect( 0 , 0 , width , height );

        Graphics2D g2 = ( Graphics2D ) g;

        g2.drawImage( image, width / 2, height / 2, this );

    }

}

::::::::::::::
LayeredPaneAndScrollPane.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: L-S.html,v 1.1 2009/06/22 16:11:48 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class LayeredPaneAndScrollPane extends JFrame {

    public LayeredPaneAndScrollPane() {
        super( "JLayeredPaneをJScrollPaneにはめ込むテスト" );

        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 );

        // LayeredPaneをコンテナに配置
        MyLayeredPane lPane = new MyLayeredPane( this.getWidth(), this.getHeight() );

        JScrollPane sPane = new JScrollPane();
        sPane.getViewport().setView( lPane );
        sPane.setPreferredSize( new Dimension( 640, 480 ) );

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

        setVisible( true );

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

    }

    private static void createAndShowGUI() {

        JFrame frame = new LayeredPaneAndScrollPane();
    }

    public static void main( String[] args ) {

        javax.swing.SwingUtilities.invokeLater( new Runnable() {
                                                    public void run() {
                                                        createAndShowGUI();
                                                    }
                                                }
                                              );

    }

}

::::::::::::::
MyLayeredPane.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: L-S.html,v 1.1 2009/06/22 16:11:48 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class MyLayeredPane extends JLayeredPane {

    public MyLayeredPane( int width, int height ) {
        super();

        System.out.println( "現在の使用しているレイアウトマネージャーは、" + getLayout() );
        System.out.println( "preferredSize: " + getPreferredSize() );

        //--- 重要 ---------------------------------------------------------------------------------------------
        // レイアウトマネージャがnull、あるいは、PreferredSizeが未設定の場合、JScrollPaneがスクロールバーを表示できないので、
        // PreferredSizeを適宜セットする
        //------------------------------------------------------------------------------------------------------
        setPreferredSize( new Dimension( width, height ) );

        // 背景となるPanel
        BGPanel bgPanel = new BGPanel();
        bgPanel.setBounds( 0, 0, width, height );

        this.add( bgPanel );
        this.setLayer( bgPanel , -1 );

        // 重ね合わせするPanel
        OverlayedPanel olPanel = new OverlayedPanel();
        olPanel.setBounds( 0, 0, width, height );

        this.add( olPanel );
        this.setLayer( olPanel , 0 );

    }

}

::::::::::::::
OverlayedPanel.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: L-S.html,v 1.1 2009/06/22 16:11:48 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class OverlayedPanel extends JPanel implements ActionListener {
    private BufferedImage offImage = null;
    private Point currentPoint;
    public OverlayedPanel() {
        super();

        setLayout( null );

        // 透明にする
        setOpaque( false );

        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 ) {
                                                 // ここは注意が必要 ⇒ BufferedImage.TYPE_INT_ARGB
                                                 // BufferedImage.TYPE_INT_RGBを使うと下のレイヤーの画像が塗り消されてしまいます
                                                 // アルファチャネルも追加しておく
                                                 offImage = new BufferedImage( getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB );
                                             }
                                             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