centered text

This commit is contained in:
Greg DiCristofaro 2020-09-22 15:10:49 -04:00
parent d538575819
commit 7cc7934c4f
2 changed files with 27 additions and 3 deletions

View File

@ -38,7 +38,6 @@ public class BaseMessageOverlay {
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
label.setOpaque(false);
}
/**
@ -76,12 +75,32 @@ public class BaseMessageOverlay {
* @param height The height.
*/
public void paintOverlay(Graphics g, int width, int height) {
paintOverlay(g, width, height, null);
}
/**
* Paints the jlabel at full width and height with the graphics object.
*
* @param g The graphics object.
* @param parentWidth The width of the component.
* @param parentHeight The height of the component.
* @param labelMaxWidth The maximum width of the label drawn for the
* overlay. The label will be vertically and
* horizontally centered.
*/
public void paintOverlay(Graphics g, int parentWidth, int parentHeight, Integer labelMaxWidth) {
if (!visible) {
return;
}
int labelWidth = (labelMaxWidth == null) ? parentWidth : Math.min(labelMaxWidth, parentWidth);
int leftPad = (parentWidth - labelWidth) / 2;
// paint the jlabel if visible.
label.setBounds(0, 0, width, height);
g.translate(leftPad, 0);
label.setBounds(0, 0, labelWidth, parentHeight);
g.translate(0, 0);
label.paint(g);
}
}

View File

@ -98,6 +98,10 @@ public class PieChartPanel extends AbstractLoadableComponent<List<PieChartPanel.
private static final long serialVersionUID = 1L;
private final BaseMessageOverlay overlay = new BaseMessageOverlay();
// multiply this value by the smaller dimension (height or width) of the component
// to determine width of text to be displayed.
private static final double MESSAGE_WIDTH_FACTOR = .6;
/**
* Sets this layer visible when painted. In order to be shown in UI,
* this component needs to be repainted.
@ -119,7 +123,8 @@ public class PieChartPanel extends AbstractLoadableComponent<List<PieChartPanel.
@Override
public void paintOverlay(Graphics2D gd, ChartPanel cp) {
overlay.paintOverlay(gd, cp.getWidth(), cp.getHeight());
int labelWidth = (int) (Math.min(cp.getWidth(), cp.getHeight()) * MESSAGE_WIDTH_FACTOR);
overlay.paintOverlay(gd, cp.getWidth(), cp.getHeight(), labelWidth);
}
}