Comment #3 on issue 114 by heuermh: Minor API improvements to PText
http://code.google.com/p/piccolo2d/issues/detail?id=114
And patch for justification/horizontalAlignment:
Index: PText.java
===================================================================
--- PText.java (revision 622)
+++ PText.java (working copy)
@@ -77,12 +77,14 @@
public static Font DEFAULT_FONT = new Font("Helvetica", Font.PLAIN,
12);
public static double DEFAULT_GREEK_THRESHOLD = 5.5;
+ /** Default horizontal alignment,
<code>Component.LEFT_ALIGNMENT</code>. */
+ public static final float DEFAULT_HORIZONTAL_ALIGNMENT =
Component.LEFT_ALIGNMENT;
private String text;
private Paint textPaint;
private Font font;
protected double greekThreshold = DEFAULT_GREEK_THRESHOLD;
- private float justification = Component.LEFT_ALIGNMENT;
+ private float horizontalAlignment = DEFAULT_HORIZONTAL_ALIGNMENT;
private boolean constrainHeightToTextHeight = true;
private boolean constrainWidthToTextWidth = true;
private transient TextLayout[] lines;
@@ -98,26 +100,58 @@
setText(aText);
}
+ /** @deprecated by getHorizontalAlignment() */
+ public float getJustification() {
+ return getHorizontalAlignment();
+ }
+
+ /** @deprecated by setHorizontalAlignment(float) */
+ public void setJustification(final float justification) {
+ setHorizontalAlignment(justification);
+ }
+
/**
- * Return the justificaiton of the text in the bounds.
- *
- * @return float
+ * Return the horizontal alignment for this text node. The horizontal
alignment
will be one of
+ * <code>Component.LEFT_ALIGNMENT</code>,
<code>Component.CENTER_ALIGNMENT</code>,
+ * or <code>Component.RIGHT_ALIGNMENT</code>.
+ *
+ * @see #DEFAULT_HORIZONTAL_ALIGNMENT
+ * @return the horizontal alignment for this text node
*/
- public float getJustification() {
- return justification;
+ public float getHorizontalAlignment() {
+ return horizontalAlignment;
}
/**
- * Sets the justificaiton of the text in the bounds.
- *
- * @param just
+ * Set the horizontal alignment for this text node to
<code>horizontalAlignment</code>
+ *
+ * @param horizontalAlignment horizontal alignment, must be one of
+ * <code>Component.LEFT_ALIGNMENT</code>,
<code>Component.CENTER_ALIGNMENT</code>,
+ * or <code>Component.RIGHT_ALIGNMENT</code>
*/
- public void setJustification(final float just) {
- justification = just;
- recomputeLayout();
+ public void setHorizontalAlignment(final float horizontalAlignment) {
+ if (!validHorizontalAlignment(horizontalAlignment)) {
+ throw new IllegalArgumentException("horizontalAlignment must
be one of
Component.LEFT_ALIGNMENT, "
+ + "Component.CENTER_ALIGNMENT, or
Component.RIGHT_ALIGNMENT");
+ }
+ this.horizontalAlignment = horizontalAlignment;
}
/**
+ * Return true if the specified horizontal alignment is one of
<code>Component.LEFT_ALIGNMENT</code>,
+ * <code>Component.CENTER_ALIGNMENT</code>, or
<code>Component.RIGHT_ALIGNMENT</code>.
+ *
+ * @param horizontalAlignment horizontal alignment
+ * @return true if the specified horizontal alignment is one of
<code>Component.LEFT_ALIGNMENT</code>,
+ * <code>Component.CENTER_ALIGNMENT</code>, or
<code>Component.RIGHT_ALIGNMENT</code>
+ */
+ private static boolean validHorizontalAlignment(final float
horizontalAlignment) {
+ return Component.LEFT_ALIGNMENT == horizontalAlignment
+ || Component.CENTER_ALIGNMENT == horizontalAlignment
+ || Component.RIGHT_ALIGNMENT == horizontalAlignment;
+ }
+
+ /**
* Get the paint used to paint this nodes text.
*
* @return Paint
@@ -328,7 +362,7 @@
return;
}
- final float offset = (float) (getWidth() - tl.getAdvance()) *
justification;
+ final float offset = (float) (getWidth() - tl.getAdvance()) *
horizontalAlignment;
tl.draw(g2, x + offset, y);
y += tl.getDescent() + tl.getLeading();
FindBugs may complain about the float comparisons in
validHorizontalAlignment.
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---