⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conwaygui.java

📁 jboss规则引擎
💻 JAVA
字号:
package org.drools.examples.conway.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import org.drools.examples.conway.CellGrid;
import org.drools.examples.conway.ConwayApplicationProperties;
import org.drools.examples.conway.patterns.ConwayPattern;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import foxtrot.Job;
import foxtrot.Worker;

/**
 * @author <a href="mailto:brown_j@ociweb.com">Jeff Brown</a>
 */
public class ConwayGUI extends JPanel {
    private final JButton   nextGenerationButton;
    private final JButton   startStopButton;
    private final JButton   clearButton;
    private final JComboBox patternSelector = new JComboBox();
    private final Timer     timer;

    public ConwayGUI() {
        super( new BorderLayout() );
        final String nextGenerationLabel = ConwayApplicationProperties.getProperty( "next.generation.label" );
        this.nextGenerationButton = new JButton( nextGenerationLabel );
        final String startLabel = ConwayApplicationProperties.getProperty( "start.label" );
        this.startStopButton = new JButton( startLabel );
        final String clearLabel = ConwayApplicationProperties.getProperty( "clear.label" );
        this.clearButton = new JButton( clearLabel );
        final CellGrid grid = new CellGrid( 30,
                                            30 );
        final CellGridCanvas canvas = new CellGridCanvas( grid );
        final JPanel panel = new JPanel( new BorderLayout() );
        panel.add( BorderLayout.CENTER,
                   canvas );
        final Border etchedBorder = BorderFactory.createEtchedBorder( EtchedBorder.LOWERED );
        final Border outerBlankBorder = BorderFactory.createEmptyBorder( 5,
                                                                   5,
                                                                   5,
                                                                   5 );
        final Border innerBlankBorder = BorderFactory.createEmptyBorder( 5,
                                                                   5,
                                                                   5,
                                                                   5 );
        final Border border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( outerBlankBorder,
                                                                                                etchedBorder ),
                                                            innerBlankBorder );
        panel.setBorder( border );
        add( BorderLayout.CENTER,
             panel );
        add( BorderLayout.EAST,
             createControlPanel() );
        this.nextGenerationButton.addActionListener( new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                Worker.post( new Job() {
                    public Object run() {
                        grid.nextGeneration();
                        return null;
                    }
                } );
                canvas.repaint();
            }
        } );
        this.clearButton.addActionListener( new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                Worker.post( new Job() {
                    public Object run() {
                        grid.killAll();
                        return null;
                    }
                } );
                canvas.repaint();
            }
        } );

        final ActionListener timerAction = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                Worker.post( new Job() {
                    public Object run() {
                        if ( !grid.nextGeneration() ) {
                            stopTimer();
                        }
                        return null;
                    }
                } );
                canvas.repaint();
            }
        };
        this.timer = new Timer( 500,
                           timerAction );
        this.startStopButton.addActionListener( new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                if ( ConwayGUI.this.timer.isRunning() ) {
                    stopTimer();
                } else {
                    startTimer();
                }
            }
        } );

        populatePatternSelector();

        this.patternSelector.addActionListener( new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                final ConwayPattern pattern = (ConwayPattern) ConwayGUI.this.patternSelector.getSelectedItem();
                if ( pattern != null ) {
                    grid.setPattern( pattern );
                    canvas.repaint();
                }
            }
        } );

        this.patternSelector.setSelectedIndex( -1 );
    }

    private void populatePatternSelector() {
        final String patternClassNames = ConwayApplicationProperties.getProperty( "conway.pattern.classnames" );
        final StringTokenizer tokenizer = new StringTokenizer( patternClassNames );

        String className = null;
        while ( tokenizer.hasMoreTokens() ) {
            className = tokenizer.nextToken().trim();
            try {
                final Class clazz = Class.forName( className );
                if ( ConwayPattern.class.isAssignableFrom( clazz ) ) {
                    this.patternSelector.addItem( clazz.newInstance() );
                } else {
                    System.err.println( "Invalid pattern class name: " + className );
                }
            } catch ( final Exception e ) {
                System.err.println( "An error occurred populating patterns: " );
                e.printStackTrace();
            }
        }
    }

    private void startTimer() {
        final String stopLabel = ConwayApplicationProperties.getProperty( "stop.label" );
        this.startStopButton.setText( stopLabel );
        this.nextGenerationButton.setEnabled( false );
        this.clearButton.setEnabled( false );
        this.patternSelector.setEnabled( false );
        this.timer.start();
    }

    private void stopTimer() {
        this.timer.stop();
        final String startLabel = ConwayApplicationProperties.getProperty( "start.label" );
        this.startStopButton.setText( startLabel );
        this.nextGenerationButton.setEnabled( true );
        this.clearButton.setEnabled( true );
        this.patternSelector.setEnabled( true );
    }

    private JPanel createControlPanel() {
        final FormLayout layout = new FormLayout( "pref, 3dlu, pref, 3dlu:grow",
                                            "pref, 15dlu, pref, 15dlu, pref, 3dlu:grow, pref" );
        final PanelBuilder builder = new PanelBuilder( layout );
        final CellConstraints cc = new CellConstraints();

        final String title = ConwayApplicationProperties.getProperty( "app.title" );
        builder.addLabel( title,
                          cc.xywh( 1,
                                   1,
                                   layout.getColumnCount(),
                                   1 ) );

        final String info = ConwayApplicationProperties.getProperty( "app.description" );
        builder.addLabel( info,
                          cc.xywh( 1,
                                   3,
                                   layout.getColumnCount(),
                                   1 ) );

        final String patternLabel = ConwayApplicationProperties.getProperty( "pattern.label" );
        builder.addLabel( patternLabel,
                          cc.xy( 1,
                                 5 ) );

        builder.add( this.patternSelector,
                     cc.xy( 3,
                            5 ) );
        final JPanel buttonPanel = ButtonBarFactory.buildLeftAlignedBar( this.nextGenerationButton,
                                                                   this.startStopButton,
                                                                   this.clearButton );
        builder.add( buttonPanel,
                     cc.xywh( 1,
                              7,
                              layout.getColumnCount(),
                              1 ) );

        final Border etchedBorder = BorderFactory.createEtchedBorder( EtchedBorder.LOWERED );
        final Border outerBlankBorder = BorderFactory.createEmptyBorder( 5,
                                                                   5,
                                                                   5,
                                                                   5 );
        final Border innerBlankBorder = BorderFactory.createEmptyBorder( 5,
                                                                   5,
                                                                   5,
                                                                   5 );
        final Border border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( outerBlankBorder,
                                                                                                etchedBorder ),
                                                            innerBlankBorder );
        builder.setBorder( border );
        return builder.getPanel();
    }

    public static void main(final String[] args) {
        //        if ( args.length != 1 )
        //        {
        //            System.out.println( "Usage: " + ConwayGUI.class.getName( ) + " [drl file]" );
        //            return;
        //        }
        //        System.out.println( "Using drl: " + args[0] );
        //
        //        System.setProperty( "conway.drl.file",
        //                            args[0] );

        final String appTitle = ConwayApplicationProperties.getProperty( "app.title" );
        final JFrame f = new JFrame( appTitle );
        f.setResizable( false );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.getContentPane().add( BorderLayout.CENTER,
                                new ConwayGUI() );
        f.pack();
        f.setVisible( true );
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -