Crossfire JXClient, Trunk  R20561
ItemPainter.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.item;
23 
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.Font;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.Image;
32 import java.awt.geom.RectangularShape;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
40 public class ItemPainter {
41 
45  private static final int TEXT_OFFSET = 3;
46 
50  @Nullable
51  private final Image cursedImage;
52 
56  @Nullable
57  private final Image damnedImage;
58 
62  @Nullable
63  private final Image magicImage;
64 
68  @Nullable
69  private final Image blessedImage;
70 
74  @Nullable
75  private final Image appliedImage;
76 
80  @Nullable
81  private final Image unidentifiedImage;
82 
86  @Nullable
87  private final Image selectorImage;
88 
92  @Nullable
93  private final Image lockedImage;
94 
98  @Nullable
99  private final Image unpaidImage;
100 
104  @Nullable
105  private final Color cursedColor;
106 
110  @Nullable
111  private final Color damnedColor;
112 
116  @Nullable
117  private final Color magicColor;
118 
122  @Nullable
123  private final Color blessedColor;
124 
128  @Nullable
129  private final Color appliedColor;
130 
134  @Nullable
135  private final Color unidentifiedColor;
136 
140  @Nullable
141  private final Color selectorColor;
142 
146  @Nullable
147  private final Color lockedColor;
148 
152  @Nullable
153  private final Color unpaidColor;
154 
158  @NotNull
159  private final Font font;
160 
164  @NotNull
165  private final Color nrofColor;
166 
190  public ItemPainter(@Nullable final Image cursedImage, @Nullable final Image damnedImage, @Nullable final Image magicImage, @Nullable final Image blessedImage, @Nullable final Image appliedImage, @Nullable final Image unidentifiedImage, @Nullable final Image selectorImage, @Nullable final Image lockedImage, @Nullable final Image unpaidImage, @Nullable final Color cursedColor, @Nullable final Color damnedColor, @Nullable final Color magicColor, @Nullable final Color blessedColor, @Nullable final Color appliedColor, @Nullable final Color unidentifiedColor, @Nullable final Color selectorColor, @Nullable final Color lockedColor, @Nullable final Color unpaidColor, @NotNull final Font font, @NotNull final Color nrofColor) {
191  this.cursedImage = cursedImage;
192  this.damnedImage = damnedImage;
193  this.magicImage = magicImage;
194  this.blessedImage = blessedImage;
195  this.appliedImage = appliedImage;
196  this.unidentifiedImage = unidentifiedImage;
197  this.selectorImage = selectorImage;
198  this.lockedImage = lockedImage;
199  this.unpaidImage = unpaidImage;
200  this.cursedColor = cursedColor;
201  this.damnedColor = damnedColor;
202  this.magicColor = magicColor;
203  this.blessedColor = blessedColor;
204  this.appliedColor = appliedColor;
205  this.unidentifiedColor = unidentifiedColor;
206  this.selectorColor = selectorColor;
207  this.lockedColor = lockedColor;
208  this.unpaidColor = unpaidColor;
209  this.font = font;
210  this.nrofColor = nrofColor;
211  }
212 
218  @NotNull
220  return new ItemPainter(cursedImage, damnedImage, magicImage, blessedImage, appliedImage, unidentifiedImage, selectorImage, lockedImage, unpaidImage, cursedColor, damnedColor, magicColor, blessedColor, appliedColor, unidentifiedColor, selectorColor, lockedColor, unpaidColor, font, nrofColor);
221  }
222 
227  @NotNull
228  public Dimension getMinimumSize() {
229  final Dimension dimension = new Dimension(32, 32);
230  updateMinimumSize(dimension, appliedImage);
231  updateMinimumSize(dimension, unidentifiedImage);
232  updateMinimumSize(dimension, cursedImage);
233  updateMinimumSize(dimension, magicImage);
234  updateMinimumSize(dimension, blessedImage);
235  updateMinimumSize(dimension, lockedImage);
236  updateMinimumSize(dimension, unpaidImage);
237  return dimension;
238  }
239 
245  private static void updateMinimumSize(@NotNull final Dimension minimumSize, @Nullable final Image image) {
246  if (image == null) {
247  return;
248  }
249 
250  final int width = image.getWidth(null);
251  if (minimumSize.width < width) {
252  minimumSize.width = width;
253  }
254 
255  final int height = image.getWidth(null);
256  if (minimumSize.height < height) {
257  minimumSize.height = height;
258  }
259  }
260 
270  public void paint(@NotNull final Graphics2D g, @NotNull final CfItem item, final boolean selected, @NotNull final Image face, final int w, final int h) {
271  paintColor(g, appliedColor, item.isApplied(), w, h);
272  paintColor(g, unidentifiedColor, item.isUnidentified(), w, h);
273  paintColor(g, cursedColor, item.isCursed(), w, h);
274  paintColor(g, damnedColor, item.isDamned(), w, h);
275  paintColor(g, magicColor, item.isMagic(), w, h);
276  paintColor(g, blessedColor, item.isBlessed(), w, h);
277  paintColor(g, lockedColor, item.isLocked(), w, h);
278  paintColor(g, selectorColor, selected, w, h);
279  paintColor(g, unpaidColor, item.isUnpaid(), w, h);
280  final int imageW = Math.max(0, face.getWidth(null));
281  final int imageH = Math.max(0, face.getHeight(null));
282  final int scaledW;
283  final int scaledH;
284  final int offsetX;
285  final int offsetY;
286  if (imageW > imageH) {
287  scaledW = h;
288  scaledH = MathUtils.divRound(imageH*h, imageW);
289  offsetX = 0;
290  offsetY = (h-scaledH)/2;
291  } else {
292  scaledW = MathUtils.divRound(imageW*h, imageH);
293  scaledH = h;
294  offsetX = (h-scaledW)/2;
295  offsetY = 0;
296  }
297  g.drawImage(face, offsetX, offsetY, scaledW, scaledH, null);
298  paintImage(g, appliedImage, item.isApplied());
299  paintImage(g, unidentifiedImage, item.isUnidentified());
300  paintImage(g, cursedImage, item.isCursed());
301  paintImage(g, damnedImage, item.isDamned());
302  paintImage(g, magicImage, item.isMagic());
303  paintImage(g, blessedImage, item.isBlessed());
304  paintImage(g, lockedImage, item.isLocked());
305  paintImage(g, selectorImage, selected);
306  paintImage(g, unpaidImage, item.isUnpaid());
307  if (w <= h) {
308  if (item.getNrOf() > 1) {
309  g.setFont(font);
310  g.setColor(nrofColor);
311  g.drawString(String.valueOf(item.getNrOf()), 1, 1+font.getSize());
312  }
313  } else {
314  g.setFont(font);
315  g.setColor(nrofColor);
316  g.setBackground(new Color(0, 0, 0, 0.0f));
317  renderText(g, TEXT_OFFSET+h, 0, h/2, item.getTooltipText1());
318  renderText(g, TEXT_OFFSET+h, h/2, h/2, item.getTooltipText2());
319  }
320  }
321 
330  private static void paintColor(@NotNull final Graphics g, @Nullable final Color color, final boolean isActive, final int w, final int h) {
331  if (isActive && color != null) {
332  g.setColor(color);
333  g.fillRect(0, 0, w, h);
334  }
335  }
336 
343  private static void paintImage(@NotNull final Graphics g, @Nullable final Image image, final boolean isActive) {
344  if (isActive) {
345  g.drawImage(image, 0, 0, null);
346  }
347  }
348 
357  private void renderText(@NotNull final Graphics2D g, final int dx, final int dy, final int height, @NotNull final String text) {
358  final RectangularShape rectangle = font.getStringBounds(text, g.getFontRenderContext());
359  final int y = dy+(int)Math.round(height-rectangle.getMaxY()-rectangle.getMinY())/2;
360  g.drawString(text, dx, y);
361  }
362 
363 }
void paint(@NotNull final Graphics2D g, @NotNull final CfItem item, final boolean selected, @NotNull final Image face, final int w, final int h)
Paints an CfItem.
final Image lockedImage
The overlay image for locked objects.
final Image appliedImage
The overlay image for applied objects.
final Color unidentifiedColor
The background color for unidentified objects.
final Color unpaidColor
The background color for unpaid objects.
final Color magicColor
The background color for magical objects.
final Color nrofColor
The color for the "nrof" text.
ItemPainter newItemPainter()
Creates a new instance having the same parameters as this instance except for the item&#39;s size...
static void updateMinimumSize(@NotNull final Dimension minimumSize, @Nullable final Image image)
Updates the minimum size to contain an image.
final Color appliedColor
The background color for applied objects.
static void paintImage(@NotNull final Graphics g, @Nullable final Image image, final boolean isActive)
Conditionally paints an image.
static void paintColor(@NotNull final Graphics g, @Nullable final Color color, final boolean isActive, final int w, final int h)
Conditionally paints the background with a solid color.
Utility class for mathematical functions.
Definition: MathUtils.java:28
final Font font
The font for the "nrof" text.
final Color blessedColor
The background color for blessed objects.
final Image magicImage
The overlay image for magical objects.
final Image selectorImage
The overlay image for selected objects.
final Image unidentifiedImage
The overlay image for unidentified objects.
final Color lockedColor
The background color for locked objects.
final Color damnedColor
The background color for damned objects.
static int divRound(final int numerator, final int denominator)
Returns the quotient of two values, rounded to the nearest integer.
Definition: MathUtils.java:79
final Image unpaidImage
The overlay image for unpaid objects.
Dimension getMinimumSize()
Returns the minimal size needed to display this item.
final Image damnedImage
The overlay image for damned objects.
final Image cursedImage
The overlay image for cursed objects.
final Image blessedImage
The overlay image for blessed objects.
void renderText(@NotNull final Graphics2D g, final int dx, final int dy, final int height, @NotNull final String text)
Renders a text string.
final Color cursedColor
The background color for cursed objects.
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
ItemPainter(@Nullable final Image cursedImage, @Nullable final Image damnedImage, @Nullable final Image magicImage, @Nullable final Image blessedImage, @Nullable final Image appliedImage, @Nullable final Image unidentifiedImage, @Nullable final Image selectorImage, @Nullable final Image lockedImage, @Nullable final Image unpaidImage, @Nullable final Color cursedColor, @Nullable final Color damnedColor, @Nullable final Color magicColor, @Nullable final Color blessedColor, @Nullable final Color appliedColor, @Nullable final Color unidentifiedColor, @Nullable final Color selectorColor, @Nullable final Color lockedColor, @Nullable final Color unpaidColor, @NotNull final Font font, @NotNull final Color nrofColor)
Creates a new instance.
static final int TEXT_OFFSET
The indentation of the item&#39;s text from the icon.
final Color selectorColor
The background color for selected objects.