Crossfire JXClient, Trunk  R20561
GuiUtils.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.gui;
23 
24 import java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.Font;
27 import java.awt.FontMetrics;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class GuiUtils {
36 
40  private GuiUtils() {
41  }
42 
50  @NotNull
51  public static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics) {
52  final int width = fontMetrics.stringWidth(text);
53  final int height = fontMetrics.getMaxAscent()+fontMetrics.getMaxDescent();
54  return new Dimension(width, height);
55  }
56 
62  public static int getElementX(@NotNull final Component element) {
63  final Component gui = getGui(element);
64  int x = gui == null ? 0 : gui.getX();
65  for (Component component = element; component != null && !(component instanceof Gui); component = component.getParent()) {
66  x += component.getX();
67  }
68  return x;
69  }
70 
76  public static int getElementY(@NotNull final Component element) {
77  final Component gui = getGui(element);
78  int y = gui == null ? 0 : gui.getY();
79  for (Component component = element; component != null && !(component instanceof Gui); component = component.getParent()) {
80  y += component.getY();
81  }
82  return y;
83  }
84 
90  @Nullable
91  public static Gui getGui(@NotNull final Component element) {
92  for (Component component = element; component != null; component = component.getParent()) {
93  if (component instanceof Gui) {
94  return (Gui)component;
95  }
96  }
97  return null;
98  }
99 
100 }
static Gui getGui(@NotNull final Component element)
Returns the Gui an element is part of.
Definition: GuiUtils.java:91
static int getElementY(@NotNull final Component element)
Returns an element's absolute screen coordinate.
Definition: GuiUtils.java:76
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Returns the extents of a string when rendered in a given Font on this component.
Definition: GuiUtils.java:51
Utility class for Gui related functions.
Definition: GuiUtils.java:35
static int getElementX(@NotNull final Component element)
Returns an element's absolute screen coordinate.
Definition: GuiUtils.java:62
GuiUtils()
Private constructor to prevent instantiation.
Definition: GuiUtils.java:40