Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, Could you make a graphical spinning wheel? The wheel will be broken int

ID: 3824187 • Letter: I

Question

In Java,

Could you make a graphical spinning wheel? The wheel will be broken into 6 equal parts labeled 1 through 6. A user will click the wheel or a button and the wheel will spin and land on a random number. (similar to the wheel in the board game called "game of life" )

Here is an example of what the wheel may look like (it doesn't need to look this good):

This has to be a graphical wheel that a user can see not just a random number output.

Thank you for the help!

shut www.shutterstock.com 273877922

Explanation / Answer

import java.io.File; import java.util.Random; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.Timer; import javax.swing.event.EventListenerList; //A random number generator in the form of a graphical spinning wheel. public class SpinningWheel extends Canvas { private Image wheel; private Image ball; private int number = 0; private int duration = 8000; private int refresh; private int elapsed; private int radius; private int wheel_inner_radius = 73; private int wheel_outer_radius = 123; private int wheel_center_x = 145; private int wheel_center_y = 145; private int ball_position = 0; private int[] positions = {3,15,34,22,5,17,32,20,7,11,30,26,9,28,37,2,14,35,23,4,16,33,21,6,18,31,19,8,12,29,25,10,27,38,1,13,36,24}; private int angle_of_first_position = 0; private Random random = new Random(); private AnimationThread thread; private EventListenerList listeners = new EventListenerList(); private boolean isSpinning = true; /** * Constructs the SpinningWheel. */ public SpinningWheel() { // load the image try { MediaTracker media = new MediaTracker(this); wheel = Toolkit.getDefaultToolkit().getImage(" "); /*specify the path where image is stored in your system*/ media.addImage(wheel, 0); ball = Toolkit.getDefaultToolkit().getImage(" "); /*specify the path where image is stored in your system*/ media.addImage(ball, 0); media.waitForID(0); } catch (Exception e) { e.printStackTrace(); } setSize(wheel.getWidth(null), wheel.getHeight(null)); this.ball_position = this.random.nextInt(this.positions.length); } /*Spins the wheel*/ public void spin() { /* * launch a new thread */ this.thread = new AnimationThread(); /* * select a random starting position */ this.ball_position = this.random.nextInt(this.positions.length); //place the ball on the outer part of the wheel this.radius = wheel_outer_radius; // start the animation thread this.refresh = 20; this.elapsed = 0; this.thread.animate(this, refresh); this.thread.start(); } /* Tests whether the wheel is spinning. @return true if the wheel is spinning (the ball is in play) */ public boolean isSpinning() { return this.isSpinning; } /* Gets the number the ball landed on. @return the number */ public String getNumber() { if (this.number (this.positions.length - 1)) this.ball_position = 0; } /* * Calculate the new ball position relative to the image. * The image is mostly circular so a little trig is required. */ double angle = (double) ((this.angle_of_first_position) - (360 * this.ball_position / this.positions.length)); /* * As it turns out, the image isn't perfectly round. */ if (this.ball_position > 12) angle += 2; if (this.ball_position == 37) angle -= 2; angle = Math.toRadians(angle); x = ((double) this.radius * Math.sin(angle)) + this.wheel_center_x; y = this.wheel_center_y - ((double) this.radius * Math.cos(angle)); /* * Do some poor man's physics to make it seem real. */ if (this.isSpinning == true) { this.elapsed += this.refresh; /* * add a little friction to slow the ball */ if (this.elapsed > this.duration * .2) this.refresh += 2; /* * At some point, the ball slips to the inside */ if (this.elapsed > this.duration * .85) { this.radius = this.wheel_inner_radius + this.random.nextInt(20) -10; } if (this.elapsed > this.duration) { this.isSpinning = false; this.refresh += 3; this.radius = this.wheel_inner_radius; this.number = this.positions[this.ball_position]; /** stop the animation thread */ thread.finished(); /* * send an action event to the caller */ ActionEvent event = new ActionEvent(this, 0, "spinning"); sendEvents(event); } else { thread.animate(this, this.refresh); } } /* * draw the wheel */ if (wheel != null) { g.drawImage(wheel, 0, 0, this); } /* * draw the ball */ if (ball != null) { g.drawImage(ball, (int) x, (int) y, this); } return; } /** * Adds an ActionListener. * @parameter listener the listener to add */ public void addActionListener(ActionListener listener) { this.listeners.add(ActionListener.class, listener); } /** * Removes an ActionListener. * @param listener the listener to remove */ public void removeActionListener(ActionListener listener) { this.listeners.remove(ActionListener.class, listener); } /* * sends an event to all the registered listeners */ private void sendEvents(ActionEvent event) { Object[] listeners = this.listeners.getListenerList(); for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote