* Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
* initial version Sep 13, 2014
* @author Alexander Verbraeck
*/
public class LinkAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
{
/** */
private static final long serialVersionUID = 20140000L;
/** */
private float width;
/** the Text object to destroy when the animation is destroyed. */
private Text text;
/**
* @param link Link; Link
* @param simulator SimulatorInterface.TimeDoubleUnit; simulator
* @param width float; width
* @throws NamingException for problems with registering in context
* @throws RemoteException on communication failure
*/
public LinkAnimation(final Link link, final SimulatorInterface.TimeDoubleUnit simulator, final float width)
throws NamingException, RemoteException
{
super(link, simulator);
this.width = width;
this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator,
link.getLinkType().getId().equals(LinkType.DEFAULTS.FREEWAY.getId()) ? TextAnimation.RENDERWHEN10
: TextAnimation.RENDERWHEN1);
}
/** {@inheritDoc} */
@Override
public final void paint(final Graphics2D graphics, final ImageObserver observer)
{
Color color = getSource().getLinkType().isConnector() ? Color.PINK.darker() : Color.BLUE;
PolyLine3d designLine = getSource().getDesignLine();
PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
// Accentuate the end points
try
{
drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
}
catch (DrawRuntimeException exception)
{
// Cannot happen
CategoryLogger.always().error(exception);
}
}
/**
* Draw end point on design line.
* @param endPoint OTSPoint3D; the end of the design line where a end point must be highlighted
* @param nextPoint OTSPoint3D; the point nearest endPoint (needed to figure out the direction of the design
* line)
* @param graphics Graphics2D; graphics content
*/
private void drawEndPoint(final Point3d endPoint, final Point3d nextPoint, final Graphics2D graphics)
{
// End point marker is 2 times the width of the design line
double dx = nextPoint.x - endPoint.x;
double dy = nextPoint.y - endPoint.y;
double length = endPoint.distance(nextPoint);
// scale dx, dy so that size is this.width
dx *= this.width / length;
dy *= this.width / length;
try
{
PolyLine3d line = new PolyLine3d(new Point3d(endPoint.x - dy, endPoint.y + dx, endPoint.z),
new Point3d(endPoint.x + dy, endPoint.y - dx, endPoint.z));
PaintLine.paintLine(graphics, getSource().getLinkType().isConnector() ? Color.PINK.darker() : Color.BLUE,
this.width / 30, getSource().getLocation(), line);
}
catch (DrawRuntimeException exception)
{
CategoryLogger.always().error(exception);
}
}
/** {@inheritDoc} */
@Override
public final void destroy(final SimulatorInterface, ?, ?> simulator)
{
super.destroy(simulator);
this.text.destroy(simulator);
}
/** {@inheritDoc} */
@Override
public ClonableRenderable2DInterface clone(final Link newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
throws NamingException, RemoteException
{
// the constructor also constructs the corresponding Text object
return new LinkAnimation(newSource, newSimulator, this.width);
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
}
/**
* Text animation for the Link. Separate class to be able to turn it on and off...
*
* Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
*
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
* initial version Dec 11, 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class Text extends TextAnimation
{
/** */
private static final long serialVersionUID = 20161211L;
/**
* @param source Locatable; the object for which the text is displayed
* @param text String; the text to display
* @param dx float; the horizontal movement of the text, in meters
* @param dy float; the vertical movement of the text, in meters
* @param textPlacement TextAlignment; where to place the text
* @param color Color; the color of the text
* @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
* @param scaleDependentRendering ScaleDependentRendering; enables rendering in a scale dependent fashion
* @throws NamingException when animation context cannot be created or retrieved
* @throws RemoteException - when remote context cannot be found
*/
@SuppressWarnings("checkstyle:parameternumber")
public Text(final Locatable source, final String text, final float dx, final float dy,
final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator,
final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
{
super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, simulator, null, scaleDependentRendering);
}
/** {@inheritDoc} */
@Override
public OrientedPoint3d getLocation() throws RemoteException
{
// draw always on top, and not upside down.
Ray3d p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
double a = Angle.normalizePi(p.getPhi());
if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
{
a += Math.PI;
}
return new OrientedPoint3d(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
}
/** {@inheritDoc} */
@Override
public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
throws RemoteException, NamingException
{
return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator,
super.getScaleDependentRendering());
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return "LinkAnimation.Text []";
}
}
}