Crossfire JXClient, Trunk  R20561
GUIDialogBackground.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.misc;
23 
27 import java.awt.AlphaComposite;
28 import java.awt.Dimension;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.Image;
32 import java.awt.Transparency;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
41 
45  private static final long serialVersionUID = 1L;
46 
50  @NotNull
51  private final Image frameNW;
52 
56  @NotNull
57  private final Image frameN;
58 
62  @NotNull
63  private final Image frameNE;
64 
68  @NotNull
69  private final Image frameW;
70 
74  @NotNull
75  private final Image frameC;
76 
80  @NotNull
81  private final Image frameE;
82 
86  @NotNull
87  private final Image frameSW;
88 
92  @NotNull
93  private final Image frameS;
94 
98  @NotNull
99  private final Image frameSE;
100 
104  private final int heightN;
105 
109  private final int heightS;
110 
114  private final int widthW;
115 
119  private final int widthE;
120 
124  private final float alpha;
125 
143  public GUIDialogBackground(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final float alpha, @NotNull final Image frameNW, @NotNull final Image frameN, @NotNull final Image frameNE, @NotNull final Image frameW, @NotNull final Image frameC, @NotNull final Image frameE, @NotNull final Image frameSW, @NotNull final Image frameS, @NotNull final Image frameSE) {
144  super(tooltipManager, elementListener, name, alpha < 1.0F ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
145  this.frameNW = frameNW;
146  this.frameN = frameN;
147  this.frameNE = frameNE;
148  this.frameW = frameW;
149  this.frameC = frameC;
150  this.frameE = frameE;
151  this.frameSW = frameSW;
152  this.frameS = frameS;
153  this.frameSE = frameSE;
154  this.alpha = alpha;
155  heightN = frameN.getHeight(null);
156  heightS = frameS.getHeight(null);
157  widthW = frameW.getWidth(null);
158  widthE = frameE.getWidth(null);
159  if (frameNW.getWidth(null) != widthW) {
160  throw new IllegalArgumentException("frameNW.width="+frameNW.getWidth(null)+" != "+widthW+"=widthW");
161  }
162  if (frameSW.getWidth(null) != widthW) {
163  throw new IllegalArgumentException("frameSW.width="+frameSW.getWidth(null)+" != "+widthW+"=widthW");
164  }
165  if (frameNE.getWidth(null) != widthE) {
166  throw new IllegalArgumentException("frameNE.width="+frameNE.getWidth(null)+" != "+widthE+"=widthE");
167  }
168  if (frameSE.getWidth(null) != widthE) {
169  throw new IllegalArgumentException("frameSE.width="+frameSE.getWidth(null)+" != "+widthE+"=widthE");
170  }
171  if (frameNW.getHeight(null) != heightN) {
172  throw new IllegalArgumentException("frameNW.height="+frameNW.getHeight(null)+" != "+heightN+"heightN");
173  }
174  if (frameSW.getHeight(null) != heightS) {
175  throw new IllegalArgumentException("frameSW.height="+frameSW.getHeight(null)+" != "+heightS+"=heightS");
176  }
177  if (frameNE.getHeight(null) != heightN) {
178  throw new IllegalArgumentException("frameNE.height="+frameNE.getHeight(null)+" != "+heightN+"=heightN");
179  }
180  if (frameSE.getHeight(null) != heightS) {
181  throw new IllegalArgumentException("frameSE.height="+frameSE.getHeight(null)+" != "+heightS+"=heightS");
182  }
183  }
184 
188  @Override
189  public void paintComponent(@NotNull final Graphics g) {
190  final Graphics paint;
191  if (alpha < 1.0F) {
192  final Graphics2D g2d = (Graphics2D)g.create();
193  g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
194  paint = g2d;
195  } else {
196  paint = g;
197  }
198 
199  super.paintComponent(paint);
200  final int w = Math.max(1, getWidth()-widthW-widthE);
201  final int h = Math.max(1, getHeight()-heightN-heightS);
202  paint.drawImage(frameNW, 0, 0, null);
203  paint.drawImage(frameN, widthW, 0, widthW+w, heightN, 0, 0, w, heightN, null);
204  paint.drawImage(frameNE, widthW+w, 0, null);
205  paint.drawImage(frameW, 0, heightN, widthW, heightN+h, 0, 0, widthW, h, null);
206  paint.drawImage(frameC, widthW, heightN, widthW+w, heightN+h, 0, 0, w, h, null);
207  paint.drawImage(frameE, widthW+w, heightN, widthW+w+widthE, heightN+h, 0, 0, widthE, h, null);
208  paint.drawImage(frameSW, 0, heightN+h, null);
209  paint.drawImage(frameS, widthW, heightN+h, widthW+w, heightN+h+heightS, 0, 0, w, heightS, null);
210  paint.drawImage(frameSE, widthW+w, heightN+h, null);
211  if (paint != g) {
212  paint.dispose();
213  }
214  }
215 
219  @Nullable
220  @Override
221  public Dimension getPreferredSize() {
222  return getMinimumSize();
223  }
224 
228  @Nullable
229  @Override
230  public Dimension getMinimumSize() {
231  return new Dimension(widthW+1+widthE, heightN+1+heightS);
232  }
233 
234 }
final int widthW
The width of the west border in pixel.
final TooltipManager tooltipManager
The TooltipManager to update.
final int heightN
The width of the north border in pixel.
final GUIElementListener elementListener
The GUIElementListener to notify.
GUIDialogBackground(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final float alpha, @NotNull final Image frameNW, @NotNull final Image frameN, @NotNull final Image frameNE, @NotNull final Image frameW, @NotNull final Image frameC, @NotNull final Image frameE, @NotNull final Image frameSW, @NotNull final Image frameS, @NotNull final Image frameSE)
Creates a new instance.
static final long serialVersionUID
The serial version UID.
final float alpha
The alpha value for the background, 1 opaque 0 full transparent.
Abstract base class for GUI elements to be shown in Guis.
final int widthE
The width of the east border in pixel.
final int heightS
The width of the south border in pixel.