我想在 Java Swing 中用 JButtons 制作一个六角形 table
I want to make a hexagonal table with JButtons in Java Swing
我想制作一个 table 填充六边形 JButtons 并且最好将其放置在背景图像上(例如文明游戏)。
有什么办法吗?我已经尝试并搜索了许多解决方案,但收效甚微,如有任何帮助,我们将不胜感激!
提前致谢!
我总是对从 JButton
之类的东西进行扩展持谨慎态度,它本身是一个非常复杂的 UI 组件,其中有很多功能可以返回给你。
我可能会考虑简单地从 JPanel
开始并创建所需的功能,但无论哪种情况,都可以归结为基本相同的核心概念。
您需要能够确定最适合内容和边框的按钮大小,这可能需要进行一些调整才能正确,然后绘制所需的形状和内容,最后响应适当的事件。
以下是一个非常基本的示例,它基于 。请记住,该组件仍然只是矩形,以“蜂窝”结构布置按钮是一个复杂得多的问题,尤其是因为每个按钮的尺寸可能不同。这可能需要自定义布局管理器才能实现。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
HexagonButton btn = new HexagonButton("Hello");
setLayout(new GridBagLayout());
add(btn);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class HexagonButton extends JButton {
private HexagonPath hexagonPath;
public HexagonButton(String text) {
super(text);
applyDefaults();
}
public HexagonButton(Icon icon) {
super(icon);
applyDefaults();
}
public HexagonButton(String text, Icon icon) {
super(text, icon);
applyDefaults();
}
public HexagonButton(Action action) {
super(action);
applyDefaults();
}
@Override
public void invalidate() {
hexagonPath = null;
super.invalidate();
}
protected int getMaxDimension() {
Dimension size = super.getPreferredSize();
return Math.max(size.width, size.height);
}
@Override
public Dimension getPreferredSize() {
int maxDimension = getMaxDimension();
return new Dimension(maxDimension, maxDimension);
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
protected void applyDefaults() {
setBorderPainted(false);
setFocusPainted(false);
}
protected HexagonPath getHexagonPath() {
if (hexagonPath == null) {
hexagonPath = new HexagonPath(getMaxDimension() - 1);
}
return hexagonPath;
}
@Override
protected void paintBorder(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
HexagonPath path = getHexagonPath();
g2d.setColor(getForeground());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.draw(path);
g2d.dispose();
}
@Override
public Color getBackground() {
if (getModel().isArmed()) {
return Color.BLUE;
}
return super.getBackground();
}
@Override
public Color getForeground() {
if (getModel().isArmed()) {
return Color.WHITE;
}
return super.getForeground();
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setColor(getBackground());
g2d.fill(getHexagonPath());
super.paintComponent(g2d);
g2d.dispose();
}
protected class HexagonPath extends Path2D.Double {
public HexagonPath(double size) {
double centerX = size / 2d;
double centerY = size / 2d;
for (double i = 0; i < 6; i++) {
double angleDegrees = (60d * i) - 30d;
double angleRad = ((float) Math.PI / 180.0f) * angleDegrees;
double x = centerX + ((size / 2f) * (double) Math.cos(angleRad));
double y = centerY + ((size / 2f) * (double) Math.sin(angleRad));
if (i == 0) {
moveTo(x, y);
} else {
lineTo(x, y);
}
}
closePath();
}
}
}
}
我想制作一个 table 填充六边形 JButtons 并且最好将其放置在背景图像上(例如文明游戏)。
有什么办法吗?我已经尝试并搜索了许多解决方案,但收效甚微,如有任何帮助,我们将不胜感激!
提前致谢!
我总是对从 JButton
之类的东西进行扩展持谨慎态度,它本身是一个非常复杂的 UI 组件,其中有很多功能可以返回给你。
我可能会考虑简单地从 JPanel
开始并创建所需的功能,但无论哪种情况,都可以归结为基本相同的核心概念。
您需要能够确定最适合内容和边框的按钮大小,这可能需要进行一些调整才能正确,然后绘制所需的形状和内容,最后响应适当的事件。
以下是一个非常基本的示例,它基于
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
HexagonButton btn = new HexagonButton("Hello");
setLayout(new GridBagLayout());
add(btn);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class HexagonButton extends JButton {
private HexagonPath hexagonPath;
public HexagonButton(String text) {
super(text);
applyDefaults();
}
public HexagonButton(Icon icon) {
super(icon);
applyDefaults();
}
public HexagonButton(String text, Icon icon) {
super(text, icon);
applyDefaults();
}
public HexagonButton(Action action) {
super(action);
applyDefaults();
}
@Override
public void invalidate() {
hexagonPath = null;
super.invalidate();
}
protected int getMaxDimension() {
Dimension size = super.getPreferredSize();
return Math.max(size.width, size.height);
}
@Override
public Dimension getPreferredSize() {
int maxDimension = getMaxDimension();
return new Dimension(maxDimension, maxDimension);
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
protected void applyDefaults() {
setBorderPainted(false);
setFocusPainted(false);
}
protected HexagonPath getHexagonPath() {
if (hexagonPath == null) {
hexagonPath = new HexagonPath(getMaxDimension() - 1);
}
return hexagonPath;
}
@Override
protected void paintBorder(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
HexagonPath path = getHexagonPath();
g2d.setColor(getForeground());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.draw(path);
g2d.dispose();
}
@Override
public Color getBackground() {
if (getModel().isArmed()) {
return Color.BLUE;
}
return super.getBackground();
}
@Override
public Color getForeground() {
if (getModel().isArmed()) {
return Color.WHITE;
}
return super.getForeground();
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setColor(getBackground());
g2d.fill(getHexagonPath());
super.paintComponent(g2d);
g2d.dispose();
}
protected class HexagonPath extends Path2D.Double {
public HexagonPath(double size) {
double centerX = size / 2d;
double centerY = size / 2d;
for (double i = 0; i < 6; i++) {
double angleDegrees = (60d * i) - 30d;
double angleRad = ((float) Math.PI / 180.0f) * angleDegrees;
double x = centerX + ((size / 2f) * (double) Math.cos(angleRad));
double y = centerY + ((size / 2f) * (double) Math.sin(angleRad));
if (i == 0) {
moveTo(x, y);
} else {
lineTo(x, y);
}
}
closePath();
}
}
}
}