Package manager

Source Code of manager.GUIWorldController

package manager;

import agent.Agent;
import environment.Cell;
import environment.World;
import gui.GUIWorld;
import java.awt.Color;
import java.util.Observable;
import java.util.Observer;
import constants.EventConstants;
import parameters.LocalWorldParams;
import resource.Resource;
import time.TimeEventDispatcher;

public class GUIWorldController extends Observable implements Observer {

    private LocalWorldParams params;
    private GUIWorld guiWorld;

    public GUIWorldController() {
        TimeEventDispatcher.getInstance().addObserver(this);
    }

    public GUIWorld createGUIWorld(World world) {
        params = world.getParams();
        guiWorld = new GUIWorld(params);
        resetGUIGrid(world);
        return guiWorld;
    }

    public void resetGUIGrid(World world) {
        for (int i = 0; i < params.getCols(); i++) {
            for (int j = 0; j < params.getRows(); j++) {
                Cell cell = world.getCell(i, j);
                Color color = getCellColor(cell);
                guiWorld.updateCell(i, j, color);
            }
        }
    }

    public void updateGUIAgent(Agent agent) {
        int id = agent.getId();
        int x = agent.getUnit().getCell().getX();
        int y = agent.getUnit().getCell().getY();
        int progenitor = agent.getProgenitor();
        Color bodyColor = Color.WHITE;
        Color dnaColor = agent.getInternalColor();
        guiWorld.updateUnit(id, progenitor, x, y, bodyColor, dnaColor);
    }

    public void updateGUICell(Cell cell) {
        Color color = getCellColor(cell);
        guiWorld.updateCell(cell.getX(), cell.getY(), color);
    }

    public void killGUIAgent(Agent agent) {
        int id = agent.getId();
        int progenitor = agent.getProgenitor();
        guiWorld.killUnit(id, progenitor);
    }

    private Color getCellColor(Cell cell) {
        Resource resource = cell.getResource();
        Color color = null;
        color = resource.getColor();
        return color;
    }

    /**
     * update methiod is used in order to repaint GUI every time event thrown
     * TimeEventDispatcher is the observed class
     * @param o
     * @param arg
     */
    public void update(Observable o, Object arg) {
        String event = (String) arg;
        if (event.startsWith(EventConstants.TIME_EVENT)) {
            if (guiWorld != null) {
                guiWorld.repaint();
            }
        }
    }
}
TOP

Related Classes of manager.GUIWorldController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.