📄 SpinWheel.java .java dosyası
⬇️ İndir
// If you have a different package name
// Change package name...
// If you have no package name
// Delete the line:
// package spin;
package spin;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SpinWheel extends JPanel implements ActionListener {
private double angle = 0;
private Timer timer;
private int speed = 20;
private String[] prizes = {
"1 $", "12 $", "100 $", "15 $",
"20 $", "25 $", "30 $", "500 $",
"35 $", "40 $", "200 $", "50 $"
};
public SpinWheel() {
JButton spinButton = new JButton("SPIN");
spinButton.addActionListener(e -> startSpin());
this.add(spinButton);
timer = new Timer(30, this);
}
private void startSpin() {
speed = 30 + (int)(Math.random() * 20); // rastgele hız
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
angle += speed;
speed = Math.max(0, speed - 1); // yavaşlama efekti
if (speed == 0) timer.stop();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int w = getWidth();
int h = getHeight();
int size = Math.min(w, h) - 50;
int x = (w - size) / 2;
int y = (h - size) / 2;
double sliceAngle = 360.0 / prizes.length;
double startAngle = angle + sliceAngle / 2; // yarım dilim kaydırma ✅
for (int i = 0; i < prizes.length; i++) {
g2.setColor(Color.getHSBColor((float)i / prizes.length, 0.8f, 0.9f));
g2.fillArc(x, y, size, size, (int)startAngle, (int)sliceAngle);
g2.setColor(Color.BLACK);
g2.drawArc(x, y, size, size, (int)startAngle, (int)sliceAngle);
// yazı
double theta = Math.toRadians(startAngle + sliceAngle / 2);
int tx = (int)(w/2 + Math.cos(theta) * size/3);
int ty = (int)(h/2 - Math.sin(theta) * size/3);
g2.drawString(prizes[i], tx - 20, ty);
startAngle += sliceAngle;
}
// Ok işareti (sabit yukarıda ortalanmış şekilde)
g2.setColor(Color.BLACK);
int[] px = {w/2 - 10, w/2 + 10, w/2};
int[] py = {y-5, y-5, y+20};
g2.fillPolygon(px, py, 3);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Java Blackboard 161 | Spin Wheel");
frame.setSize(800, 450);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SpinWheel());
frame.setVisible(true);
}
}