MCPcopy Create free account
hub / github.com/AstroImageJ/astroimagej / drawLegend

Method drawLegend

ij/src/main/java/ij/gui/Plot.java:4004–4105  ·  view source on GitHub ↗

Draw the legend

(PlotObject legendObject, ImageProcessor ip)

Source from the content-addressed store, hash-verified

4002
4003 /** Draw the legend */
4004 void drawLegend(PlotObject legendObject, ImageProcessor ip) {
4005 ip.setFont(scFont(legendObject.getFont()));
4006 int nLabels = 0;
4007 int maxStringWidth = 0;
4008 float maxLineThickness = 0;
4009 Vector<PlotObject> usedPlotObjects = allPlotObjects;
4010 Vector<PlotObject> indexedObjects = getIndexedPlotObjects();
4011 if(indexedObjects != null)
4012 usedPlotObjects= indexedObjects;
4013
4014 for (PlotObject plotObject : usedPlotObjects)
4015 if (plotObject.type == PlotObject.XY_DATA && !plotObject.hasFlag(PlotObject.HIDDEN) && plotObject.label != null) { //label exists: was set now or previously
4016 nLabels++;
4017 String label = plotObject.label;
4018 if (indexedObjects != null)
4019 label = label.substring(label.indexOf("__") + 2);
4020 int w = ip.getStringWidth(label);
4021 if (w > maxStringWidth) maxStringWidth = w;
4022 if (plotObject.lineWidth > maxLineThickness) maxLineThickness = plotObject.lineWidth;
4023 }
4024 if (nLabels == 0) return;
4025 if (pp.antialiasedText && scale > 1) //fix incorrect width of large fonts
4026 maxStringWidth = (int)((1 + 0.004*scale) * maxStringWidth);
4027 int frameThickness = sc(legendObject.lineWidth > 0 ? legendObject.lineWidth : 1);
4028 FontMetrics fm = ip.getFontMetrics();
4029 ip.setJustification(LEFT);
4030 int lineHeight = fm.getHeight();
4031 int height = nLabels*lineHeight + 2*sc(LEGEND_PADDING);
4032 int width = maxStringWidth + sc(3*LEGEND_PADDING + LEGEND_LINELENGTH + maxLineThickness);
4033 int positionCode = legendObject.flags & LEGEND_POSITION_MASK;
4034 if (positionCode == AUTO_POSITION)
4035 positionCode = autoLegendPosition(width, height, frameThickness);
4036 Rectangle rect = legendRect(positionCode, width, height, frameThickness);
4037 int x0 = rect.x;
4038 int y0 = rect.y;
4039
4040 ip.setColor(Color.white);
4041 ip.setLineWidth(1);
4042 if (!legendObject.hasFlag(LEGEND_TRANSPARENT)) {
4043 ip.setRoi(x0, y0, width, height);
4044 ip.fill();
4045 } else if (hasFlag(X_GRID | Y_GRID)) { //erase grid
4046 int grid = ip instanceof ColorProcessor ? (gridColor.getRGB() & 0xffffff) : ip.getBestIndex(gridColor);
4047 for (int y=y0; y<y0+height; y++)
4048 for (int x=x0; x<x0+width; x++)
4049 if ((ip.getPixel(x, y) & 0xffffff) == grid)
4050 ip.drawPixel(x, y);
4051 }
4052 ip.setLineWidth(frameThickness);
4053 ip.setColor(legendObject.color);
4054 ip.drawRect(x0-frameThickness/2, y0-frameThickness/2, width+frameThickness, height);
4055 boolean bottomUp = legendObject.hasFlag(LEGEND_BOTTOM_UP);
4056 int y = y0 + frameThickness/2 + sc(LEGEND_PADDING) + lineHeight/2;
4057 if (bottomUp) y += (nLabels-1) * lineHeight;
4058 int xText = x0 + frameThickness/2 + sc(2f*LEGEND_PADDING + LEGEND_LINELENGTH + maxLineThickness);
4059 int xMarker = x0 + frameThickness/2 + sc(LEGEND_PADDING + 0.5f*(LEGEND_LINELENGTH + maxLineThickness));
4060 int xLine0 = x0 + frameThickness/2 + sc(LEGEND_PADDING) + 1;
4061 for (PlotObject plotObject : usedPlotObjects)

Callers 1

drawPlotObjectMethod · 0.95

Calls 15

scFontMethod · 0.95
getIndexedPlotObjectsMethod · 0.95
scMethod · 0.95
autoLegendPositionMethod · 0.95
legendRectMethod · 0.95
hasFlagMethod · 0.95
fillShapeMethod · 0.95
drawShapeMethod · 0.95
substringMethod · 0.80
getRGBMethod · 0.80
drawRectMethod · 0.80
fillRectMethod · 0.80

Tested by

no test coverage detected