package org.opentrafficsim.trafficcontrol.trafcod; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; import java.rmi.RemoteException; import java.util.HashSet; import java.util.Set; import javax.media.j3d.Bounds; import javax.swing.JPanel; import javax.swing.ToolTipManager; import org.djunits.value.vdouble.scalar.Length; import org.opentrafficsim.core.geometry.OTSLine3D; import org.opentrafficsim.core.network.LongitudinalDirectionality; import org.opentrafficsim.road.network.lane.Lane; import org.opentrafficsim.road.network.lane.object.sensor.NonDirectionalOccupancySensor; import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight; import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor; import nl.tudelft.simulation.event.EventInterface; import nl.tudelft.simulation.event.EventListenerInterface; import nl.tudelft.simulation.event.EventType; import nl.tudelft.simulation.language.d3.DirectedPoint; /** * Display the current state of a TrafCOD machine. *
* Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version Nov 15, 2016
* @author Peter Knoppers
*/
public class TrafCODDisplay extends JPanel implements MouseMotionListener, MouseListener
{
/** */
private static final long serialVersionUID = 20161115L;
/** Background image. */
private final BufferedImage image;
/** The set of objects drawn on the image. */
private Set
* The implementation of TrafficLight only implements setTrafficLightColor. All other methods are dummies.
*/
class TrafficLightImage implements TrafficLight, TrafCODObject
{
/** The TrafCOD display. */
private final TrafCODDisplay display;
/** X-coordinate on the TrafCOD display image where this traffic light must be drawn. */
private final int x;
/** Y-coordinate on the TrafCOD display image where this traffic light must be drawn. */
private final int y;
/** Tool tip text for this traffic light image. */
private final String description;
/** The current color. */
private TrafficLightColor color = TrafficLightColor.BLACK;
/**
* Create a traffic light image.
* @param display TrafCODDisplay; the TrafCOD display on which this traffic light image will be rendered
* @param center Point2D; coordinates in the image where this traffic light is centered on
* @param description String; tool tip text for the new traffic light image
*/
public TrafficLightImage(final TrafCODDisplay display, final Point2D center, final String description)
{
this.display = display;
this.x = (int) center.getX();
this.y = (int) center.getY();
this.description = description;
display.addTrafCODObject(this);
}
/** {@inheritDoc} */
@Override
public String toolTipHit(int testX, int testY)
{
if (testX < this.x - DISC_SIZE / 2 || testX >= this.x + DISC_SIZE / 2 || testY < this.y - DISC_SIZE / 2
|| testY >= this.y + DISC_SIZE / 2)
{
return null;
}
return this.description;
}
/** {@inheritDoc} */
@Override
public DirectedPoint getLocation()
{
return null;
}
/** {@inheritDoc} */
@Override
public Bounds getBounds()
{
return null;
}
/** {@inheritDoc} */
@Override
public Lane getLane()
{
return null;
}
/** {@inheritDoc} */
@Override
public LongitudinalDirectionality getDirection()
{
return LongitudinalDirectionality.DIR_NONE;
}
/** {@inheritDoc} */
@Override
public Length getLongitudinalPosition()
{
return null;
}
/** {@inheritDoc} */
@Override
public OTSLine3D getGeometry()
{
return null;
}
/** {@inheritDoc} */
@Override
public Length getHeight()
{
return null;
}
/** {@inheritDoc} */
@Override
public String getId()
{
return null;
}
/** {@inheritDoc} */
@Override
public String getFullId()
{
return null;
}
/** {@inheritDoc} */
@Override
public boolean addListener(EventListenerInterface listener, EventType eventType) throws RemoteException
{
return false;
}
/** {@inheritDoc} */
@Override
public boolean addListener(EventListenerInterface listener, EventType eventType, boolean weak) throws RemoteException
{
return false;
}
/** {@inheritDoc} */
@Override
public boolean addListener(EventListenerInterface listener, EventType eventType, short position) throws RemoteException
{
return false;
}
/** {@inheritDoc} */
@Override
public boolean addListener(EventListenerInterface listener, EventType eventType, short position, boolean weak)
throws RemoteException
{
return false;
}
/** {@inheritDoc} */
@Override
public boolean removeListener(EventListenerInterface listener, EventType eventType) throws RemoteException
{
return false;
}
/** {@inheritDoc} */
@Override
public TrafficLightColor getTrafficLightColor()
{
return null;
}
/** {@inheritDoc} */
@Override
public void setTrafficLightColor(TrafficLightColor trafficLightColor)
{
this.color = trafficLightColor;
this.display.repaint();
}
/** Diameter of a traffic light disk in pixels. */
private static final int DISC_SIZE = 11;
/** {@inheritDoc} */
@Override
public void draw(Graphics2D g2)
{
Color lightColor;
switch (this.color)
{
case BLACK:
lightColor = Color.BLACK;
break;
case GREEN:
lightColor = Color.green;
break;
case YELLOW:
lightColor = Color.YELLOW;
break;
case RED:
lightColor = Color.RED;
break;
default:
System.err.println("Unhandled TrafficLightColor: " + this.color);
return;
}
g2.setColor(lightColor);
g2.fillOval(this.x - DISC_SIZE / 2, this.y - DISC_SIZE / 2, DISC_SIZE, DISC_SIZE);
// System.out.println("Drawn disk in color " + lightColor);
}
}