Crossfire JXClient, Trunk  R20561
AbstractLabel.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 
27 import java.awt.Color;
28 import java.awt.Font;
29 import java.awt.Graphics;
30 import java.awt.Transparency;
31 import java.awt.image.BufferedImage;
32 import javax.swing.ImageIcon;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
42 public abstract class AbstractLabel extends AbstractGUIElement {
43 
47  private static final long serialVersionUID = 1;
48 
52  @NotNull
53  private String text;
54 
58  @NotNull
59  private final Font textFont;
60 
64  @NotNull
65  private final Color textColor;
66 
70  @Nullable
71  private ImageIcon backgroundImage;
72 
77  @Nullable
78  private final Color backgroundColor;
79 
92  protected AbstractLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final String text, @NotNull final Font textFont, @NotNull final Color textColor, @Nullable final BufferedImage backgroundPicture, @Nullable final Color backgroundColor) {
93  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
94  this.text = text;
95  this.textFont = textFont;
96  this.textColor = textColor;
97  backgroundImage = backgroundPicture == null ? null : new ImageIcon(backgroundPicture);
98  this.backgroundColor = backgroundColor;
99  }
100 
105  public void setText(@NotNull final String text) {
106  if (!this.text.equals(text)) {
107  this.text = text;
108  textChanged();
109  }
110  }
111 
115  protected abstract void textChanged();
116 
121  @NotNull
122  protected String getText() {
123  return text;
124  }
125 
130  @NotNull
131  protected Font getTextFont() {
132  return textFont;
133  }
134 
139  @NotNull
140  protected Color getTextColor() {
141  return textColor;
142  }
143 
147  @Override
148  public void paintComponent(@NotNull final Graphics g) {
149  super.paintComponent(g);
150 
151  if (backgroundImage != null) {
152  g.drawImage(backgroundImage.getImage(), 0, 0, null);
153  } else if (backgroundColor != null) {
154  g.setColor(backgroundColor);
155  g.fillRect(0, 0, getWidth(), getHeight());
156  }
157  }
158 
164  protected void setBackgroundImage(@Nullable final ImageIcon backgroundImage) {
165  this.backgroundImage = backgroundImage;
166  setChanged();
167  }
168 
169 }
Abstract base class for all label classes.
void setText(@NotNull final String text)
The label text.
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.
abstract void textChanged()
Will be called whenever text has changed.
final GUIElementListener elementListener
The GUIElementListener to notify.
AbstractLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final String text, @NotNull final Font textFont, @NotNull final Color textColor, @Nullable final BufferedImage backgroundPicture, @Nullable final Color backgroundColor)
Creates a new instance.
final Color backgroundColor
If set, the opaque background color.
void setBackgroundImage(@Nullable final ImageIcon backgroundImage)
Sets the background image.
Abstract base class for GUI elements to be shown in Guis.
static final long serialVersionUID
The serial version UID.