Crossfire JXClient, Trunk  R20561
GUILabel.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.label;
23 
26 import java.awt.Color;
27 import java.awt.Font;
28 import java.awt.Graphics2D;
29 import java.awt.geom.RectangularShape;
30 import java.awt.image.BufferedImage;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
38 public abstract class GUILabel extends AbstractLabel {
39 
43  private static final long serialVersionUID = 1;
44 
48  @NotNull
49  private final Alignment textAlignment;
50 
63  protected GUILabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final BufferedImage picture, @NotNull final String text, @NotNull final Font textFont, @NotNull final Color textColor, @Nullable final Color backgroundColor, @NotNull final Alignment textAlignment) {
64  super(tooltipManager, elementListener, name, text, textFont, textColor, picture, backgroundColor);
65  this.textAlignment = textAlignment;
66  }
67 
71  @Override
72  protected void textChanged() {
73  setChanged();
74  }
75 
83  protected void drawLine(@NotNull final Graphics2D g, final int y0, final int h0, @NotNull final String text) {
84  g.setBackground(new Color(0, 0, 0, 0.0f));
85  g.setFont(getTextFont());
86  g.setColor(getTextColor());
87  final RectangularShape rectangle = getTextFont().getStringBounds(text, g.getFontRenderContext());
88  final int y = y0+(int)(Math.round(h0-rectangle.getHeight())/2-rectangle.getY());
89  switch (textAlignment) {
90  case LEFT:
91  g.drawString(text, 0, y);
92  break;
93 
94  case CENTER:
95  g.drawString(text, (int)Math.round((getWidth()-rectangle.getWidth())/2), y);
96  break;
97 
98  case RIGHT:
99  g.drawString(text, (int)Math.round(getWidth()-rectangle.getWidth()), y);
100  break;
101  }
102  }
103 
104 }
Abstract base class for all label classes.
final TooltipManager tooltipManager
The TooltipManager to update.
final Font textFont
The font for rendering the label text.
void setChanged()
Records that the contents have changed and must be repainted.
void drawLine(@NotNull final Graphics2D g, final int y0, final int h0, @NotNull final String text)
Draws one line of text.
Definition: GUILabel.java:83
final GUIElementListener elementListener
The GUIElementListener to notify.
Abstract base class for labels that render text.
Definition: GUILabel.java:38
final Color backgroundColor
If set, the opaque background color.
static final long serialVersionUID
The serial version UID.
Definition: GUILabel.java:43
final Alignment textAlignment
The text alignment.
Definition: GUILabel.java:49
GUILabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final BufferedImage picture, @NotNull final String text, @NotNull final Font textFont, @NotNull final Color textColor, @Nullable final Color backgroundColor, @NotNull final Alignment textAlignment)
Creates a new instance.
Definition: GUILabel.java:63