Crossfire JXClient, Trunk  R20561
ButtonImages.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 
24 import java.awt.Graphics;
25 import java.awt.Image;
26 import org.jetbrains.annotations.NotNull;
27 
34 public class ButtonImages {
35 
39  @NotNull
40  private final Image imageLeft;
41 
45  @NotNull
46  private final Image imageMiddle;
47 
51  @NotNull
52  private final Image imageRight;
53 
57  private final int height;
58 
65  public ButtonImages(@NotNull final Image imageLeft, @NotNull final Image imageMiddle, @NotNull final Image imageRight) {
66  if (imageLeft.getHeight(null) != imageMiddle.getHeight(null)) {
67  throw new IllegalArgumentException("left image height is "+imageLeft.getHeight(null)+" but middle image height is "+imageMiddle.getHeight(null));
68  }
69  if (imageMiddle.getHeight(null) != imageRight.getHeight(null)) {
70  throw new IllegalArgumentException("middle image height is "+imageMiddle.getHeight(null)+" but right image height is "+imageRight.getHeight(null));
71  }
72 
73  this.imageLeft = imageLeft;
74  this.imageMiddle = imageMiddle;
75  this.imageRight = imageRight;
76  height = imageMiddle.getHeight(null);
77  }
78 
83  public int getHeight() {
84  return height;
85  }
86 
92  public void render(@NotNull final Graphics g, final int w) {
93  g.drawImage(imageLeft, 0, 0, null);
94  g.drawImage(imageRight, w-imageRight.getWidth(null), 0, null);
95 
96  final int middleWidth = imageMiddle.getWidth(null);
97  int tmpWidth = w-imageLeft.getWidth(null)-imageRight.getWidth(null);
98  int tmpX = imageLeft.getWidth(null);
99  while (tmpWidth > 0) {
100  final int thisWidth = Math.min(tmpWidth, middleWidth);
101  g.drawImage(imageMiddle, tmpX, 0, tmpX+thisWidth, height, 0, 0, thisWidth, height, null);
102  tmpX += thisWidth;
103  tmpWidth -= thisWidth;
104  }
105  }
106 
107 }
int getHeight()
Returns the button's height.
void render(@NotNull final Graphics g, final int w)
Draws the button.
A set of images to form a button image.
final Image imageMiddle
The background of the middle part of the button.
final Image imageRight
The right border of the button.
final Image imageLeft
The left border of the button.
ButtonImages(@NotNull final Image imageLeft, @NotNull final Image imageMiddle, @NotNull final Image imageRight)
Creates a new instance.