diff --git a/src/eva2/gui/GraphPointSetLegend.java b/src/eva2/gui/GraphPointSetLegend.java index 5d074e02..a2db2313 100644 --- a/src/eva2/gui/GraphPointSetLegend.java +++ b/src/eva2/gui/GraphPointSetLegend.java @@ -17,6 +17,7 @@ import javax.swing.JLabel; import javax.swing.JPanel; import eva2.tools.Pair; +import eva2.tools.StringTools; import eva2.tools.chart2d.SlimRect; /** @@ -64,12 +65,12 @@ public class GraphPointSetLegend { for (int i = 0; i < pointSetContainer.size(); i++) { GraphPointSet pointset = pointSetContainer.get(i); String entryStr; - if (appendIndex) entryStr = i + ": " + pointset.getInfoString(); + if (appendIndex) entryStr = StringTools.expandPrefixZeros(i, pointSetContainer.size()-1) + ": " + pointset.getInfoString(); else entryStr = pointset.getInfoString(); legendEntries.add(new Pair(entryStr,pointset.getColor())); } } - + /** * A constructor without enumeration. * diff --git a/src/eva2/tools/StringTools.java b/src/eva2/tools/StringTools.java index ae9a5613..fa365795 100644 --- a/src/eva2/tools/StringTools.java +++ b/src/eva2/tools/StringTools.java @@ -1,5 +1,6 @@ package eva2.tools; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -41,6 +42,26 @@ public class StringTools { return (searchStringArray(arr, key, 0, ignoreCase))>=0; } + /** + * Convert an integer to a String filling it with zeros from the left, so + * that is it of the same length as the Integer maxSize would achieve. + * Note that this only works for positive values. + * + * @param index + * @param maxSize + * @return + */ + public static String expandPrefixZeros(int index, int maxSize) { + if (maxSize<10) return ""+index; + else if (maxSize<100) return ((index<10) ? "0" : "")+index; + else { + int lenZeros = (int)Math.log10(maxSize)-(int)Math.log10(index); + char[] zerArr = new char[lenZeros]; + Arrays.fill(zerArr, '0'); + return new String(zerArr)+index; + } + } + /** * Search a String array for a given String and return its index if it is found. * If it is not found, -1 is returned.