Crossfire JXClient, Trunk  R20561
GUIButton.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.button;
23 
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.Font;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.Transparency;
33 import java.awt.image.BufferedImage;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
44 public class GUIButton extends AbstractButton {
45 
49  private static final long serialVersionUID = 1;
50 
54  @NotNull
55  private final Image imageUp;
56 
60  @NotNull
61  private final Image imageDown;
62 
67  @Nullable
68  private final String text;
69 
74  @Nullable
75  private final Font font;
76 
80  private final int textX;
81 
86  private final int textY;
87 
92  @Nullable
93  private final Color color;
94 
98  @NotNull
99  private final Dimension preferredSize;
100 
121  public GUIButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final BufferedImage imageUp, @NotNull final BufferedImage imageDown, @Nullable final String text, @Nullable final Font font, @Nullable final Color color, final int textX, final int textY, final boolean autoRepeat, @NotNull final CommandList commandList) {
122  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT, autoRepeat, commandList);
123  final int preferredWidth = imageUp.getWidth();
124  if (preferredWidth != imageDown.getWidth()) {
125  throw new IllegalArgumentException("preferredWidth="+preferredWidth+", imageWidth="+imageDown.getWidth());
126  }
127  final int preferredHeight = imageUp.getHeight();
128  if (preferredHeight != imageDown.getHeight()) {
129  throw new IllegalArgumentException("preferredHeight="+preferredHeight+", imageHeight="+imageDown.getHeight());
130  }
131  this.imageUp = imageUp;
132  this.imageDown = imageDown;
133  this.text = text;
134  this.font = font;
135  this.color = color;
136  this.textX = textX;
137  this.textY = textY;
138  preferredSize = new Dimension(preferredWidth, preferredHeight);
139  }
140 
144  @Override
145  public void activeChanged() {
146  setChanged();
147  }
148 
152  @Override
153  public void paintComponent(@NotNull final Graphics g) {
154  super.paintComponent(g);
155  g.drawImage(isActive() ? imageDown : imageUp, 0, 0, null);
156  if (text != null && font != null && color != null) {
157  g.setFont(font);
158  g.setColor(color);
159  g.drawString(text, textX, textY);
160  }
161  }
162 
166  @NotNull
167  @Override
168  protected Dimension getMinimumSizeInt() {
169  return new Dimension(preferredSize);
170  }
171 
175  @NotNull
176  @Override
177  public Dimension getMaximumSize() {
178  return getMinimumSizeInt();
179  }
180 
181 }
final Color color
The Color of the overlay text or.
Definition: GUIButton.java:93
final TooltipManager tooltipManager
The TooltipManager to update.
final Font font
The Font for the overlay text or.
Definition: GUIButton.java:75
final GUIElementListener elementListener
The GUIElementListener to notify.
void setChanged()
Records that the contents have changed and must be repainted.
GUIButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final BufferedImage imageUp, @NotNull final BufferedImage imageDown, @Nullable final String text, @Nullable final Font font, @Nullable final Color color, final int textX, final int textY, final boolean autoRepeat, @NotNull final CommandList commandList)
Creates a new instance.
Definition: GUIButton.java:121
final int textY
The y coordinate of the overlay text.
Definition: GUIButton.java:86
static final long serialVersionUID
The serial version UID.
Definition: GUIButton.java:49
final String text
The overlay text or.
Definition: GUIButton.java:68
final CommandList commandList
The commands to execute when the button is elected.
void paintComponent(@NotNull final Graphics g)
Definition: GUIButton.java:153
final Image imageUp
The image in unselected state.
Definition: GUIButton.java:55
final Image imageDown
The image in selected state.
Definition: GUIButton.java:61
final Dimension preferredSize
The preferred size of this component.
Definition: GUIButton.java:99
final boolean autoRepeat
Whether this button should autorepeat.
final int textX
The x coordinate of the overlay text.
Definition: GUIButton.java:80
boolean isActive()
Returns whether a GUI element is active.
A GUIElement that implements a button.
Definition: GUIButton.java:44