+ drawLine(g, dashedLine(charWidth), 0, y2, (eRes - sRes) * charWidth,
+ y2);
+ }
+ g2d.setStroke(prevStroke);
+ }
+
+ private static double log2 = Math.log(2);
+
+ // Cached dashed line Strokes
+ private static Map<Integer, Stroke> dashedLineLookup = new HashMap<>();
+
+ /**
+ * Returns a dashed line stroke as close to 6-4 pixels as fits within the
+ * charWidth. This allows translations of multiples of charWidth without
+ * disrupting the dashed line. The exact values are 0.6-0.4 proportions of
+ * charWidth for charWidth under 16. For charWidth 16 or over, the number of
+ * dashes doubles as charWidth doubles.
+ *
+ * @param charWidth
+ * @return Stroke with appropriate dashed line fitting exactly within the
+ * charWidth
+ */
+ private static Stroke dashedLine(int charWidth)
+ {
+ if (!dashedLineLookup.containsKey(charWidth))
+ {
+ int power2 = charWidth >= 8 ? (int) (Math.log(charWidth) / log2) : 2;
+ float width = ((float) charWidth) / ((float) Math.pow(2, power2 - 3));
+ float segment1 = width * 0.6f;
+ float segment2 = width - segment1;
+ dashedLineLookup.put(charWidth, new BasicStroke(1,
+ BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]
+ { segment1, segment2 }, 0f));
+ }
+ return dashedLineLookup.get(charWidth);
+ }
+
+ private void drawSegmentedLine(Graphics g,
+ List<Map.Entry<Float, Color>> valCols, int x1, int y1, int x2,
+ int y2)
+ {
+ if (valCols == null || valCols.size() == 0)
+ {
+ return;
+ }
+ // let's only go forwards+up|down -- try and avoid providing right to left
+ // x values
+ if (x2 < x1)
+ {
+ int tmp = y2;
+ y2 = y1;
+ y1 = tmp;
+ tmp = x2;
+ x2 = x1;
+ x1 = tmp;
+ Collections.reverse(valCols);
+ }
+ Graphics2D g2d = (Graphics2D) g.create();
+ float yd = y2 - y1;
+ boolean reverse = yd > 0; // reverse => line going DOWN (y increasing)
+ Map.Entry<Float, Color> firstValCol = valCols.remove(0);
+ float firstVal = firstValCol.getKey();
+ Color firstCol = firstValCol.getValue();
+ int yy1 = 0;
+ yy1 = reverse ? (int) Math.ceil(y1 + firstVal * yd)
+ : (int) Math.floor(y1 + firstVal * yd);
+ Color thisCol = firstCol;
+ for (int i = 0; i < valCols.size(); i++)
+ {
+ Map.Entry<Float, Color> valCol = valCols.get(i);
+ float val = valCol.getKey();
+ Color col = valCol.getValue();
+ int clipX = x1 - 1;
+ int clipW = x2 - x1 + 2;
+ int clipY = 0;
+ int clipH = 0;
+ int yy2 = 0;
+ if (reverse) // line going down
+ {
+ yy2 = (int) Math.ceil(y1 + val * yd);
+ g2d.setColor(thisCol);
+ clipY = yy1 - 1;
+ clipH = yy2 - yy1;
+ if (i == 0)
+ {
+ // highest segment, don't clip at the top
+ clipY -= 2;
+ clipH += 2;
+ }
+ if (i == valCols.size() - 1)
+ {
+ // lowest segment, don't clip at the bottom
+ clipH += 2;
+ }
+ }
+ else // line going up (or level)
+ {
+ yy2 = (int) Math.floor(y1 + val * yd);
+ // g2d.setColor(Color.cyan); g2d.drawRect(x1 - 1, yy1, x2 - x1 + 1, yy2
+ // -
+ // yy1 + 1);
+ g2d.setColor(col);
+ clipY = yy2;
+ clipH = yy1 - yy2;
+ if (i == 0)
+ {
+ // lowest segment, don't clip at the bottom
+ clipH += 2;
+ }
+ if (i == valCols.size() - 1)
+ {
+ // highest segment, don't clip at the top
+ clipY -= 2;
+ clipH += 2;
+ }
+ }
+ g2d.setClip(clipX, clipY, clipW, clipH);
+ drawLine(g2d, x1, y1, x2, y2);
+ yy1 = yy2;
+ thisCol = col;