📄 texttitle.java
字号:
throw new IllegalArgumentException(
"TextTitle.setPaint(...): null paint not permitted."
);
}
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 preferred width of the title. This will only be called when the title
* is being drawn at the left or right of a chart.
*
* @param g2 the graphics device.
* @param height the height.
*
* @return The preferred width of the title.
*/
public float getPreferredWidth(Graphics2D g2, float height) {
float result = 0.0f;
if (this.text != null && !this.text.equals("")) {
g2.setFont(this.font);
TextBlock title = TextUtilities.createTextBlock(
this.text, this.font, this.paint, height, new G2TextMeasurer(g2)
);
Size2D d = title.calculateDimensions(g2);
result = (float) getSpacer().getAdjustedWidth(d.getHeight());
// use height here because the title
// is displayed rotated
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Title preferred width = " + result);
}
return result;
}
/**
* Returns the preferred height of the title.
*
* @param g2 the graphics device.
* @param width the width.
*
* @return The preferred height of the title.
*/
public float getPreferredHeight(Graphics2D g2, float width) {
float result = 0.0f;
if (this.text != null && !this.text.equals("")) {
g2.setFont(this.font);
float textWidth = (float) getSpacer().trimWidth(width);
TextBlock title = TextUtilities.createTextBlock(
this.text, this.font, this.paint, textWidth, new G2TextMeasurer(g2)
);
Size2D d = title.calculateDimensions(g2);
result = (float) getSpacer().getAdjustedHeight(d.getHeight());
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Title preferred height = " + result);
}
return result;
}
/**
* Draws the title on a Java 2D graphics device (such as the screen or a printer).
*
* @param g2 the graphics device.
* @param area the area allocated for the title.
*/
public void draw(Graphics2D g2, Rectangle2D area) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Drawing title to area " + area.toString());
}
if (this.text.equals("")) {
return;
}
if (this.backgroundPaint != null) {
g2.setPaint(this.backgroundPaint);
g2.fill(area);
}
RectangleEdge position = getPosition();
if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) {
drawHorizontal(g2, area);
}
else if (position == RectangleEdge.LEFT || position == RectangleEdge.RIGHT) {
drawVertical(g2, area);
}
}
/**
* Draws a the title horizontally within the specified area. This method will be called
* from the {@link #draw(Graphics2D, Rectangle2D) draw} method.
*
* @param g2 the graphics device.
* @param area the area for the title.
*/
protected void drawHorizontal(Graphics2D g2, Rectangle2D area) {
Rectangle2D titleArea = (Rectangle2D) area.clone();
getSpacer().trim(titleArea);
g2.setFont(this.font);
g2.setPaint(this.paint);
TextBlock title = TextUtilities.createTextBlock(
this.text, this.font, this.paint, (float) titleArea.getWidth(), new G2TextMeasurer(g2)
);
TextBlockAnchor anchor = null;
float x = 0.0f;
HorizontalAlignment horizontalAlignment = getHorizontalAlignment();
if (horizontalAlignment == HorizontalAlignment.LEFT) {
x = (float) titleArea.getX();
anchor = TextBlockAnchor.TOP_LEFT;
}
else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
x = (float) titleArea.getMaxX();
anchor = TextBlockAnchor.TOP_RIGHT;
}
else if (horizontalAlignment == HorizontalAlignment.CENTER) {
x = (float) titleArea.getCenterX();
anchor = TextBlockAnchor.TOP_CENTER;
}
float y = 0.0f;
RectangleEdge position = getPosition();
if (position == RectangleEdge.TOP) {
y = (float) titleArea.getY();
}
else if (position == RectangleEdge.BOTTOM) {
y = (float) titleArea.getMaxY();
if (horizontalAlignment == HorizontalAlignment.LEFT) {
anchor = TextBlockAnchor.BOTTOM_LEFT;
}
else if (horizontalAlignment == HorizontalAlignment.CENTER) {
anchor = TextBlockAnchor.BOTTOM_CENTER;
}
else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
anchor = TextBlockAnchor.BOTTOM_RIGHT;
}
}
title.draw(g2, x, y, anchor);
}
/**
* Draws a the title vertically within the specified area. This method will be called
* from the {@link #draw(Graphics2D, Rectangle2D) draw} method.
*
* @param g2 the graphics device.
* @param area the area for the title.
*/
protected void drawVertical(Graphics2D g2, Rectangle2D area) {
Rectangle2D titleArea = (Rectangle2D) area.clone();
getSpacer().trim(titleArea);
g2.setFont(this.font);
g2.setPaint(this.paint);
TextBlock title = TextUtilities.createTextBlock(
this.text, this.font, this.paint, (float) titleArea.getHeight(), new G2TextMeasurer(g2)
);
TextBlockAnchor anchor = null;
float y = 0.0f;
VerticalAlignment verticalAlignment = getVerticalAlignment();
if (verticalAlignment == VerticalAlignment.TOP) {
y = (float) titleArea.getY();
anchor = TextBlockAnchor.TOP_RIGHT;
}
else if (verticalAlignment == VerticalAlignment.BOTTOM) {
y = (float) titleArea.getMaxY();
anchor = TextBlockAnchor.TOP_LEFT;
}
else if (verticalAlignment == VerticalAlignment.CENTER) {
y = (float) titleArea.getCenterY();
anchor = TextBlockAnchor.TOP_CENTER;
}
float x = 0.0f;
RectangleEdge position = getPosition();
if (position == RectangleEdge.LEFT) {
x = (float) titleArea.getX();
}
else if (position == RectangleEdge.RIGHT) {
x = (float) titleArea.getMaxX();
if (verticalAlignment == VerticalAlignment.TOP) {
anchor = TextBlockAnchor.BOTTOM_RIGHT;
}
else if (verticalAlignment == VerticalAlignment.CENTER) {
anchor = TextBlockAnchor.BOTTOM_CENTER;
}
else if (verticalAlignment == VerticalAlignment.BOTTOM) {
anchor = TextBlockAnchor.BOTTOM_LEFT;
}
}
title.draw(g2, x, y, anchor, x, y, -Math.PI / 2.0);
}
/**
* Tests this title for equality with another object.
*
* @param obj the object.
*
* @return <code>true</code> or <code>false</code>.
*/
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj instanceof TextTitle) {
TextTitle t = (TextTitle) obj;
if (super.equals(obj)) {
if (!ObjectUtils.equal(this.text, t.text)) {
return false;
}
if (!ObjectUtils.equal(this.font, t.font)) {
return false;
}
if (!ObjectUtils.equal(this.paint, t.paint)) {
return false;
}
if (!ObjectUtils.equal(this.backgroundPaint, t.backgroundPaint)) {
return false;
}
return true;
}
}
return false;
}
/**
* Returns a hash code.
*
* @return A hash code.
*/
public int hashCode() {
int result = super.hashCode();
result = 29 * result + (this.text != null ? this.text.hashCode() : 0);
result = 29 * result + (this.font != null ? this.font.hashCode() : 0);
result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0);
result = 29 * result + (this.backgroundPaint != null ? this.backgroundPaint.hashCode() : 0);
return result;
}
/**
* Returns a clone of this object.
*
* @return A clone.
*
* @throws CloneNotSupportedException never.
*/
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writePaint(this.paint, stream);
SerialUtilities.writePaint(this.backgroundPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.paint = SerialUtilities.readPaint(stream);
this.backgroundPaint = SerialUtilities.readPaint(stream);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -