📄 texttitle.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* --------------
* TextTitle.java
* --------------
* (C) Copyright 2000-2007, by David Berry and Contributors.
*
* Original Author: David Berry;
* Contributor(s): David Gilbert (for Object Refinery Limited);
* Nicolas Brodu;
*
* $Id: TextTitle.java,v 1.16.2.7 2007/04/04 10:44:11 mungady Exp $
*
* Changes (from 18-Sep-2001)
* --------------------------
* 18-Sep-2001 : Added standard header (DG);
* 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now
* requires jcommon.jar (DG);
* 09-Jan-2002 : Updated Javadoc comments (DG);
* 07-Feb-2002 : Changed Insets --> Spacer in AbstractTitle.java (DG);
* 06-Mar-2002 : Updated import statements (DG);
* 25-Jun-2002 : Removed redundant imports (DG);
* 18-Sep-2002 : Fixed errors reported by Checkstyle (DG);
* 28-Oct-2002 : Small modifications while changing JFreeChart class (DG);
* 13-Mar-2003 : Changed width used for relative spacing to fix bug 703050 (DG);
* 26-Mar-2003 : Implemented Serializable (DG);
* 15-Jul-2003 : Fixed null pointer exception (DG);
* 11-Sep-2003 : Implemented Cloneable (NB)
* 22-Sep-2003 : Added checks for null values and throw nullpointer
* exceptions (TM);
* Background paint was not serialized.
* 07-Oct-2003 : Added fix for exception caused by empty string in title (DG);
* 29-Oct-2003 : Added workaround for text alignment in PDF output (DG);
* 03-Feb-2004 : Fixed bug in getPreferredWidth() method (DG);
* 17-Feb-2004 : Added clone() method and fixed bug in equals() method (DG);
* 01-Apr-2004 : Changed java.awt.geom.Dimension2D to org.jfree.ui.Size2D
* because of JDK bug 4976448 which persists on JDK 1.3.1. Also
* fixed bug in getPreferredHeight() method (DG);
* 29-Apr-2004 : Fixed bug in getPreferredWidth() method - see bug id
* 944173 (DG);
* 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
* release (DG);
* 08-Feb-2005 : Updated for changes in RectangleConstraint class (DG);
* 11-Feb-2005 : Implemented PublicCloneable (DG);
* 20-Apr-2005 : Added support for tooltips (DG);
* 26-Apr-2005 : Removed LOGGER (DG);
* 06-Jun-2005 : Modified equals() to handle GradientPaint (DG);
* 06-Jul-2005 : Added flag to control whether or not the title expands to
* fit the available space (DG);
* 07-Oct-2005 : Added textAlignment attribute (DG);
* ------------- JFREECHART 1.0.x RELEASED ------------------------------------
* 13-Dec-2005 : Fixed bug 1379331 - incorrect drawing with LEFT or RIGHT
* title placement (DG);
*
*/
package org.jfree.chart.title;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.jfree.chart.block.BlockResult;
import org.jfree.chart.block.EntityBlockParams;
import org.jfree.chart.block.LengthConstraintType;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.event.TitleChangeEvent;
import org.jfree.data.Range;
import org.jfree.io.SerialUtilities;
import org.jfree.text.G2TextMeasurer;
import org.jfree.text.TextBlock;
import org.jfree.text.TextBlockAnchor;
import org.jfree.text.TextUtilities;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.Size2D;
import org.jfree.ui.VerticalAlignment;
import org.jfree.util.ObjectUtilities;
import org.jfree.util.PaintUtilities;
import org.jfree.util.PublicCloneable;
/**
* A chart title that displays a text string with automatic wrapping as
* required.
*/
public class TextTitle extends Title
implements Serializable, Cloneable, PublicCloneable {
/** For serialization. */
private static final long serialVersionUID = 8372008692127477443L;
/** The default font. */
public static final Font DEFAULT_FONT = new Font("SansSerif", Font.BOLD,
12);
/** The default text color. */
public static final Paint DEFAULT_TEXT_PAINT = Color.black;
/** The title text. */
private String text;
/** The font used to display the title. */
private Font font;
/** The text alignment. */
private HorizontalAlignment textAlignment;
/** The paint used to display the title text. */
private transient Paint paint;
/** The background paint. */
private transient Paint backgroundPaint;
/** The tool tip text (can be <code>null</code>). */
private String toolTipText;
/** The URL text (can be <code>null</code>). */
private String urlText;
/** The content. */
private TextBlock content;
/**
* A flag that controls whether the title expands to fit the available
* space..
*/
private boolean expandToFitSpace = false;
/**
* Creates a new title, using default attributes where necessary.
*/
public TextTitle() {
this("");
}
/**
* Creates a new title, using default attributes where necessary.
*
* @param text the title text (<code>null</code> not permitted).
*/
public TextTitle(String text) {
this(text, TextTitle.DEFAULT_FONT, TextTitle.DEFAULT_TEXT_PAINT,
Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT,
Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
}
/**
* Creates a new title, using default attributes where necessary.
*
* @param text the title text (<code>null</code> not permitted).
* @param font the title font (<code>null</code> not permitted).
*/
public TextTitle(String text, Font font) {
this(text, font, TextTitle.DEFAULT_TEXT_PAINT, Title.DEFAULT_POSITION,
Title.DEFAULT_HORIZONTAL_ALIGNMENT,
Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
}
/**
* Creates a new title.
*
* @param text the text for the title (<code>null</code> not permitted).
* @param font the title font (<code>null</code> not permitted).
* @param paint the title paint (<code>null</code> not permitted).
* @param position the title position (<code>null</code> not permitted).
* @param horizontalAlignment the horizontal alignment (<code>null</code>
* not permitted).
* @param verticalAlignment the vertical alignment (<code>null</code> not
* permitted).
* @param padding the space to leave around the outside of the title.
*/
public TextTitle(String text, Font font, Paint paint,
RectangleEdge position,
HorizontalAlignment horizontalAlignment,
VerticalAlignment verticalAlignment,
RectangleInsets padding) {
super(position, horizontalAlignment, verticalAlignment, padding);
if (text == null) {
throw new NullPointerException("Null 'text' argument.");
}
if (font == null) {
throw new NullPointerException("Null 'font' argument.");
}
if (paint == null) {
throw new NullPointerException("Null 'paint' argument.");
}
this.text = text;
this.font = font;
this.paint = paint;
// the textAlignment and the horizontalAlignment are separate things,
// but it makes sense for the default textAlignment to match the
// title's horizontal alignment...
this.textAlignment = horizontalAlignment;
this.backgroundPaint = null;
this.content = null;
this.toolTipText = null;
this.urlText = null;
}
/**
* Returns the title text.
*
* @return The text (never <code>null</code>).
*
* @see #setText(String)
*/
public String getText() {
return this.text;
}
/**
* Sets the title to the specified text and sends a
* {@link TitleChangeEvent} to all registered listeners.
*
* @param text the text (<code>null</code> not permitted).
*/
public void setText(String text) {
if (text == null) {
throw new IllegalArgumentException("Null 'text' argument.");
}
if (!this.text.equals(text)) {
this.text = text;
notifyListeners(new TitleChangeEvent(this));
}
}
/**
* Returns the text alignment. This controls how the text is aligned
* within the title's bounds, whereas the title's horizontal alignment
* controls how the title's bounding rectangle is aligned within the
* drawing space.
*
* @return The text alignment.
*/
public HorizontalAlignment getTextAlignment() {
return this.textAlignment;
}
/**
* Sets the text alignment.
*
* @param alignment the alignment (<code>null</code> not permitted).
*/
public void setTextAlignment(HorizontalAlignment alignment) {
if (alignment == null) {
throw new IllegalArgumentException("Null 'alignment' argument.");
}
this.textAlignment = alignment;
notifyListeners(new TitleChangeEvent(this));
}
/**
* Returns the font used to display the title string.
*
* @return The font (never <code>null</code>).
*
* @see #setFont(Font)
*/
public Font getFont() {
return this.font;
}
/**
* Sets the font used to display the title string. Registered listeners
* are notified that the title has been modified.
*
* @param font the new font (<code>null</code> not permitted).
*
* @see #getFont()
*/
public void setFont(Font font) {
if (font == null) {
throw new IllegalArgumentException("Null 'font' argument.");
}
if (!this.font.equals(font)) {
this.font = font;
notifyListeners(new TitleChangeEvent(this));
}
}
/**
* Returns the paint used to display the title string.
*
* @return The paint (never <code>null</code>).
*
* @see #setPaint(Paint)
*/
public Paint getPaint() {
return this.paint;
}
/**
* Sets the paint used to display the title string. Registered listeners
* are notified that the title has been modified.
*
* @param paint the new paint (<code>null</code> not permitted).
*
* @see #getPaint()
*/
public void setPaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
if (!this.paint.equals(paint)) {
this.paint = paint;
notifyListeners(new TitleChangeEvent(this));
}
}
/**
* Returns the background paint.
*
* @return The paint (possibly <code>null</code>).
*/
public Paint getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Sets the background paint and sends a {@link TitleChangeEvent} to all
* registered listeners. If you set this attribute to <code>null</code>,
* no background is painted (which makes the title background transparent).
*
* @param paint the background paint (<code>null</code> permitted).
*/
public void setBackgroundPaint(Paint paint) {
this.backgroundPaint = paint;
notifyListeners(new TitleChangeEvent(this));
}
/**
* Returns the tool tip text.
*
* @return The tool tip text (possibly <code>null</code>).
*/
public String getToolTipText() {
return this.toolTipText;
}
/**
* Sets the tool tip text to the specified text and sends a
* {@link TitleChangeEvent} to all registered listeners.
*
* @param text the text (<code>null</code> permitted).
*/
public void setToolTipText(String text) {
this.toolTipText = text;
notifyListeners(new TitleChangeEvent(this));
}
/**
* Returns the URL text.
*
* @return The URL text (possibly <code>null</code>).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -