22 package com.realtime.crossfire.jxclient.gui.button;
28 import java.awt.Color;
29 import java.awt.Dimension;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.Transparency;
35 import java.awt.font.FontRenderContext;
36 import java.awt.geom.RectangularShape;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
117 public GUITextButton(@NotNull
final TooltipManager tooltipManager, @NotNull
final GUIElementListener elementListener, @NotNull
final String
name, @NotNull
final ButtonImages up, @NotNull
final ButtonImages down, @NotNull
final String text, @NotNull
final Font font, @NotNull
final Color color, @NotNull
final Color colorSelected,
final boolean autoRepeat, @NotNull
final CommandList commandList) {
118 super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT, autoRepeat, commandList);
120 final int preferredHeight = up.getHeight();
121 if (preferredHeight != down.getHeight()) {
122 throw new IllegalArgumentException(
"'up' state height is "+preferredHeight+
" but 'down' state height is "+down.getHeight());
130 if (preferredSize.height < preferredHeight) {
131 preferredSize.height = preferredHeight;
133 preferredSize.width += 12;
149 super.paintComponent(g);
150 final Graphics2D g2 = (Graphics2D)g;
152 g2.setColor(selected ? colorSelected : color);
153 final int width = getWidth();
155 final FontRenderContext fontRenderContext = g2.getFontRenderContext();
156 final RectangularShape rectangle = font.getStringBounds(text, fontRenderContext);
157 final int x = (int)Math.round((width-rectangle.getWidth())/2);
158 final FontMetrics fontMetrics = g2.getFontMetrics();
159 final int y = (int)Math.round(preferredSize.height-rectangle.getHeight())/2+fontMetrics.getAscent();
160 g2.drawString(text, x, y);
169 return new Dimension(preferredSize);
178 return new Dimension(Integer.MAX_VALUE, preferredSize.height);
185 public void select(
final boolean selected) {
Listener for GUIElement related events.
Dimension getMinimumSizeInt()
final TooltipManager tooltipManager
The TooltipManager to update.
final GUIElementListener elementListener
The GUIElementListener to notify.
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Returns the extents of a string when rendered in a given Font on this component.
void setChanged()
Records that the contents have changed and must be repainted.
final String text
The button text.
final Dimension preferredSize
The preferred size of this component.
final Color color
The text color.
final String name
The name of this element.
final ButtonImages up
The images comprising the "up" button state.
final Color colorSelected
The text color when selected.
boolean selected
Whether the element is currently selected.
A GUIElement that implements a button.
Utility class for Gui related functions.
GUITextButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ButtonImages up, @NotNull final ButtonImages down, @NotNull final String text, @NotNull final Font font, @NotNull final Color color, @NotNull final Color colorSelected, final boolean autoRepeat, @NotNull final CommandList commandList)
Creates a new instance.
Dimension getMaximumSize()
void select(final boolean selected)
Selects or deselects the element.whether the element should be selected
void paintComponent(@NotNull final Graphics g)
A list of GUICommand instances.
final ButtonImages down
The images comprising the "down" button state.
static final long serialVersionUID
The serial version UID.
boolean isActive()
Returns whether a GUI element is active.
final Font font
The font to use.