MCPcopy Create free account
hub / github.com/Neop/mudmap2 / renderLegend

Method renderLegend

src/main/java/mudmap2/backend/legend/Legend.java:222–302  ·  view source on GitHub ↗

Renders the prepared entries @param g graphic to draw on

()

Source from the content-addressed store, hash-verified

220 * @param g graphic to draw on
221 */
222 private BufferedImage renderLegend() throws RenderException {
223 /// TODO: javadoc
224 final int numEntries = legendEntries.size();
225
226 // get sizing (maximum width -> number of columns, maximum heigth -> graphics size)
227 int maxElementWidth = 0;
228 int maxElementHeight = 0;
229
230 BufferedImage tempImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
231 Graphics tempGraphics = tempImage.getGraphics();
232
233 for(LegendEntry entry: legendEntries){
234 maxElementWidth = Math.max(maxElementWidth, entry.getWidth(tempGraphics));
235 maxElementHeight = Math.max(maxElementHeight, entry.getHeight(tempGraphics));
236 }
237
238 int numColumns;
239 int numRows;
240 int targetWidth;
241 int targetHeight;
242
243 if(orientation == Orientation.HORIZONTAL){
244 numColumns = Math.floorDiv(size - 2*ELEMENT_MARGIN_OUTSIDE,
245 maxElementWidth + ELEMENT_MARGIN_BETWEEN_HOR);
246 if(numColumns < 1) throw new RenderException(ExceptionReason.IMAGE_TOO_SMALL);
247 numRows = (int) Math.ceil((double) numEntries / (double) numColumns);
248
249 targetWidth = size;
250 targetHeight = numRows * (maxElementHeight + ELEMENT_MARGIN_BETWEEN_VERT)
251 + 2*ELEMENT_MARGIN_OUTSIDE;
252 } else { // Orientation.VERTICAL
253 numRows = Math.floorDiv(size - 2*ELEMENT_MARGIN_OUTSIDE,
254 maxElementHeight + ELEMENT_MARGIN_BETWEEN_VERT);
255 if(numRows < 1) throw new RenderException(ExceptionReason.IMAGE_TOO_SMALL);
256 numColumns = (int) Math.ceil((double) numEntries / (double) numRows);
257
258 targetWidth = numColumns * (maxElementWidth + ELEMENT_MARGIN_BETWEEN_HOR)
259 + 2*ELEMENT_MARGIN_OUTSIDE;
260 targetHeight = size;
261 }
262
263
264 // create temporary graphics
265 BufferedImage image = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
266 Graphics graphics = image.getGraphics();
267 ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
268 RenderingHints.VALUE_ANTIALIAS_ON);
269
270 // draw background
271 ((Graphics2D) graphics).setBackground(backgroundColor);
272 graphics.clearRect(0, 0, targetWidth, targetHeight);
273
274 // render entries
275 ListIterator<LegendEntry> iterator = legendEntries.listIterator();
276 int curColumn = 0;
277 int curY = ELEMENT_MARGIN_OUTSIDE;
278 int curX = ELEMENT_MARGIN_OUTSIDE;
279

Callers 1

generateMethod · 0.95

Calls 4

getWidthMethod · 0.95
getHeightMethod · 0.95
renderGraphicMethod · 0.95
maxMethod · 0.80

Tested by

no test coverage detected