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

📄 world.java

📁 利用JAVA实现的利用进化算法对人口增长进行仿真的源码
💻 JAVA
字号:
/*
 * World.java
 *
 * Created on 12 October 2006, 08:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wpc.data;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
/**
 *
 * @author User
 */
public class World extends DataObject{
    Continent[] data;
    Integer[][] worldArray;
    BufferedImage worldImage;
    Tile[][] worldTile;
    int tempWorldX;
    int tempWorldY;
    
    /** Creates a new instance of World 
     * Contains all world infomation, in a series of continents.
     * On creatation, create x continents, where x is a number greater than 0
     * If no number supplied, use 1
     *
     * After creation, the world splits the available size into the available continents, with a 
     * random factor, +/- 25%, meaning one continent can be at most 50% larger than the others
     */    
    public World(wpc.MainFrame mainFrame) {
         super(mainFrame);
    }    
    
    public void createInitialWorld(){
         init(mainFrame.getRandom(mainFrame.getMaxRandomContinents())+1);
    }
    public void createInitialWorld(int number){
        if (number<=0){
            number=1;
        }
        init(number);
    }
    
    public void init(int number){
        createData(number);
        tempWorldX = mainFrame.getWorldX();
        tempWorldY = mainFrame.getWorldY();
        super.setXPOS(0, tempWorldX);
        super.setYPOS(0, tempWorldY);
        super.setData(data);
        createWorldGraphics();
        super.splitWorld();
        mainFrame.setTotalProgressPlus();
    }
    /* This calculates the available space, currently 1000*1000 pixels, and splits it into the
     * space per continent.
     *  
     */
        
    public void createData(int number){
        if(number<=0){
            number=1;
        }
        if(mainFrame.getMaxWorldTraversal() >= 0) {
            data = new Continent[number];
            mainFrame.setTotalProgressMax(number+3);
            mainFrame.addMessageDateStringDebug("World started@");
            for(int i = 0; i<number; i++){
                if(mainFrame.getLowDebugLevel()){
                    mainFrame.addMessageDateStringDebug("Starting Continent("+i+") @ ");
                }
                data[i] = new Continent(mainFrame);
                data[i].setColour(new Color(mainFrame.getRandom(255),mainFrame.getRandom(255),mainFrame.getRandom(255)));            
                if(mainFrame.getLowDebugLevel()){
                    mainFrame.addMessageDateStringDebug("Continent("+i+") finished @ ");
                }
                mainFrame.setTotalProgressPlus();
                      
            }
        }
        mainFrame.addMessageDateStringDebug("World finished@");
        
    }
    
    public void draw(Graphics g){
        //g.drawRect(0,0, mainFrame.worldX, mainFrame.worldY);
        //tempDrawWorldProc(g);
        Graphics2D g2 = (Graphics2D)g;
        if(worldImage!=null){
            g2.drawImage(worldImage, null, 0, 0);
        }
        if(data!=null){
          for(int i = 0; i<data.length; i++){
            if (data[i]!=null){
                data[i].draw(g);
            }
          }
        }
    }
    
 public void createWorldGraphics(){
        switch(mainFrame.getAlgorithm()){
            case 0: diamondSquareAlgorithm();
                    break;
            default: diamondSquareAlgorithm();
                    break;
        }
        System.out.println("Array Created");
        mainFrame.setTotalProgressPlus();
        convertToTile();
        loadImage();
        mainFrame.setTotalProgressPlus();
        System.out.println("Image ready to be drawn");
    }
 
    public void diamondSquareAlgorithm(){
        worldArray = new Integer[tempWorldX][tempWorldY];
        //Diamond Square Algorithm
        if(mainFrame.getRandomStartDiamondSquare()){
        //Step one.  Populate the corners with random numbers;
            worldArray[0][0] = Integer.valueOf(mainFrame.getRandom(mainFrame.getMaxWorldHeight()));
            worldArray[0][tempWorldY-1] = Integer.valueOf(mainFrame.getRandom(mainFrame.getMaxWorldHeight()));
            worldArray[tempWorldX-1][0] = Integer.valueOf(mainFrame.getRandom(mainFrame.getMaxWorldHeight()));
            worldArray[tempWorldX-1][tempWorldY-1] = Integer.valueOf(mainFrame.getRandom(mainFrame.getMaxWorldHeight()));
        }else{
            worldArray[0][0] = Integer.valueOf(mainFrame.getPatternStartHeight(0, mainFrame.getDiamondSquarePattern()));
            worldArray[0][tempWorldY-1] = Integer.valueOf(mainFrame.getPatternStartHeight(1, mainFrame.getDiamondSquarePattern()));
            worldArray[tempWorldX-1][0] = Integer.valueOf(mainFrame.getPatternStartHeight(2, mainFrame.getDiamondSquarePattern()));
            worldArray[tempWorldX-1][tempWorldY-1] = Integer.valueOf(mainFrame.getPatternStartHeight(3, mainFrame.getDiamondSquarePattern()));
        }
        //Step two.  Recursive Diamond step
        diamondStep(0,0,tempWorldX-1, tempWorldY-1);
    }
 
