1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| public class JText extends JComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
public enum VerticalAlignmentType {
BASELINE,
FONTHEIGHT,
TEXTHEIGHT
}
private String text;
private int halign;
private int valign;
private boolean borderBox;
private VerticalAlignmentType vcentermode;
public JText() {
this("");
}
public JText(String text) {
this(text, SwingConstants.CENTER, SwingConstants.CENTER);
}
public JText(String text, int halign, int valign) {
this.text=text==null?"":text;
this.halign = halign;
this.valign = valign;
this.borderBox = false;
this.vcentermode = VerticalAlignmentType.FONTHEIGHT;
setOpaque(false);
setPreferredSize(null);
}
public void setText(String text) {
text = text==null?"":text;
String oldValue = this.text;
this.text = text;
if ( !Objects.equals(oldValue, text) ) {
firePropertyChange("TEXT", oldValue, text);
repaint();
}
}
public void setBorderBox(boolean borderBox) {
boolean oldValue = this.borderBox;
this.borderBox = borderBox;
if ( oldValue!=borderBox ) {
firePropertyChange("BORDERBOX", oldValue, borderBox);
repaint();
}
}
public boolean isBorderBox() {
return borderBox;
}
public void setTextVerticalCenterMode(VerticalAlignmentType vcentermode) {
VerticalAlignmentType oldValue = this.vcentermode;
this.vcentermode = vcentermode==null?VerticalAlignmentType.FONTHEIGHT:vcentermode;
if ( oldValue!=vcentermode ) {
firePropertyChange("VERTICAL_CENTER_MODE", oldValue, vcentermode);
repaint();
}
}
public VerticalAlignmentType getTextVerticalCenterMode() {
return vcentermode;
}
@Override
public Dimension getPreferredSize() {
if ( isPreferredSizeSet() ) {
return super.getPreferredSize();
}
else {
return computePrefSize();
}
}
private Dimension computePrefSize() {
Font font = getFont();
FontMetrics fontMetrics = getFontMetrics(font);
return new Dimension(fontMetrics.stringWidth(text),fontMetrics.getHeight());
}
@Override
public void paint(Graphics g) {
super.paint(g);
if ( isOpaque() && getBackground()!=null ) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
paintBorder(g);
paintComponent(g);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getForeground());
Insets insets = getInsets();
Rectangle2D bounds;
if ( borderBox ) {
bounds = new Rectangle2D.Double(insets.left, insets.top,
getWidth()-insets.left-insets.right,
getHeight()-insets.top-insets.bottom);
}
else {
bounds = new Rectangle2D.Double(0, 0,
getWidth(),
getHeight());
}
Graphics2D g2d = (Graphics2D)g;
FontMetrics fontMetrics = g.getFontMetrics();
Rectangle2D textBounds = fontMetrics.getStringBounds(text, g2d);
double textWidth = textBounds.getWidth();
double x,y;
switch (halign) {
case SwingConstants.LEFT:
x = bounds.getX();
break;
case SwingConstants.RIGHT:
x = bounds.getX() + bounds.getWidth() - textWidth;
break;
case SwingConstants.CENTER:
default:
x = bounds.getX()
+ (bounds.getWidth() - textWidth) / 2;
break;
}
switch (valign) {
case SwingConstants.TOP:
y = bounds.getY() + fontMetrics.getAscent() - fontMetrics.getDescent();
break;
case SwingConstants.BOTTOM:
y = bounds.getY()
+ bounds.getHeight() - fontMetrics.getDescent() ;
break;
case SwingConstants.CENTER:
default:
double height;
switch(vcentermode) {
case BASELINE:
height= fontMetrics.getAscent() + fontMetrics.getDescent();
break;
case TEXTHEIGHT:
height=textBounds.getHeight();
break;
case FONTHEIGHT:
default:
height=fontMetrics.getHeight();
break;
}
y = bounds.getY()
+ bounds.getHeight()/ 2 - height / 2 + fontMetrics.getAscent() ;
break;
}
g2d.drawString(text, (int)x, (int)y);
}
} |
Partager