/*
* File name: ClockEnvironment.java (package eas.simulation.users.lukas.clock)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 28.12.2010 (23:08:48)
*
* (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.demos.clock;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Calendar;
import java.util.List;
import eas.math.geometry.Polygon2D;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.simulation.Wink;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
/**
* @author Lukas König
*
*/
public class ClockEnvironment extends AbstractEnvironment2D<AbstractAgent2D<?>> {
/**
*
*/
private static final long serialVersionUID = 4517359899544367079L;
private boolean smoothSeconds = false;
private boolean smoothMinutes = true;
private boolean smoothHours = true;
public ClockEnvironment(int ident, ParCollection params) {
super(ident, params);
if (params != null) {
smoothSeconds = params.getParValueBoolean("SmoothSeconds");
}
if (params != null) {
smoothMinutes = params.getParValueBoolean("SmoothMinutes");
}
if (params != null) {
smoothHours = params.getParValueBoolean("SmoothHours");
}
}
private int oldSeconds = 0;
@Override
public void step(Wink simTime) {
super.step(simTime);
Calendar ca = Calendar.getInstance();
double seconds = ca.get(Calendar.SECOND);
double minutes = ca.get(Calendar.MINUTE);
double hours = ca.get(Calendar.HOUR);
if (smoothSeconds) {
seconds += (double) ca.get(Calendar.MILLISECOND) / 1000;
}
if (smoothMinutes) {
minutes += seconds / 60;
}
if (smoothHours) {
hours += minutes / 60;
}
double hourAngle = 360 / 12 * hours + 180;
double minuteAngle = 360 / 60 * minutes + 180;
double secondAngle = 360 / 60 * seconds + 180;
this.setAgentAngle(63, hourAngle);
this.setAgentAngle(62, minuteAngle);
this.setAgentAngle(61, secondAngle);
if (!smoothSeconds) {
if (oldSeconds != (int) seconds) {
try {
Thread.sleep(900);
} catch (InterruptedException e) {
e.printStackTrace();
}
oldSeconds = (int) seconds;
}
}
smoothSeconds = this.getParCollection().getParValueBoolean("SmoothSeconds");
smoothMinutes = this.getParCollection().getParValueBoolean("SmoothMinutes");
smoothHours = this.getParCollection().getParValueBoolean("SmoothHours");
}
@Override
public List<SingleParameter> getParameters() {
List<SingleParameter> liste = super.getParameters();
liste.add(new SingleParameter(
"SmoothSeconds",
Datatypes.BOOLEAN,
true,
"",
"CLOCK_ENVIRONMENT"));
liste.add(new SingleParameter(
"SmoothMinutes",
Datatypes.BOOLEAN,
true,
"",
"CLOCK_ENVIRONMENT"));
liste.add(new SingleParameter(
"SmoothHours",
Datatypes.BOOLEAN,
true,
"",
"CLOCK_ENVIRONMENT"));
return liste;
}
@Override
public synchronized BufferedImage getOutsideView() {
return this.getOutsideView(null);
}
@Override
public synchronized BufferedImage getOutsideView(Graphics2D g2) {
BufferedImage img2 = super.getOutsideView(g2);
BufferedImage img = new BufferedImage(img2.getWidth(), img2.getHeight(), img2.getType());
Graphics2D g;
double rad = 22;
if (g2 == null) {
g = img.createGraphics();
} else {
g = g2;
}
Rectangle2D clip = this.getClippingRectangle();
g.clipRect(
(int) clip.upperLeftCorner().x,
(int) clip.upperLeftCorner().y,
(int) clip.lowerRightCorner().x + 1,
(int) clip.lowerRightCorner().y + 1);
g.setColor(Color.black);
if (!this.getParCollection().getParValueBoolean("showMinuteSegments")) {
double large = 0.65 * this.globalScale();
double huge = 1.25 * this.globalScale();
double small = 0.35 * this.globalScale();
for (int i = 0; i < 60; i++) {
Vector2D pos = this.getPointInVisualization(new Vector2D(
Math.sin((double) i / 30 * Math.PI) * rad,
Math.cos((double) i / 30 * Math.PI) * rad));
if (i % 60 == 30) {
g.fillOval(
(int) (pos.x - huge / 2),
(int) (pos.y - huge / 2),
(int) huge,
(int) huge);
} else if (i % 5 == 0) {
g.fillOval(
(int) (pos.x - large / 2),
(int) (pos.y - large / 2),
(int) large,
(int) large);
} else {
g.fillOval(
(int) (pos.x - small / 2),
(int) (pos.y - small / 2),
(int) small,
(int) small);
}
}
}
double transX12 = -1.5;
double transY12 = 3.5;
if (!GlobalVariables.getPrematureParameters().getParValueBoolean("strangeView?")
&& !GlobalVariables.getPrematureParameters().getParValueBoolean("showMinuteSegments")) {
if (i1 == null) {
i1 = new Polygon2D();
i1.add(new Vector2D(-1.5, -10));
i1.add(new Vector2D(1.5, -10));
i1.add(new Vector2D(1.5, 6));
i1.add(new Vector2D(-1.5, 6));
i1.scale(Vector2D.NULL_VECTOR, new Vector2D(0.1, 0.2));
i1.translate(new Vector2D(transX12 + 2, -rad + transY12));
}
if (i2 == null) {
i2 = new Polygon2D(i1);
i2.translate(new Vector2D(1, 0));
}
if (i3 == null) {
i3 = new Polygon2D(i2);
i3.translate(new Vector2D(rad - 5, 19));
}
if (i4 == null) {
i4 = new Polygon2D(i3);
i4.translate(new Vector2D(1, 0));
}
if (i5 == null) {
i5 = new Polygon2D(i3);
i5.translate(new Vector2D(2, 0));
}
if (x1 == null) {
x1 = new Polygon2D();
x1.add(new Vector2D(-2, -2));
x1.add(new Vector2D(-10, -10));
x1.add(new Vector2D(-7, -10));
x1.add(new Vector2D(0, -3));
x1.add(new Vector2D(7, -10));
x1.add(new Vector2D(10, -10));
x1.add(new Vector2D(2, -2));
x1.add(new Vector2D(10, 6));
x1.add(new Vector2D(7, 6));
x1.add(new Vector2D(0, -1));
x1.add(new Vector2D(-7, 6));
x1.add(new Vector2D(-10, 6));
x1.scale(Vector2D.NULL_VECTOR, new Vector2D(0.1, 0.2));
x1.translate(new Vector2D(transX12, -rad + transY12));
}
if (i6 == null) {
i6 = new Polygon2D(i5);
i6.translate(new Vector2D(-19.5, 19));
}
if (v == null) {
v = new Polygon2D();
v.add(new Vector2D(-1.5, 6));
v.add(new Vector2D(1.5, 6));
v.add(new Vector2D(12.5, -10));
v.add(new Vector2D(9.5, -10));
v.add(new Vector2D(0, 4.5));
v.add(new Vector2D(-0, 4.5));
v.add(new Vector2D(-9.5, -10));
v.add(new Vector2D(-12.5, -10));
v.scale(Vector2D.NULL_VECTOR, new Vector2D(0.1, 0.2));
v.translate(new Vector2D(-1, 19.5));
}
if (x2 == null) {
x2 = new Polygon2D(x1);
x2.translate(new Vector2D(-17, 19));
}
if (i7 == null) {
i7 = new Polygon2D(i6);
i6.translate(new Vector2D(-21.5, -19));
}
Polygon2D x1v = this.getPolygonInVisualization(x1);
Polygon2D i1v = this.getPolygonInVisualization(i1);
Polygon2D i2v = this.getPolygonInVisualization(i2);
Polygon2D i3v = this.getPolygonInVisualization(i3);
Polygon2D i4v = this.getPolygonInVisualization(i4);
Polygon2D i5v = this.getPolygonInVisualization(i5);
Polygon2D i6v = this.getPolygonInVisualization(i6);
Polygon2D vv = this.getPolygonInVisualization(v);
Polygon2D x2v = this.getPolygonInVisualization(x2);
Polygon2D i7v = this.getPolygonInVisualization(i7);
g.setColor(Color.GRAY);
g.fillPolygon(x1v.toPol());
g.fillPolygon(i1v.toPol());
g.fillPolygon(i2v.toPol());
g.fillPolygon(i3v.toPol());
g.fillPolygon(i4v.toPol());
g.fillPolygon(i5v.toPol());
g.fillPolygon(i6v.toPol());
g.fillPolygon(vv.toPol());
g.fillPolygon(x2v.toPol());
g.fillPolygon(i7v.toPol());
}
g.drawImage(img2, 0, 0, null);
return img;
}
private Polygon2D x1; // 12
private Polygon2D i1;
private Polygon2D i2;
private Polygon2D i3; // 3
private Polygon2D i4;
private Polygon2D i5;
private Polygon2D v; // 6
private Polygon2D i6;
private Polygon2D x2; // 9
private Polygon2D i7;
}