private int boundedTextContainerWidth(Graphics2D graphics2D) { switch (rectangleTextPosition) { case LEFT_OF: return Math.max(baseRectangle.width, graphics2D.getFontMetrics().stringWidth(maxSingleLineStringWidth(text))); case RIGHT_OF: return Math.min(fullImageDimension.width - (baseRectangle.x + baseRectangle.width + insets.right + insets.left), baseRectangle.width); case TOP_OF: /**fall through to RIGHT_OF"&**/ case BOTTOM_OF: return baseRectangle.width; } return 0; }
private void setupFontHeight(Graphics2D graphics2D, int textContainerWidth, int maxHeightOfText) { float fontSize = requestedFont.getSize(); FontRenderContext fontRenderContext = graphics2D.getFontRenderContext(); Rectangle2D bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext); while (bounds.getWidth() > textContainerWidth || bounds.getHeight() > maxHeightOfText) { requestedFont = requestedFont.deriveFont((fontSize -= 2)); bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext); } graphics2D.setFont(requestedFont); }
private void setupFontWidth(Graphics2D graphics2D, int textContainerWidth) { float fontSize = requestedFont.getSize(); FontRenderContext fontRenderContext = graphics2D.getFontRenderContext(); Rectangle2D bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext); while (bounds.getWidth() > textContainerWidth - (insets.left + insets.right)) { requestedFont = requestedFont.deriveFont((fontSize -= 2)); bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext); } graphics2D.setFont(requestedFont); }
private void applyTextOver(Graphics2D graphics2D, Rectangle boundedTextContainer) { graphics2D.setColor(cognitiveJColourPalette.getForeground()); java.util.List<String> stringRows = Arrays.asList(text.split("\\n")); int width = boundedTextContainer.width; int height = boundedTextContainer.height; int textWidth = graphics2D.getFontMetrics().stringWidth(maxSingleLineStringWidth(text)); int textHeight = graphics2D.getFontMetrics().getHeight() / 2; int rowPadding = Math.max(0, (height - textHeight * stringRows.size()) / stringRows.size()); Point insideContainerPoint = PointLocations.TOP_CENTER.calculate( width, height, textWidth, 0, 0, 0, 0, 0 ); double yRatio = insideContainerPoint.y / (double) height; int yOffset = (int) ((1.0 - yRatio) * textHeight); for (int i = 0; i < stringRows.size(); i++) { String s = stringRows.get(i); graphics2D.drawString(s, insideContainerPoint.x + boundedTextContainer.x, insideContainerPoint.y + (rowPadding / 2) + yOffset + boundedTextContainer.y + ((textHeight + rowPadding) * i)); } }