1

Currently working on my second semester working on a project where we have used an Enum class for the first time. Currently it holds WayTypes which we use to color in our MapCanvas class. I'd like to iterate through this class and assign the different colors to the different Enums during this iteration, but I can't figure out how.

Currently my Enum class looks like this:

public enum WayType {
UNKNOWN, BUILDING, WATER, COASTLINE, LANDUSE, LEISURE, FARMLAND, 
BICYCLE, FOOTWAY, PRIMARYROAD, MOTORWAY, TERTIARYROAD, SECONDARYROAD;
}

How we apply the color in our MapCanvas at this moment:

    gc.setFill(Color.LIGHTGREEN);
    for (Drawable way : model.getWaysOfType(WayType.LANDUSE)) 
    way.fill(gc);
    for (Drawable way : model.getWaysOfType(WayType.LANDUSE)) 
    way.stroke(gc);

Stroke and fill methods looks like this:

    public void stroke(GraphicsContext gc) {
    gc.beginPath();
    trace(gc);
    gc.stroke();
}

And

    public void fill(GraphicsContext gc) {
    gc.beginPath();
    trace(gc);
    gc.fill();
}

Any help is greatly appreciated. Thanks in advance :)

rasm937k
  • 91
  • 8
  • 1
    There is no need to loop through and assign values in that manner. You can create an enum with default values, see related question. https://stackoverflow.com/questions/1067352/can-i-set-enum-start-value-in-java – adickinson Mar 21 '19 at 12:16

1 Answers1

0

As @adickinson suggested:

import java.awt.Color;

public enum WayType {
    UNKNOWN(Color.Black), BUILDING(Color.Gray), WATER(Color.Blue), 
    COASTLINE(Color.DarkBlue), LANDUSE(Color.Green) /*... to complete*/;

    private Color color;

    private WayType(Color color) {
        this.color=color;
    }
    public Color getColor() {
        return color;
    }
}
Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • It does somehow not let me acces the getColor method from my MapCanvas class as it is not static, but it won't allow me to make it static – rasm937k Mar 21 '19 at 12:55
  • 1
    How do you call getColor ? You must call it on an instance of the enum like ''WayType.BUILDING.getColor()'', and not ''WayType.getColor()'' – Conffusion Mar 21 '19 at 13:04