mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
Label vertical and horizontal alignment can be changed
This commit is contained in:
+64
-16
@@ -28,12 +28,18 @@ public class Label extends AbstractComponent<Void> {
|
|||||||
private String text = DEFAULT_TEXT;
|
private String text = DEFAULT_TEXT;
|
||||||
private Font font = LABEL_FONT;
|
private Font font = LABEL_FONT;
|
||||||
private Color color = LABEL_COLOR;
|
private Color color = LABEL_COLOR;
|
||||||
private boolean center = true;
|
@Deprecated
|
||||||
|
private boolean center;
|
||||||
|
private HorizontalAlignment horizontalAlignment = HorizontalAlignment.Center;
|
||||||
|
private VerticalAlignment verticalAlignment = VerticalAlignment.Center;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode,
|
public void draw(Graphics2D g2d, ComponentState componentState,
|
||||||
Project project, IDrawingObserver drawingObserver) {
|
boolean outlineMode, Project project,
|
||||||
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
|
IDrawingObserver drawingObserver) {
|
||||||
|
g2d
|
||||||
|
.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED
|
||||||
|
: color);
|
||||||
g2d.setFont(font);
|
g2d.setFont(font);
|
||||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||||
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
|
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
|
||||||
@@ -41,15 +47,35 @@ public class Label extends AbstractComponent<Void> {
|
|||||||
int textHeight = (int) rect.getHeight();
|
int textHeight = (int) rect.getHeight();
|
||||||
int textWidth = (int) rect.getWidth();
|
int textWidth = (int) rect.getWidth();
|
||||||
|
|
||||||
int x;
|
int x = point.x;
|
||||||
int y;
|
int y = point.y;
|
||||||
if (center) {
|
switch (verticalAlignment) {
|
||||||
// Center text horizontally and vertically
|
case Center:
|
||||||
x = point.x - textWidth / 2;
|
|
||||||
y = point.y - textHeight / 2 + fontMetrics.getAscent();
|
y = point.y - textHeight / 2 + fontMetrics.getAscent();
|
||||||
} else {
|
break;
|
||||||
|
case Top:
|
||||||
|
y = point.y - textHeight + fontMetrics.getAscent();
|
||||||
|
break;
|
||||||
|
case Bottom:
|
||||||
|
y = point.y + fontMetrics.getAscent();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("Unexpected alignment: "
|
||||||
|
+ verticalAlignment);
|
||||||
|
}
|
||||||
|
switch (horizontalAlignment) {
|
||||||
|
case Center:
|
||||||
|
x = point.x - textWidth / 2;
|
||||||
|
break;
|
||||||
|
case Left:
|
||||||
x = point.x;
|
x = point.x;
|
||||||
y = point.y - textHeight;
|
break;
|
||||||
|
case Right:
|
||||||
|
x = point.x - textWidth;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("Unexpected alignment: "
|
||||||
|
+ horizontalAlignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
g2d.drawString(text, x, y);
|
g2d.drawString(text, x, y);
|
||||||
@@ -58,7 +84,8 @@ public class Label extends AbstractComponent<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public void drawIcon(Graphics2D g2d, int width, int height) {
|
public void drawIcon(Graphics2D g2d, int width, int height) {
|
||||||
g2d.setColor(LABEL_COLOR);
|
g2d.setColor(LABEL_COLOR);
|
||||||
g2d.setFont(LABEL_FONT.deriveFont(13f * width / 32).deriveFont(Font.PLAIN));
|
g2d.setFont(LABEL_FONT.deriveFont(13f * width / 32).deriveFont(
|
||||||
|
Font.PLAIN));
|
||||||
|
|
||||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||||
Rectangle2D rect = fontMetrics.getStringBounds("Abc", g2d);
|
Rectangle2D rect = fontMetrics.getStringBounds("Abc", g2d);
|
||||||
@@ -179,12 +206,25 @@ public class Label extends AbstractComponent<Void> {
|
|||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getCenter() {
|
@EditableProperty(name = "Vertical alignment")
|
||||||
return center;
|
public VerticalAlignment getVerticalAlignment() {
|
||||||
|
return verticalAlignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCenter(boolean center) {
|
public void setVerticalAlignment(VerticalAlignment verticalAlignment) {
|
||||||
this.center = center;
|
this.verticalAlignment = verticalAlignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EditableProperty(name = "Horizontal alignment")
|
||||||
|
public HorizontalAlignment getHorizontalAlignment() {
|
||||||
|
if (horizontalAlignment == null) {
|
||||||
|
horizontalAlignment = HorizontalAlignment.Center;
|
||||||
|
}
|
||||||
|
return horizontalAlignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHorizontalAlignment(HorizontalAlignment alignment) {
|
||||||
|
this.horizontalAlignment = alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -200,4 +240,12 @@ public class Label extends AbstractComponent<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public void setValue(Void value) {
|
public void setValue(Void value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum HorizontalAlignment {
|
||||||
|
Left, Center, Right;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum VerticalAlignment {
|
||||||
|
Top, Center, Bottom;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user