Crossfire JXClient, Trunk  R20561
GaugeState.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.gauge;
23 
24 import java.awt.Dimension;
25 import java.awt.Graphics;
26 import java.awt.Image;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
34 public class GaugeState {
35 
39  @Nullable
40  private final Image fullImage;
41 
45  @Nullable
46  private final Image negativeImage;
47 
51  @NotNull
52  private final Dimension preferredSize;
53 
57  private final int dx;
58 
62  private int dy;
63 
67  private int filledW;
68 
72  private int filledH;
73 
77  private int filledX;
78 
82  private int filledY;
83 
87  @Nullable
88  private Image filledPicture;
89 
98  public GaugeState(@Nullable final Image fullImage, @Nullable final Image negativeImage, final int dx, final int dy) {
99  this.fullImage = fullImage;
100  this.negativeImage = negativeImage;
101  final int preferredWidth = Math.max(Math.max(fullImage == null ? 1 : fullImage.getWidth(null), negativeImage == null ? 1 : negativeImage.getWidth(null)), 1);
102  final int preferredHeight = Math.max(Math.max(fullImage == null ? 1 : fullImage.getHeight(null), negativeImage == null ? 1 : negativeImage.getHeight(null)), 1);
103  preferredSize = new Dimension(preferredWidth, preferredHeight);
104  this.dx = dx;
105  this.dy = dy;
106  }
107 
112  public void setDy(final int dy) {
113  this.dy = dy;
114  }
115 
121  public boolean setValues(@NotNull final Orientation orientation) {
122  final int newFilledX = orientation.getX();
123  final int newFilledY = orientation.getY();
124  final int newFilledW = orientation.getW();
125  final int newFilledH = orientation.getH();
126  final Image newFilledPicture = orientation.isValid() ? orientation.isNegativeImage() ? negativeImage : fullImage : null;
127 
128  if (filledX == newFilledX && filledY == newFilledY && filledW == newFilledW && filledH == newFilledH && filledPicture == newFilledPicture) {
129  return false;
130  }
131 
132  filledX = newFilledX;
133  filledY = newFilledY;
134  filledW = newFilledW;
135  filledH = newFilledH;
136  filledPicture = newFilledPicture;
137  return true;
138  }
139 
144  public void draw(@NotNull final Graphics g) {
145  if (filledPicture != null) {
146  g.drawImage(filledPicture, filledX+dx, filledY+dy, filledX+dx+filledW, filledY+dy+filledH, filledX, filledY, filledX+filledW, filledY+filledH, null);
147  }
148  }
149 
154  @NotNull
155  public Dimension getPreferredSize() {
156  return new Dimension(preferredSize);
157  }
158 
159 }
final Dimension preferredSize
The preferred size of this component.
Definition: GaugeState.java:52
void draw(@NotNull final Graphics g)
Draws the gauge image into the given graphics context.
final int dx
The x-offset for drawing.
Definition: GaugeState.java:57
Dimension getPreferredSize()
Returns the preferred size.
final Image fullImage
The image representing a full gauge.
Definition: GaugeState.java:40
int filledY
The y-coordinate of the "filled" area.
Definition: GaugeState.java:82
int filledH
The height of the "filled" area.
Definition: GaugeState.java:72
void setDy(final int dy)
Sets the y-offset for drawing.
int filledX
The x-coordinate of the "filled" area.
Definition: GaugeState.java:77
GaugeState(@Nullable final Image fullImage, @Nullable final Image negativeImage, final int dx, final int dy)
Creates a new instance.
Definition: GaugeState.java:98
int filledW
The width of the "filled" area.
Definition: GaugeState.java:67
Image filledPicture
The image for painting the "filled" area.
Definition: GaugeState.java:88
boolean setValues(@NotNull final Orientation orientation)
Updates the values from a Orientation state.
final Image negativeImage
The image representing a more-than-empty gauge.
Definition: GaugeState.java:46