    public void loadImage(){
        BufferedImage tempImage = new BufferedImage(tempWorldX,tempWorldY, BufferedImage.TYPE_3BYTE_BGR);
        for (int i=0;i<tempWorldX;i++){
            for (int n=0;n<tempWorldY;n++){
                tempImage.setRGB(i,n, worldTile[i][n].getColorInt());
            }
        }
        worldImage = tempImage;
    }
    
    public void convertToTile(){
        worldTile = new Tile[tempWorldX][tempWorldY];
        for (int i=0;i<tempWorldX;i++){
            for (int n=0;n<tempWorldY;n++){
                worldTile[i][n] = new Tile(worldArray[i][n].intValue(), super.mainFrame);
            }
        }
        worldArray = null;
    }
    
    public void diamondStep(int x1, int y1, int x2, int y2){
        /*Step two cont.  Calculate the midpoint, and populate with the average of the four corners
         *plus a random amount.
         *
         *Assumptions:
         *All four corners are != null
         */
        int average = fourPointAvg(worldArray[x1][y1].intValue(),worldArray[x1][y2].intValue(),
        worldArray[x2][y1].intValue(),worldArray[x2][y2].intValue());
        int midpointX = x2-x1;
        double temp;        
        temp = midpointX;
        temp = temp/2;
        midpointX = (int)Math.ceil(temp);
        midpointX=midpointX+x1;
        int midpointY = y2-y1;
        temp = midpointY;
        temp = temp/2;
        midpointY = (int)Math.ceil(temp);
        midpointY=midpointY+y1;
        if ((midpointX==x1 || midpointX==x2)&&(midpointY==y1 || midpointY==y2)){
            //Point doesnt exist, break out of recursivity
        }else{ 
        worldArray[midpointX][midpointY] = Integer.valueOf(mainFrame.getRandomHeightAddition(average));
        /*Step three.  Call SquareStep four times, each with a quarter of the array
         *use cl
         */        
        squareStep(x1,y1,midpointX,midpointY);
        squareStep(midpointX,y1,x2,midpointY);
        squareStep(x1,midpointY,midpointX,y2);
        squareStep(midpointX,midpointY,x2,y2);
        }
        
    }
    
    public void squareStep(int x1, int y1, int x2, int y2){
        /* Step Four
         * Create a average of the points we know, and use that to create the 
         * points we dont
         */
        int count=0;
        int average=0;
        if(worldArray[x1][y1] != null){
            average=average+worldArray[x1][y1].intValue();
            count++;
        }
        if(worldArray[x1][y2] != null){
            average=average+worldArray[x1][y2].intValue();
            count++;
        }
        if(worldArray[x2][y1] != null){
            average=average+worldArray[x2][y1].intValue();
            count++;
        }
        if(worldArray[x2][y2] != null){
            average=average+worldArray[x2][y2].intValue();
            count++;
        }
        average = (int)Math.floor(average/count);
        if(worldArray[x1][y1] == null){
            worldArray[x1][y1] = Integer.valueOf(mainFrame.getRandomHeightAddition(average));
        }
        if(worldArray[x1][y2] == null){
            worldArray[x1][y2] = Integer.valueOf(mainFrame.getRandomHeightAddition(average));
        }
        if(worldArray[x2][y1] == null){
            worldArray[x2][y1] = Integer.valueOf(mainFrame.getRandomHeightAddition(average));
        }
        if(worldArray[x2][y2] == null){
            worldArray[x2][y2] = Integer.valueOf(mainFrame.getRandomHeightAddition(average));
        }
        /*Step 5
         *Call DimondStep for this square
         */
        diamondStep(x1,y1,x2,y2);
    }
    
    public int fourPointAvg(int x1, int x2, int x3, int x4){
        int average = (x1+x2+x3+x4);
        average = (int)Math.floor(average/4);
        return average;       
    }
}

⌨️ 快捷键说明

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