Added support for rendering label text with a shadow or bold text style.
Fixed MultiLineLabel.getPreferredSize() to attempt to lay out the label before obtaining its preferred size, and to lay out a dirtied label lazily rather than each time a setXXX() method is called. git-svn-id: https://samskivert.googlecode.com/svn/trunk@842 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: Label.java,v 1.18 2002/07/23 03:06:59 shaper Exp $
|
// $Id: Label.java,v 1.19 2002/09/23 21:19:06 shaper Exp $
|
||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2002 Michael Bayne
|
// Copyright (C) 2002 Michael Bayne
|
||||||
@@ -55,7 +55,7 @@ import com.samskivert.util.Tuple;
|
|||||||
* text at hand. It is not a component, but is intended for use by
|
* text at hand. It is not a component, but is intended for use by
|
||||||
* components and other more heavyweight entities.
|
* components and other more heavyweight entities.
|
||||||
*/
|
*/
|
||||||
public class Label implements SwingConstants
|
public class Label implements SwingConstants, LabelStyleConstants
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constructs a blank label.
|
* Constructs a blank label.
|
||||||
@@ -119,15 +119,14 @@ public class Label implements SwingConstants
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instructs the label to render the text outlined in the specified
|
* Instructs the label to render the text with the specified alternate
|
||||||
* color when rendering. The text itself will be rendered in whatever
|
* color when rendering. The text itself will be rendered in whatever
|
||||||
* color is currently set in the graphics context, but the outline
|
* color is currently set in the graphics context, but the outline or
|
||||||
* will always be in the specified color. Set the outline color to
|
* shadow (if any) will always be in the specified color.
|
||||||
* <code>null</code> to disable outlining (which is the default).
|
|
||||||
*/
|
*/
|
||||||
public void setOutlineColor (Color color)
|
public void setAlternateColor (Color color)
|
||||||
{
|
{
|
||||||
_outlineColor = color;
|
_alternateColor = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,6 +148,15 @@ public class Label implements SwingConstants
|
|||||||
_align = align;
|
_align = align;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the style of the text within the label to one of the styles
|
||||||
|
* defined in {@link LabelStyleConstants}.
|
||||||
|
*/
|
||||||
|
public void setStyle (int style)
|
||||||
|
{
|
||||||
|
_style = style;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the target width for this label. Text will be wrapped to fit
|
* Sets the target width for this label. Text will be wrapped to fit
|
||||||
* into this width, forcibly breaking words on character boundaries if
|
* into this width, forcibly breaking words on character boundaries if
|
||||||
@@ -248,11 +256,26 @@ public class Label implements SwingConstants
|
|||||||
layouts.add(new Tuple(layout, bounds));
|
layouts.add(new Tuple(layout, bounds));
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we have an outline color, we need to be two pixels bigger in
|
switch (_style) {
|
||||||
// both directions to account for stealthy, hack outline mode
|
case OUTLINE:
|
||||||
if (_outlineColor != null) {
|
// we need to be two pixels bigger in both directions
|
||||||
_size.width += 2;
|
_size.width += 2;
|
||||||
_size.height += 2;
|
_size.height += 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SHADOW:
|
||||||
|
// we need to be one pixel bigger in both directions
|
||||||
|
_size.width += 1;
|
||||||
|
_size.height += 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BOLD:
|
||||||
|
// we need to be one pixel bigger horizontally
|
||||||
|
_size.width += 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create our layouts array
|
// create our layouts array
|
||||||
@@ -269,10 +292,10 @@ public class Label implements SwingConstants
|
|||||||
/**
|
/**
|
||||||
* Computes the lines of text for this label given the specified
|
* Computes the lines of text for this label given the specified
|
||||||
* target width. The overall size of the computed lines is stored into
|
* target width. The overall size of the computed lines is stored into
|
||||||
* the size parameter.
|
* the <code>size</code> parameter.
|
||||||
*
|
*
|
||||||
* @return an ArrayList or null if keepWordsWhole was true and
|
* @return an {@link ArrayList} or null if <code>keepWordsWhole</code>
|
||||||
* the lines could not be layed out in the target width.
|
* was true and the lines could not be layed out in the target width.
|
||||||
*/
|
*/
|
||||||
protected ArrayList computeLines (
|
protected ArrayList computeLines (
|
||||||
LineBreakMeasurer measurer, int targetWidth, Dimension size,
|
LineBreakMeasurer measurer, int targetWidth, Dimension size,
|
||||||
@@ -325,10 +348,27 @@ public class Label implements SwingConstants
|
|||||||
gfx.setColor(_textColor);
|
gfx.setColor(_textColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// shift everything over and down by one pixel if we're outlining
|
switch (_style) {
|
||||||
if (_outlineColor != null) {
|
case OUTLINE:
|
||||||
|
// shift everything over and down by one pixel if we're outlining
|
||||||
x += 1;
|
x += 1;
|
||||||
y += 1;
|
y += 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SHADOW:
|
||||||
|
// shift everything over and up by one pixel if we're drawing
|
||||||
|
// with a shadow
|
||||||
|
x += 1;
|
||||||
|
y -= 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BOLD:
|
||||||
|
// shift everything over one pixel if we're drawing in bold
|
||||||
|
x -= 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// render our text
|
// render our text
|
||||||
@@ -338,10 +378,22 @@ public class Label implements SwingConstants
|
|||||||
y += layout.getAscent();
|
y += layout.getAscent();
|
||||||
|
|
||||||
float dx = 0, extra = (float)(_size.width - lbounds.getWidth());
|
float dx = 0, extra = (float)(_size.width - lbounds.getWidth());
|
||||||
// if we're outlining, we really have two pixels less space
|
switch (_style) {
|
||||||
// than we think we do
|
case OUTLINE:
|
||||||
if (_outlineColor != null) {
|
// if we're outlining, we really have two pixels less space
|
||||||
|
// than we think we do
|
||||||
extra -= 2;
|
extra -= 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SHADOW:
|
||||||
|
case BOLD:
|
||||||
|
// if we're rendering shadowed or bolded text, we really
|
||||||
|
// have one pixel less space than we think we do
|
||||||
|
extra -= 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (_align) {
|
switch (_align) {
|
||||||
@@ -358,11 +410,12 @@ public class Label implements SwingConstants
|
|||||||
// "position" part of their bounds
|
// "position" part of their bounds
|
||||||
float rx = (float)(x + dx - lbounds.getX());
|
float rx = (float)(x + dx - lbounds.getX());
|
||||||
|
|
||||||
// render the outline using the hacky, but much nicer than
|
switch (_style) {
|
||||||
// using "real" outlines (via TextLayout.getOutline), method
|
case OUTLINE:
|
||||||
if (_outlineColor != null) {
|
// render the outline using the hacky, but much nicer than
|
||||||
|
// using "real" outlines (via TextLayout.getOutline), method
|
||||||
Color textColor = gfx.getColor();
|
Color textColor = gfx.getColor();
|
||||||
gfx.setColor(_outlineColor);
|
gfx.setColor(_alternateColor);
|
||||||
layout.draw(gfx, rx - 1, y - 1);
|
layout.draw(gfx, rx - 1, y - 1);
|
||||||
layout.draw(gfx, rx - 1, y);
|
layout.draw(gfx, rx - 1, y);
|
||||||
layout.draw(gfx, rx - 1, y + 1);
|
layout.draw(gfx, rx - 1, y + 1);
|
||||||
@@ -372,6 +425,21 @@ public class Label implements SwingConstants
|
|||||||
layout.draw(gfx, rx + 1, y);
|
layout.draw(gfx, rx + 1, y);
|
||||||
layout.draw(gfx, rx + 1, y + 1);
|
layout.draw(gfx, rx + 1, y + 1);
|
||||||
gfx.setColor(textColor);
|
gfx.setColor(textColor);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SHADOW:
|
||||||
|
textColor = gfx.getColor();
|
||||||
|
gfx.setColor(_alternateColor);
|
||||||
|
layout.draw(gfx, rx - 1, y + 1);
|
||||||
|
gfx.setColor(textColor);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BOLD:
|
||||||
|
layout.draw(gfx, rx + 1, y);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// and draw the text itself
|
// and draw the text itself
|
||||||
@@ -409,6 +477,9 @@ public class Label implements SwingConstants
|
|||||||
/** The text of the label. */
|
/** The text of the label. */
|
||||||
protected String _text;
|
protected String _text;
|
||||||
|
|
||||||
|
/** The text style. */
|
||||||
|
protected int _style;
|
||||||
|
|
||||||
/** The text alignment. */
|
/** The text alignment. */
|
||||||
protected int _align = -1; // -1 means default according to locale
|
protected int _align = -1; // -1 means default according to locale
|
||||||
|
|
||||||
@@ -428,9 +499,9 @@ public class Label implements SwingConstants
|
|||||||
/** Formatted text layout instances that contain each line of text. */
|
/** Formatted text layout instances that contain each line of text. */
|
||||||
protected Rectangle2D[] _lbounds;
|
protected Rectangle2D[] _lbounds;
|
||||||
|
|
||||||
/** The color in which to outline the text when rendering or null if
|
/** The color in which to render the text outline or shadow if we're
|
||||||
* the text should not be outlined. */
|
* rendering in outline or shadow mode. */
|
||||||
protected Color _outlineColor = null;
|
protected Color _alternateColor = null;
|
||||||
|
|
||||||
/** The color in which to render the text or null if the text should
|
/** The color in which to render the text or null if the text should
|
||||||
* be rendered with the graphics context color. */
|
* be rendered with the graphics context color. */
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
//
|
||||||
|
// $Id: LabelStyleConstants.java,v 1.1 2002/09/23 21:19:06 shaper Exp $
|
||||||
|
//
|
||||||
|
// samskivert library - useful routines for java programs
|
||||||
|
// Copyright (C) 2002 Walter Korman
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.samskivert.swing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines text style constants for use with the {@link Label} and {@link
|
||||||
|
* MultiLineLabel}.
|
||||||
|
*/
|
||||||
|
public interface LabelStyleConstants
|
||||||
|
{
|
||||||
|
/** Constant denoting normal text style. */
|
||||||
|
public static final int NORMAL = 0;
|
||||||
|
|
||||||
|
/** Constant denoting bold text style. */
|
||||||
|
public static final int BOLD = 1;
|
||||||
|
|
||||||
|
/** Constant denoting outline text style. */
|
||||||
|
public static final int OUTLINE = 2;
|
||||||
|
|
||||||
|
/** Constant denoting shadow text style. */
|
||||||
|
public static final int SHADOW = 3;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: MultiLineLabel.java,v 1.2 2002/09/18 01:19:26 shaper Exp $
|
// $Id: MultiLineLabel.java,v 1.3 2002/09/23 21:19:06 shaper Exp $
|
||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2002 Walter Korman
|
// Copyright (C) 2002 Walter Korman
|
||||||
@@ -30,12 +30,13 @@ import javax.swing.JComponent;
|
|||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
|
|
||||||
import com.samskivert.Log;
|
import com.samskivert.Log;
|
||||||
|
import com.samskivert.util.StringUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Swing component that displays a {@link Label}.
|
* A Swing component that displays a {@link Label}.
|
||||||
*/
|
*/
|
||||||
public class MultiLineLabel extends JComponent
|
public class MultiLineLabel extends JComponent
|
||||||
implements SwingConstants
|
implements SwingConstants, LabelStyleConstants
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constructs an empty multi line label.
|
* Constructs an empty multi line label.
|
||||||
@@ -65,7 +66,7 @@ public class MultiLineLabel extends JComponent
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a multi line label that displays the supplied text and
|
* Constructs a multi line label that displays the supplied text with
|
||||||
* the specified constraints and alignment. Constraints should be one
|
* the specified constraints and alignment. Constraints should be one
|
||||||
* of {@link #HORIZONTAL}, {@link #VERTICAL}, or <code>-1</code> if no
|
* of {@link #HORIZONTAL}, {@link #VERTICAL}, or <code>-1</code> if no
|
||||||
* constraints are desired.
|
* constraints are desired.
|
||||||
@@ -84,6 +85,7 @@ public class MultiLineLabel extends JComponent
|
|||||||
public void setAntiAliased (boolean antialiased)
|
public void setAntiAliased (boolean antialiased)
|
||||||
{
|
{
|
||||||
_antialiased = antialiased;
|
_antialiased = antialiased;
|
||||||
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,17 +94,16 @@ public class MultiLineLabel extends JComponent
|
|||||||
public void setText (String text)
|
public void setText (String text)
|
||||||
{
|
{
|
||||||
_label.setText(text);
|
_label.setText(text);
|
||||||
dirty();
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the outline color used to display the label text if the text
|
* Sets the alternate color used to display the label text.
|
||||||
* style is currently set to outline mode.
|
|
||||||
*/
|
*/
|
||||||
public void setOutlineColor (Color color)
|
public void setAlternateColor (Color color)
|
||||||
{
|
{
|
||||||
_label.setOutlineColor(color);
|
_label.setAlternateColor(color);
|
||||||
dirty();
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -111,7 +112,7 @@ public class MultiLineLabel extends JComponent
|
|||||||
public void setAlignment (int align)
|
public void setAlignment (int align)
|
||||||
{
|
{
|
||||||
_label.setAlignment(align);
|
_label.setAlignment(align);
|
||||||
dirty();
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,7 +121,16 @@ public class MultiLineLabel extends JComponent
|
|||||||
public void setOffAxisAlignment (int align)
|
public void setOffAxisAlignment (int align)
|
||||||
{
|
{
|
||||||
_offalign = align;
|
_offalign = align;
|
||||||
dirty();
|
_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the text style used to render this label.
|
||||||
|
*/
|
||||||
|
public void setStyle (int style)
|
||||||
|
{
|
||||||
|
_label.setStyle(style);
|
||||||
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
@@ -128,6 +138,11 @@ public class MultiLineLabel extends JComponent
|
|||||||
{
|
{
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
|
||||||
|
// if we're dirty, re-lay things out before painting ourselves
|
||||||
|
if (_dirty) {
|
||||||
|
layoutLabel();
|
||||||
|
}
|
||||||
|
|
||||||
Graphics2D gfx = (Graphics2D)g;
|
Graphics2D gfx = (Graphics2D)g;
|
||||||
int align = _label.getAlignment();
|
int align = _label.getAlignment();
|
||||||
int dx = 0, dy = 0;
|
int dx = 0, dy = 0;
|
||||||
@@ -154,11 +169,12 @@ public class MultiLineLabel extends JComponent
|
|||||||
public void doLayout ()
|
public void doLayout ()
|
||||||
{
|
{
|
||||||
super.doLayout();
|
super.doLayout();
|
||||||
|
|
||||||
switch (_constrain) {
|
switch (_constrain) {
|
||||||
case HORIZONTAL: _label.setTargetWidth(getWidth()); break;
|
case HORIZONTAL: _label.setTargetWidth(getWidth()); break;
|
||||||
case VERTICAL: _label.setTargetHeight(getHeight()); break;
|
case VERTICAL: _label.setTargetHeight(getHeight()); break;
|
||||||
}
|
}
|
||||||
dirty();
|
layoutLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,7 +182,7 @@ public class MultiLineLabel extends JComponent
|
|||||||
* accordingly like to re-layout the label, update our component's
|
* accordingly like to re-layout the label, update our component's
|
||||||
* size, and repaint everything to suit.
|
* size, and repaint everything to suit.
|
||||||
*/
|
*/
|
||||||
protected void dirty ()
|
protected void layoutLabel ()
|
||||||
{
|
{
|
||||||
Graphics2D gfx = (Graphics2D)getGraphics();
|
Graphics2D gfx = (Graphics2D)getGraphics();
|
||||||
if (gfx != null) {
|
if (gfx != null) {
|
||||||
@@ -178,15 +194,22 @@ public class MultiLineLabel extends JComponent
|
|||||||
_label.layout(gfx);
|
_label.layout(gfx);
|
||||||
gfx.dispose();
|
gfx.dispose();
|
||||||
|
|
||||||
// update our size and force a layout and repaint
|
// update our size and force a layout
|
||||||
revalidate();
|
revalidate();
|
||||||
repaint();
|
|
||||||
|
// note that we're no longer dirty
|
||||||
|
_dirty = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public Dimension getPreferredSize ()
|
public Dimension getPreferredSize ()
|
||||||
{
|
{
|
||||||
|
if (_dirty) {
|
||||||
|
// attempt to lay out the label before obtaining its preferred
|
||||||
|
// dimensions
|
||||||
|
layoutLabel();
|
||||||
|
}
|
||||||
Dimension size = _label.getSize();
|
Dimension size = _label.getSize();
|
||||||
return (size == null) ? new Dimension(10, 10) : size;
|
return (size == null) ? new Dimension(10, 10) : size;
|
||||||
}
|
}
|
||||||
@@ -205,4 +228,7 @@ public class MultiLineLabel extends JComponent
|
|||||||
|
|
||||||
/** Whether to render the label with anti-aliasing. */
|
/** Whether to render the label with anti-aliasing. */
|
||||||
protected boolean _antialiased;
|
protected boolean _antialiased;
|
||||||
|
|
||||||
|
/** Whether this label is dirty and should be re-layed out. */
|
||||||
|
protected boolean _dirty;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user