Permalink
Browse files

refactored optional background image loading, added example, formatted

  • Loading branch information...
akegermany committed Dec 16, 2016
1 parent 6483115 commit 3dcd768e759f5115fbce14cdf28941f85c27b8a1
Binary file not shown.
@@ -0,0 +1,8 @@
# main initial window size. window is maximized if one value is negative
xPixSizeWindow=1300
yPixSizeWindow=900
initialScale=0.85
xOffset=10
yOffset=800
backgroundColor=FFFFFF
backgroundPicturePath=background_example.png
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<OpenDRIVE>
<header revMajor="1" revMinor="2" name="" version="1.00" date="Thu Sep 2 20:31:10 2010" north="0.0" south="0.0" east="0.0" west="0.0" />
<road name="R1" length="1200.0" id="1" junction="-1">
<road name="R1" length="1450.0" id="1" junction="-1">
<planView>
<geometry s="0.0" x="0.0" y="300.0" hdg="0.0" length="1200.0">
<geometry s="0.0" x="0.0" y="400.0" hdg="0.0" length="1450.0">
<line />
</geometry>
</planView>
@@ -53,8 +53,6 @@
* That is the base class handles the "policy free" aspects of the view. Other aspects of the view (colors, setting up the simulation,
* drawing the foreground and background) are deferred to a subclass.
* </p>
*
*
*/
public abstract class SimulationCanvasBase extends Canvas {
@@ -73,9 +71,9 @@
protected boolean backgroundChanged;
// default background color
protected Color backgroundColor;
// default background picture
// optional background picture
protected String backgroundPicturePath;
// scale factor pixels/m, smaller value means a smaller looking view
// scale factor pixels/m, smaller value means a smaller looking view
double scale;
int xOffset = 0;
int yOffset = 0;
@@ -316,13 +314,10 @@ public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
public String getBackgroundPicturePath() {
return backgroundPicturePath;
}
public void setBackgroundPicturePath(String backgroundPicturePath) {
this.backgroundPicturePath = backgroundPicturePath;
}
/**
* Function to be overridden for subclass state change handling.
*/
@@ -34,19 +34,19 @@
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.xml.bind.JAXBException;
import org.apache.commons.lang3.StringUtils;
import org.movsim.roadmappings.PosTheta;
import org.movsim.roadmappings.RoadMapping;
import org.movsim.simulator.MovsimConstants;
@@ -95,8 +95,8 @@
* </p>
*
*/
public class TrafficCanvas extends SimulationCanvasBase implements SimulationRunnable.UpdateDrawingCallback,
SimulationRunnable.HandleExceptionCallback {
public class TrafficCanvas extends SimulationCanvasBase
implements SimulationRunnable.UpdateDrawingCallback, SimulationRunnable.HandleExceptionCallback {
private static final long serialVersionUID = 7637533802145001440L;
@@ -161,7 +161,13 @@
* Vehicle color support only the first four are used by the button. commandCyclevehicleColors()
*/
public enum VehicleColorMode {
VELOCITY_COLOR, LANE_CHANGE, ACCELERATION_COLOR, VEHICLE_LABEL_COLOR, VEHICLE_COLOR, EXIT_COLOR, HIGHLIGHT_VEHICLE
VELOCITY_COLOR,
LANE_CHANGE,
ACCELERATION_COLOR,
VEHICLE_LABEL_COLOR,
VEHICLE_COLOR,
EXIT_COLOR,
HIGHLIGHT_VEHICLE
}
/** Color mode displayed on startup */
@@ -227,8 +233,6 @@ protected void initGraphicConfigFieldsFromProperties() {
setDrawNotifyObjects(Boolean.parseBoolean(properties.getProperty("drawNotifyObjects")));
final int hexRadix = 16;
setBackgroundColor(new Color(Integer.parseInt(properties.getProperty("backgroundColor"), hexRadix)));
setBackgroundPicturePath(properties.getProperty("backgroundPicturePath"));
roadColor = new Color(Integer.parseInt(properties.getProperty("roadColor"), hexRadix));
roadEdgeColor = new Color(Integer.parseInt(properties.getProperty("roadEdgeColor"), hexRadix));
roadLineColor = new Color(Integer.parseInt(properties.getProperty("roadLineColor"), hexRadix));
@@ -245,6 +249,9 @@ protected void initGraphicConfigFieldsFromProperties() {
scale = Double.parseDouble(properties.getProperty("initialScale"));
setSleepTime(Integer.parseInt(properties.getProperty("initial_sleep_time")));
setBackgroundColor(new Color(Integer.parseInt(properties.getProperty("backgroundColor"), hexRadix)));
setBackgroundPicturePath(properties.getProperty("backgroundPicturePath"));
}
@Override
@@ -311,10 +318,15 @@ private void initGraphicSettings() {
labelColors.put(vehicleTypeLabel, color);
}
backgroundPicture = null;
if(backgroundPicturePath !=null)
try {
backgroundPicture = ImageIO.read(new File(backgroundPicturePath));
} catch (Exception e) {
if (StringUtils.isNotBlank(backgroundPicturePath)) {
try {
String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();
File file = new File(currentPath, backgroundPicturePath);
LOG.info("background image file parent={}, name={}", file.getParent(), file.getName());
backgroundPicture = ImageIO.read(file);
} catch (Exception e) {
LOG.error("cannot load background image " + backgroundPicturePath, e);
}
}
}
@@ -579,8 +591,10 @@ private void drawVehicle(Graphics2D g, double simulationTime, RoadMapping roadMa
@Override
protected void drawBackground(Graphics2D g) {
if(backgroundPicture != null)
g.drawImage(backgroundPicture, 0, -(int) (backgroundPicture.getHeight() ), (int) (backgroundPicture.getWidth()*1.01 ), 0, 0, 0, backgroundPicture.getWidth(), backgroundPicture.getHeight(), null);
if (backgroundPicture != null)
g.drawImage(backgroundPicture, 0, -(int) (backgroundPicture.getHeight()),
(int) (backgroundPicture.getWidth() * 1.01), 0, 0, 0, backgroundPicture.getWidth(),
backgroundPicture.getHeight(), null);
drawRoadSegmentsAndLines(g);
@@ -713,8 +727,9 @@ private void drawRoadSectionIds(Graphics2D g) {
for (final RoadSegment roadSegment : roadNetwork) {
final RoadMapping roadMapping = roadSegment.roadMapping();
final double position = roadMapping.isPeer() ? roadMapping.roadLength() : 0.0;
final double offset = roadMapping.isPeer() ? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries()
.getLeft().getLaneCount() - 1) : roadMapping.getMaxOffsetRight();
final double offset = roadMapping.isPeer()
? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries().getLeft().getLaneCount() - 1)
: roadMapping.getMaxOffsetRight();
final PosTheta posTheta = roadMapping.map(position, offset);
final int fontHeight = 12;
final Font font = new Font("SansSerif", Font.PLAIN, fontHeight);
@@ -733,8 +748,9 @@ private void drawSpeedLimits(Graphics2D g) {
for (SpeedLimit speedLimit : roadSegment.speedLimits()) {
g.setFont(font);
final double position = speedLimit.position();
final double offset = roadMapping.isPeer() ? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries()
.getLeft().getLaneCount() - 1) : roadMapping.getMaxOffsetRight();
final double offset = roadMapping.isPeer()
? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries().getLeft().getLaneCount() - 1)
: roadMapping.getMaxOffsetRight();
final PosTheta posTheta = roadMapping.map(position, offset);
final String text = String.valueOf((int) (speedLimit.getSpeedLimitKmh())) + "km/h";
g.setFont(font);
@@ -752,8 +768,9 @@ private void drawSlopes(Graphics2D g) {
final int fontHeight = 12;
final Font font = new Font("SansSerif", Font.BOLD, fontHeight); //$NON-NLS-1$
final RoadMapping roadMapping = roadSegment.roadMapping();
final double offset = roadMapping.isPeer() ? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries()
.getLeft().getLaneCount() - 1) : roadMapping.getMaxOffsetRight();
final double offset = roadMapping.isPeer()
? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries().getLeft().getLaneCount() - 1)
: roadMapping.getMaxOffsetRight();
for (GradientProfile gradientProfile : roadSegment.gradientProfiles()) {
for (Entry<Double, Double> gradientEntry : gradientProfile.gradientEntries()) {
final double position = gradientEntry.getKey();
@@ -775,8 +792,9 @@ private void drawFlowConservingBottlenecks(Graphics2D g) {
g.setColor(color);
for (final RoadSegment roadSegment : roadNetwork) {
final RoadMapping roadMapping = roadSegment.roadMapping();
final double offset = roadMapping.isPeer() ? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries()
.getLeft().getLaneCount() - 1) : roadMapping.getMaxOffsetRight();
final double offset = roadMapping.isPeer()
? roadMapping.getOffsetLeft(roadMapping.getLaneGeometries().getLeft().getLaneCount() - 1)
: roadMapping.getMaxOffsetRight();
for (FlowConservingBottleneck bottleneck : roadSegment.flowConservingBottlenecks()) {
final double posStart = bottleneck.position();
PosTheta posTheta = roadMapping.map(posStart, offset);
@@ -20,8 +20,6 @@ lineWidth=1.0
lineLength=5.0
gapLength=15.0
gapLengthExit=9.0
backgroundColor=4AAC17
backgroundPicturePath=/home/test.jpg
roadColor=808080
roadEdgeColor=222222
roadLineColor=DDDDDD
@@ -39,3 +37,7 @@ drawSinks=true
drawSpeedLimits=true
drawSlopes=true
drawNotifyObjects=true
#
backgroundColor=4AAC17
# background image not available by default:
#backgroundPicturePath="example.jpg"

0 comments on commit 3dcd768

Please sign in to comment.