00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.button;
00023
00024 import com.realtime.crossfire.jxclient.gui.commandlist.CommandList;
00025 import com.realtime.crossfire.jxclient.gui.gui.GUIElementListener;
00026 import com.realtime.crossfire.jxclient.gui.gui.TooltipManager;
00027 import java.awt.Color;
00028 import java.awt.Dimension;
00029 import java.awt.Font;
00030 import java.awt.Graphics;
00031 import java.awt.Image;
00032 import java.awt.Transparency;
00033 import java.awt.image.BufferedImage;
00034 import org.jetbrains.annotations.NotNull;
00035 import org.jetbrains.annotations.Nullable;
00036
00044 public class GUIButton extends AbstractButton {
00045
00049 private static final long serialVersionUID = 1;
00050
00054 @NotNull
00055 private final Image imageUp;
00056
00060 @NotNull
00061 private final Image imageDown;
00062
00067 @Nullable
00068 private final String text;
00069
00074 @Nullable
00075 private final Font font;
00076
00080 private final int textX;
00081
00086 private final int textY;
00087
00092 @Nullable
00093 private final Color color;
00094
00098 @NotNull
00099 private final Dimension preferredSize;
00100
00122 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) {
00123 super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT, autoRepeat, commandList);
00124 final int preferredWidth = imageUp.getWidth();
00125 if (preferredWidth != imageDown.getWidth()) {
00126 throw new IllegalArgumentException();
00127 }
00128 final int preferredHeight = imageUp.getHeight();
00129 if (preferredHeight != imageDown.getHeight()) {
00130 throw new IllegalArgumentException();
00131 }
00132 this.imageUp = imageUp;
00133 this.imageDown = imageDown;
00134 this.text = text;
00135 this.font = font;
00136 this.color = color;
00137 this.textX = textX;
00138 this.textY = textY;
00139 preferredSize = new Dimension(preferredWidth, preferredHeight);
00140 }
00141
00145 @Override
00146 public void activeChanged() {
00147 setChanged();
00148 }
00149
00153 @Override
00154 public void paintComponent(@NotNull final Graphics g) {
00155 super.paintComponent(g);
00156 g.drawImage(isActive() ? imageDown : imageUp, 0, 0, null);
00157 if (text != null && font != null && color != null) {
00158 g.setFont(font);
00159 g.setColor(color);
00160 g.drawString(text, textX, textY);
00161 }
00162 }
00163
00167 @NotNull
00168 @Override
00169 protected Dimension getMinimumSizeInt() {
00170 return new Dimension(preferredSize);
00171 }
00172
00176 @NotNull
00177 @Override
00178 public Dimension getMaximumSize() {
00179 return getMinimumSizeInt();
00180 }
00181
00182 }