Package eas.users.ocScenarios.traffic.lights

Source Code of eas.users.ocScenarios.traffic.lights.LightsAgent

/*
* File name:        LightsAgent.java (package eas.users.ocScenario.cars.lights)
* Author(s):        Lukas König
* Java version:     7.0
* Generation date:  10.05.2014 (21:38:38)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.users.ocScenarios.traffic.lights;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import eas.math.geometry.Polygon2D;
import eas.math.geometry.Vector2D;
import eas.plugins.standard.liveInteraction.MethodRunnableProperties;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.startSetup.ParCollection;
import eas.users.ocScenarios.traffic.TrafficEnvironment;

/**
* @author Lukas König
*/
public class LightsAgent extends AbstractAgent2D<TrafficEnvironment> {

    private static final long serialVersionUID = -7267523065423916176L;

    private Polygon2D shape;
    public static final Color GREEN = Color.green;
    public static final Color YELLOW = Color.yellow;
    public static final Color RED = Color.red;
    private Color currentState = GREEN;

    public LightsAgent(int id, TrafficEnvironment env, ParCollection params) {
        super(id, env, params);
    }

    @Override
    @MethodRunnableProperties(ignoreMethod=true)
    public BufferedImage getInsideView() {
        int width = 100;
        int height = 3 * width;
        int maxWidth = width + 100;
        int radius = width / 2 - 2;
        BufferedImage img = new BufferedImage(maxWidth, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = img.createGraphics();
       
        Color top = Color.gray;
        Color middle = Color.gray;
        Color bottom = Color.gray;
       
        g.setColor(Color.white);
        g.fillRect(0, 0, maxWidth, height);
       
        if (this.currentState.equals(RED)) {
            top = RED;
        }
        if (this.currentState.equals(YELLOW)) {
            middle = YELLOW;
        }
        if (this.currentState.equals(GREEN)) {
            bottom = GREEN;
        }
       
        g.setColor(Color.orange);
        g.fillRect(width / 2 - 3, width / 2 - 3, width / 2 + 6, 3 * width / 2 + 6);
        g.setColor(Color.black);
        g.drawRect(width / 2 - 3, width / 2 - 3, width / 2 + 6, 3 * width / 2 + 6);

        g.setColor(top);
        g.fillOval(width / 2, 1 * width / 2, radius, radius);
        g.setColor(Color.black);
        g.drawOval(width / 2, 1 * width / 2, radius, radius);

        g.setColor(middle);
        g.fillOval(width / 2, 2 * width / 2, radius, radius);
        g.setColor(Color.black);
        g.drawOval(width / 2, 2 * width / 2, radius, radius);

        g.setColor(bottom);
        g.fillOval(width / 2, 3 * width / 2, radius, radius);
        g.setColor(Color.black);
        g.drawOval(width / 2, 3 * width / 2, radius, radius);
       
        return img;
    }

    @Override
    @MethodRunnableProperties(ignoreMethod=true)
    public BufferedImage getOutsideView() {
        return null;
    }
   
    @Override
    @MethodRunnableProperties(ignoreMethod=true)
    public Polygon2D getAgentShape() {
        if (shape == null) {
            shape = new Polygon2D();
            shape.add(new Vector2D(0, 1));
            shape.add(new Vector2D(1, 0));
            shape.add(new Vector2D(0, -1));
            shape.add(new Vector2D(-1, 0));
            shape.scale(Vector2D.NULL_VECTOR, new Vector2D(0.5, 0.5));
        }
       
        return shape;
    }
   
    @MethodRunnableProperties(ignoreMethod=true)
    public Color getCurrentState() {
        return this.currentState;
    }
   
    public void setCurrentState(Color currentState) {
        this.currentState = currentState;
    }
   
    @Override
    @MethodRunnableProperties(ignoreMethod=true)
    public Color getAgentColor() {
        return currentState;
    }
   
    public void toggle() {
        if (this.currentState.equals(GREEN)) {
            this.currentState = RED;
        } else if (this.currentState.equals(RED)) {
            this.currentState = GREEN;
        }
    }
}
TOP

Related Classes of eas.users.ocScenarios.traffic.lights.LightsAgent

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.