Gridarta Editor
VerticalToggleButton.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.utils.borderpanel;
21 
22 import java.awt.Color;
23 import java.awt.Font;
24 import java.awt.FontMetrics;
25 import java.awt.Graphics2D;
26 import java.awt.RenderingHints;
27 import java.awt.image.BufferedImage;
28 import javax.swing.ImageIcon;
29 import javax.swing.JToggleButton;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class VerticalToggleButton extends JToggleButton {
37 
41  private static final long serialVersionUID = 1L;
42 
46  @NotNull
47  private static final Color TRANSPARENT = new Color(0, 0, 0, 0);
48 
55  public VerticalToggleButton(@NotNull final String title, final boolean right) {
56  final Font font = getFont();
57  final FontMetrics fontMetrics = getFontMetrics(font);
58  final int textWidth = Math.max(1, fontMetrics.getHeight());
59  final int textHeight = Math.max(1, fontMetrics.stringWidth(title));
60  final BufferedImage bufferedImage = new BufferedImage(textWidth, textHeight, BufferedImage.TYPE_INT_ARGB);
61  final Graphics2D g = bufferedImage.createGraphics();
62  try {
63  g.setColor(TRANSPARENT);
64  g.fillRect(0, 0, textWidth, textHeight);
65 
66  g.setColor(getForeground());
67  g.setFont(font);
68  g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
69 
70  if (right) {
71  g.rotate(Math.PI / 2.0);
72  g.translate(0, 1 - textWidth);
73  } else {
74  g.rotate(-Math.PI / 2.0);
75  g.translate(1 - textHeight, 0);
76  }
77  g.drawString(title, 0, fontMetrics.getAscent());
78  } finally {
79  g.dispose();
80  }
81 
82  setIcon(new ImageIcon(bufferedImage));
83  }
84 
85 }
net.sf.gridarta.gui.utils.borderpanel.VerticalToggleButton
A JToggleButton that displays vertical text.
Definition: VerticalToggleButton.java:36
net.sf.gridarta.gui.utils.borderpanel.VerticalToggleButton.VerticalToggleButton
VerticalToggleButton(@NotNull final String title, final boolean right)
Creates a new instance.
Definition: VerticalToggleButton.java:55
net.sf.gridarta.gui.utils.borderpanel.VerticalToggleButton.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: VerticalToggleButton.java:41
net.sf.gridarta.gui.utils.borderpanel.VerticalToggleButton.TRANSPARENT
static final Color TRANSPARENT
The transparent Color for filling the button's background.
Definition: VerticalToggleButton.java:47