我想制作一個裝滿六邊形 JButton 的桌子,最好將它放在背景影像上(例如文明游戲)。
有什么辦法嗎?我已經嘗試并搜索了許多解決方案,但沒有取得多大成功,任何幫助將不勝感激!
先感謝您!
uj5u.com熱心網友回復:
我總是對從類似的東西進行擴展持謹慎態度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();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/359101.html
上一篇:如何向游標位置旋轉影像?
下一篇:SWING:使用setBackgroundContainer呼叫方法會由于遞回呼叫而導致StackOverFlow錯誤
