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

📄 evolve.java

📁 CroftSoft Code Library是一个开源的可移植的纯Java游戏库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     package com.croftsoft.apps.evolve;
     
     import java.awt.*;
     import java.awt.event.*;
     import java.io.*;
     import java.util.*;
     import javax.swing.*;
     import javax.swing.event.*;

     import com.croftsoft.core.awt.font.FontLib;
     import com.croftsoft.core.gui.FrameLib;
     import com.croftsoft.core.gui.plot.PlotLib;
     import com.croftsoft.core.lang.lifecycle.Lifecycle;
     import com.croftsoft.core.animation.*;
     import com.croftsoft.core.animation.component.*;
     import com.croftsoft.core.util.loop.*;

     /*********************************************************************
     * Main Evolve class.
     *
     * @version
     *   2003-07-23
     * @since
     *   1996-09-01
     * @author
     *   <a href="http://www.croftsoft.com/">David Wallace Croft</a>
     *********************************************************************/

     public final class  Evolve
       extends JApplet
       implements
         ActionListener, ChangeListener, ComponentListener, MouseListener,
         Lifecycle, ComponentAnimator
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {

     private static final String  VERSION = "2002-03-13";

     private static final String  TITLE = "CroftSoft Evolve";

     private static final String  INFO
       = "\n" + TITLE + "\n"
       + "Copyright 2002 CroftSoft Inc\n"
       + "http://www.croftsoft.com/\n"
       + "Version " + VERSION + "\n";

     private static final double  FRAME_RATE = 1.0;

     private static final Dimension  FRAME_SIZE  = null;

     private static final String  FRAME_ICON_FILENAME = "/images/david.png";

     private static final String  SHUTDOWN_CONFIRMATION_PROMPT
       = "Close " + TITLE + "?";

     private static final String  FONT_NAME = "TimesRoman";

     private static final int     FONT_STYLE = Font.BOLD;

     //

     private static final int  SPACE_WIDTH       = 100;

     private static final int  SPACE_HEIGHT      = 100;

     private static final int  BUGS_MAX          = 10000;

     private static final int  GENES_MAX         = 8;

     private static final int  BABY_ENERGY       = 10;

     private static final int  BIRTH_ENERGY      = 30;

     private static final int  BIRTH_ENERGY_COST = 20;

     private static final int  FLORA_ENERGY      = 20;

     private static final int  MAX_ENERGY        = 60;

     private static final int  MOVE_COST         = 1;

     private static final int  EDEN_WIDTH        = 2;

     private static final int  EDEN_HEIGHT       = 2;

     private static final int  EDEN_X0           = ( SPACE_WIDTH  - EDEN_WIDTH  ) / 2;

     private static final int  EDEN_Y0           = ( SPACE_HEIGHT - EDEN_HEIGHT ) / 2;

     private static final int  EDEN_X1           = EDEN_X0 + EDEN_WIDTH  - 1;

     private static final int  EDEN_Y1           = EDEN_Y0 + EDEN_HEIGHT - 1;

     private static final int  MIN_GROWTH_RATE   = 0;

     private static final int  MAX_GROWTH_RATE   = SPACE_WIDTH * SPACE_HEIGHT;

     private static final int  SPINNER_STEP_SIZE = 1;

     private static final int  TEXT_MARGIN       = 10;

     private static final int  INIT_GROWTH_RATE  = 10;

     //

     private static final Color  CRUISER_COLOR = Color.RED;

     private static final Color  NORMAL_COLOR  = Color.MAGENTA;

     private static final Color  TWIRLER_COLOR = Color.BLUE;

     //

     private Random           random;

     private Bug [ ]          bugs;

     private int              flora_growth_rate = INIT_GROWTH_RATE;

     private int              bugs_alive;

     private int              time = 0;

     private boolean [ ] [ ]  flora_present;

     //

     private Rectangle           bounds;

     private AnimatedComponent   animatedComponent;

     //

     private JButton             resetButton;

     private JButton             droughtButton;

     private JCheckBox           edenCheckBox;

     private SpinnerNumberModel  growthRateSpinnerNumberModel;

     //////////////////////////////////////////////////////////////////////
     // static methods
     //////////////////////////////////////////////////////////////////////

     public static void  main ( String [ ]  args )
     //////////////////////////////////////////////////////////////////////
     {
       System.out.println ( INFO );

       JFrame  jFrame = new JFrame ( TITLE );

       try
       {
         FrameLib.setIconImage ( jFrame, FRAME_ICON_FILENAME );
       }
       catch ( IOException  ex )
       {
       }

       Evolve  evolve = new Evolve ( );

       jFrame.setContentPane ( evolve );

       FrameLib.launchJFrameAsDesktopApp (
         jFrame,
         new Lifecycle [ ] { evolve },
         FRAME_SIZE,
         SHUTDOWN_CONFIRMATION_PROMPT );
     }

     //////////////////////////////////////////////////////////////////////
     // overridden Applet methods
     //////////////////////////////////////////////////////////////////////

     public String  getAppletInfo ( ) { return INFO; }

     //////////////////////////////////////////////////////////////////////
     // interface Lifecycle methods
     //////////////////////////////////////////////////////////////////////

     public synchronized void  init ( )
     //////////////////////////////////////////////////////////////////////
     {
       Container  contentPane = getContentPane ( );

       contentPane.setLayout ( new BorderLayout ( ) );

       //

       bounds = new Rectangle ( );

       animatedComponent
         = new BufferedAnimatedComponent ( this, FRAME_RATE );

       animatedComponent.setLoopGovernor (
         new FixedDelayLoopGovernor ( FRAME_RATE ) );

       animatedComponent.addComponentListener ( this );

       animatedComponent.addMouseListener ( this );

       animatedComponent.init ( );

       contentPane.add ( animatedComponent, BorderLayout.CENTER );

       //

       JPanel  southPanel = new JPanel ( );

       contentPane.add ( southPanel, BorderLayout.SOUTH );

       //

       resetButton = new JButton ( "Reset" );

       resetButton.addActionListener ( this );

       southPanel.add ( resetButton );

       //

       droughtButton = new JButton ( "Blight" );

       droughtButton.addActionListener ( this );

       southPanel.add ( droughtButton );

       //

       edenCheckBox = new JCheckBox ( "Eden", null, true );

       southPanel.add ( edenCheckBox );

       //

       southPanel.add (
         new JLabel ( "Food Growth Rate", SwingConstants.RIGHT ) );

       growthRateSpinnerNumberModel = new SpinnerNumberModel (
         flora_growth_rate,
         MIN_GROWTH_RATE,
         MAX_GROWTH_RATE,
         SPINNER_STEP_SIZE );

       growthRateSpinnerNumberModel.addChangeListener ( this );

       southPanel.add ( new JSpinner ( growthRateSpinnerNumberModel ) );

       //

       random = new Random ( );

       bugs = new Bug [ BUGS_MAX ];

       flora_present = new boolean [ SPACE_WIDTH ] [ SPACE_HEIGHT ];

       reset ( );
     }

     public void  start ( )
     //////////////////////////////////////////////////////////////////////
     {
       animatedComponent.start ( );
     }

     public synchronized void  stop ( )
     //////////////////////////////////////////////////////////////////////
     {
       animatedComponent.stop ( );
     }

     public synchronized void  destroy ( )
     //////////////////////////////////////////////////////////////////////
     {
       animatedComponent.destroy ( );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public void  update ( JComponent  component )
     //////////////////////////////////////////////////////////////////////
     {
       moveBugs ( );

       growFlora ( );

       component.repaint ( );
     }

     public void  paint (
       JComponent  component,
       Graphics2D  g )
     //////////////////////////////////////////////////////////////////////
     {
       g.setColor ( Color.BLACK );

       g.fillRect ( 0, 0, bounds.width, bounds.height );

       plotFlora ( g );

       plotBugs ( g );

       g.setColor ( Color.WHITE );

       g.drawString (
         createStatusString ( bugs_alive, time, genesAverageString ( ) ),
        bounds.x + TEXT_MARGIN,
        bounds.y + bounds.height - TEXT_MARGIN );
     }

     //////////////////////////////////////////////////////////////////////
     // Listener interface methods
     //////////////////////////////////////////////////////////////////////

     public void  actionPerformed ( ActionEvent  actionEvent )
     //////////////////////////////////////////////////////////////////////
     {
       Object  source = actionEvent.getSource ( );

       if ( source == resetButton )
       {
         reset ( );
       }
       else if ( source == droughtButton )
       {
         setAllFloraPresent ( false );
       }
     }

     public void  componentResized ( ComponentEvent  componentEvent )
     //////////////////////////////////////////////////////////////////////
     {
       Object  source = componentEvent.getSource ( );

       if ( source == animatedComponent )
       {
         animatedComponent.getBounds ( bounds );

         FontLib.setMaxFont (
           animatedComponent,
           createStatusString (
             BUGS_MAX, GENES_MAX - 1, genesAverageString ( ) ),
           FONT_NAME,
           FONT_STYLE,
           bounds.width - 2 * TEXT_MARGIN,
           bounds.height );
       }
     }

     public void  mousePressed ( MouseEvent  mouseEvent )
     //////////////////////////////////////////////////////////////////////
     {
       Point  bugLocation = PlotLib.graphics_to_plot_transform (
         mouseEvent.getPoint ( ), bounds, animatedComponent.getGraphics ( ),
         0, SPACE_WIDTH, 0, SPACE_HEIGHT );

       createNewBug ( bugLocation.x, bugLocation.y );
     }

     public void  stateChanged ( ChangeEvent  changeEvent )
     //////////////////////////////////////////////////////////////////////
     {
       Object  source = changeEvent.getSource ( );

       if ( source == growthRateSpinnerNumberModel )
       {
         flora_growth_rate = ( ( Integer )
           growthRateSpinnerNumberModel.getValue ( ) ).intValue ( );
       }
     }

     public void  componentHidden ( ComponentEvent  componentEvent ) { }

     public void  componentMoved  ( ComponentEvent  componentEvent ) { }

     public void  componentShown  ( ComponentEvent  componentEvent ) { }

     public void  mouseClicked  ( MouseEvent  mouseEvent ) { }

     public void  mouseEntered  ( MouseEvent  mouseEvent ) { }

     public void  mouseExited   ( MouseEvent  mouseEvent ) { }

     public void  mouseReleased ( MouseEvent  mouseEvent ) { }

     //////////////////////////////////////////////////////////////////////
     // private methods
     //////////////////////////////////////////////////////////////////////

     private void  reset ( )
     //////////////////////////////////////////////////////////////////////
     {
       for ( int  i = 0; i < BUGS_MAX; i++ )
       {

⌨️ 快捷键说明

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