SCMを可視化する

戻る







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

public class BusinessUnit extends JPanel {

    private Material material = null;
    private JButton button = null;
    private BusinessUnit partner = null;

    public BusinessUnit() {
        super();
        setLayout( null );

        button = new JButton( "DELIVER" );
        button.setLocation( 0, 0 );
        button.setSize( 100, 25 );
        this.add( button );

        button.addActionListener( new DeliveryAction( this ) );
        button.setActionCommand( "DELIVERY" );
    }

    public void setPartner( BusinessUnit partner ) {
        this.partner = partner;
    }

    public void putMaterial() {
        material = new Material();
        this.add( material );
    }

    public void removeMaterial() {
        if ( material != null ) {
            this.remove( material );
            validate();
            repaint();
            System.out.println( "親コンポーネントは、" + getParent() );

            Material clone = null;
            try {
                // 配達するためにcloneを引き渡す
                clone = ( Material ) material.clone();
                material = null;

                Deliverer deliverer = new Deliverer();
                deliverer.init( this, partner, ( MyPanel ) getParent(), clone );
                ( new Thread( deliverer ) ).start();

            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }

    }

    public Point getStartPosition() {
        System.out.println( getLocation() );
        System.out.println( getSize() );

        Point point = getLocation();
        Dimension dimension = getSize();

        int x = ( int ) point.getX();
        int y = ( int ) point.getY();
        int width = ( int ) dimension.getWidth();
        int height = ( int ) dimension.getHeight();

        return new Point( x + width / 2, y + height );
    }

}
::::::::::::::
Deliverer.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: SCM.html,v 1.1 2009/06/22 16:11:55 kishi Exp kishi $
* @author KISHI Yasuhiro
* @version $Revision: 1.1 $
*/

public class Deliverer implements Runnable {

    private int startX = 0;
    private int endX = 0;
    private int pitch = 0;
    private MyPanel panel = null;
    private Material material = null;
    private BusinessUnit to = null;

    public void init( BusinessUnit from, BusinessUnit to, MyPanel panel, Material material ) {

        this.material = material;
        this.panel = panel;
        this.to = to;

        // パネルにマテリアルを配置
        panel.addMaterial( material );

        // 開始位置を設定
        Point startPoint = from.getStartPosition();
        int width = ( int ) material.getSize().getWidth();
        startX = ( int ) startPoint.getX() - width / 2;
        material.setLocation( startX , ( int ) startPoint.getY() );
        panel.repaint();

        // 終了位置を取得(平行移動なのでX座標だけでOK
        System.out.println( to );
        Point endPoint = to.getStartPosition();
        endX = ( int ) endPoint.getX() - width / 2;
        System.out.println( "endX=" + endX );

        pitch = ( int ) ( ( endX - startX ) / 30.0 );

    }

    public void run() {
        System.out.println( this.getClass().getName() + " started!" );
        System.out.println( "pitch=" + pitch );

        Point point = material.getLocation();
        int currentX = ( int ) point.getX();

        while ( reachedTarget( currentX, endX ) ) {

            try {
                Thread.sleep( 100 );
            } catch ( Exception e ) {
                // do nothing
            }

            currentX += pitch;
            System.out.println( "currentX=" + currentX );

            int y = ( int ) material.getLocation().getY();
            material.setLocation( currentX , y );
            panel.repaint();
        }

        to.putMaterial();

        panel.removeMaterial();
        panel.repaint();

    }

    public boolean reachedTarget( int currentX, int endX ) {
        if ( pitch < 0 ) {
            return currentX > endX;
        } else {
            return currentX <= endX ;
        }

    }

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

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

public class DeliveryAction extends AbstractAction {
    private BusinessUnit bu = null;

    public DeliveryAction( BusinessUnit bu ) {
        super();
        this.bu = bu;
    }

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

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

        bu.removeMaterial();
    }
}


::::::::::::::
Material.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: SCM.html,v 1.1 2009/06/22 16:11:55 kishi Exp kishi $
* @author KISHI Yasuhiro
* @version $Revision: 1.1 $
*/

public class Material extends JPanel implements Cloneable {

    public Material() {
        super();
        setBackground( new Color( 0x33, 0x33, 0xff ) );
        setLocation( 50, 50 );
        setSize( 50, 50 );
    }

    public Object clone() throws java.lang.CloneNotSupportedException {
        return super.clone();
    }

}
::::::::::::::
MyPanel.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: SCM.html,v 1.1 2009/06/22 16:11:55 kishi Exp kishi $
*/

public class MyPanel extends JPanel {

    private Material material = null;
    private final Color bgColor = new Color( 0x99, 0x66, 0xff );

    public MyPanel() {
        super();

        setLayout( null );

        // 初期状態を設定
        BusinessUnit bu1 = new BusinessUnit();
        bu1.setBounds( 20, 100, 200, 200 );
        bu1.setBackground( Color.yellow );
        bu1.putMaterial();

        BusinessUnit bu2 = new BusinessUnit();
        bu2.setBounds( 400 , 100, 200, 200 );
        bu2.setBackground( Color.cyan );

        // パートナーをお互いに追加
        bu1.setPartner( bu2 );
        bu2.setPartner( bu1 );

        this.add( bu1 );
        this.add( bu2 );

        repaint();
    }

    public void paintComponent( Graphics g ) {
        int width = getWidth();
        int height = getHeight();

        // 画面クリア
        g.setColor( bgColor );
        // このコンポーネントのサイズを取得する
        g.fillRect( 0 , 0 , width , height );

    }

    public void addMaterial( Material material ) {
        this.material = material;
        this.add( material );
    }

    public void removeMaterial() {
        this.remove( material );
        material = null;
    }
}
::::::::::::::
Sample1.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: SCM.html,v 1.1 2009/06/22 16:11:55 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class Sample1 extends JFrame {

    public Sample1 () {
        super( "SCMの可視化" );

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

        // パネルをコンテナに配置
        MyPanel panel = new MyPanel();
        Container container = getContentPane();
        container.add( panel );

        setVisible( true );

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

    }

    private static void createAndShowGUI() {

        //Create and set up the window.
        JFrame frame = new Sample1();
    }

    public static void main( String[] args ) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater( new Runnable() {
                                                    public void run() {
                                                        createAndShowGUI();
                                                    }
                                                }
                                              );

    }

}

戻る inserted by FC2 system