下面是Java Swing程序代码,实现了一个满足您要求的转盘。这个转盘每次点击都会旋转不同的角度,并在1秒后停下,随机显示一种食物。每次结果都是随机的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
public class SpinningFoodWheel extends JFrame {
private static final int RADIUS = 100;
private static final String[] COLORS = {"#FF6347", "#4682B4", "#32CD32", "#FFD700",
"#FF69B4", "#8A2BE2", "#DA70D6", "#008080"};
private static final String[] FOODS = {"炸鸡", "披萨", "寿司", "汉堡", "冰淇淋", "沙拉", "烤肉", "水果"};
private boolean isSpinning = false;
private float currentRotationDegrees = 0;
private Timer spinTimer;
public SpinningFoodWheel() {
setTitle("食品随机选择器");
setSize(360, 360);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 设置转盘面板
SpinningPanel spinningPanel = new SpinningPanel();
add(spinningPanel);
pack();
setVisible(true);
initializeSpinTimer();
}
// 初始化定时器用于控制旋转速度
private void initializeSpinTimer() {
spinTimer = new Timer(0, e -> spinWheel());
spinTimer.start();
}
private class SpinningPanel extends JPanel {
public SpinningPanel() {
setBackground(Color.WHITE);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!isSpinning) {
startSpinning();
}
}
};
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
}
// 开始旋转
private void startSpinning() {
isSpinning = true;
currentRotationDegrees = new Random().nextFloat() * 360; // 随机定位起始角度
}
// 转盘旋转逻辑
private void spinWheel() {
if (isSpinning) {
currentRotationDegrees += Math.max(3, (int)(Math.random() * 10));
repaint(); // 重绘界面以更新转盘画面
} else {
float spinAmount = 360 + new Random().nextInt(360);
currentRotationDegrees += spinAmount;
repaint();
JOptionPane.showMessageDialog(this, "你选择了: " + FOODS[(int) (currentRotationDegrees % 360 / 45) % FOODS.length],
"结果", JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
int diameter = Math.min(width, height);
int centerX = width / 2;
int centerY = height / 2;
int sectionSize = 360 / FOODS.length;
for (int i = 0; i < FOODS.length; i++) {
g2.setColor(Color.decode(COLORS[i % COLORS.length]));
// 扇形区域
int angleStart = sectionSize * i;
int angleSize = sectionSize;
g2.fillArc(centerX - RADIUS, centerY - RADIUS, diameter, diameter, angleStart,
angleSize);
// 文字
String foodName = FOODS[i];
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth(foodName);
int stringAscent = fm.getAscent();
int offset = RADIUS / 2 - (RADIUS / 8);
int textX = centerX - stringWidth / 2;
int textY = centerY + stringAscent / 2 - RADIUS / 2 + offset;
g2.setColor(Color.BLACK);
g2.drawString(foodName, textX, textY);
}
// 画外圈
g2.setColor(Color.BLACK);
g2.drawOval(centerX - RADIUS, centerY - RADIUS, diameter, diameter);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(SpinningFoodWheel::new);
}
}
这段代码构建了一个简单的用户界面,在该界面上有一个食物旋转转盘。转盘通过鼠标点击来启动,旋转一段时间后会随机停止。转盘的颜色和食物名字可以自由修改以满足需要。这个程序使用了JOptionPane
来显示选择结果,这样可以更清晰地看到用户选中的食物名称。