In Java, Could you make a graphical spinning wheel? The wheel will be broken int
ID: 3824189 • 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):
shut www.shutterstock.com 273877922
This has to be a graphical wheel that a user can see not just a random number output.
Thank you for the help!
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):
shut www.shutterstock.com 273877922
This has to be a graphical wheel that a user can see not just a random number output.
Thank you for the help!
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):
shut www.shutterstock.com 273877922
This has to be a graphical wheel that a user can see not just a random number output.
Thank you for the help!
Explanation / Answer
Wheel.java
package com.cts.Test;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ; // Classes GeneralPath etc.
import java.awt.image.* ;
import javax.swing.* ;
class Wheel
{
int wheel_radius ;
int i1=60,j1=40,i2=0,j2=80,i3=-60,j3=40,i4=-60,j4=-40,i5=0,j5=-80,i6=60,j6=-40;
static int i =0;
int rotation_increment = 6 ; // in degrees
int current_rotation = 0 ; // in degrees
BufferedImage wheel_image ;
public Wheel( int given_radius )
{
wheel_radius = given_radius ;
wheel_image = new BufferedImage( wheel_radius * 2,
wheel_radius * 2,
BufferedImage.TYPE_INT_RGB ) ;
}
public int rotate() // This is to be called by an animation thread.
{
if ( rotation_increment >= 0 )
{
// The wheel is rotating clockwise.
if ( current_rotation <= 0 )
{
current_rotation = 360 - rotation_increment ;
}
else
{
current_rotation = current_rotation - rotation_increment ;
}
}
else
{
// It is rotating counter clockwise.
if ( current_rotation - rotation_increment < 360 )
{
current_rotation = current_rotation - rotation_increment ;
}
else
{
current_rotation = 0 ;
}
}
i++;
return i;
}
public void draw( Graphics2D graphics2D,
Point wheel_upper_left_corner )
{
i1=i2;j1=j2;
i2=i3;j2=j3;
i3=i4;j3=j4;
i4=i5;j4=j5;
i5=i6;j5=j6;
i6=i1;j6=j1;
Graphics2D image_graphics = wheel_image.createGraphics() ;
image_graphics.setPaint( Color.white ) ;
image_graphics.fill( new Rectangle2D.Double( 0, 0, wheel_radius * 2,
wheel_radius * 2 ));
image_graphics.translate( wheel_radius, wheel_radius ) ;
int right_spoke_angle = 0 ;
while ( right_spoke_angle < 360 )
{
image_graphics.setPaint( Color.black ) ;
image_graphics.setStroke( new BasicStroke( 2 ) ) ;
image_graphics.draw( new Arc2D.Double( -wheel_radius + 4,
-wheel_radius + 4,
wheel_radius * 2 - 8,
wheel_radius * 2 - 8,
right_spoke_angle
+ current_rotation,
60, // arc between_spokes
Arc2D.PIE ) ) ;
right_spoke_angle += 60 ;
}
i1=(i6)-(i5); j1= (j6)-(j5);
i2=(i1)-(i6); j2= (j1)-(j6);
i3=(i2)-(i1); j3= (j2)-(j1);
i4=(i3)-(i2); j4= (j3)-(j2);
i5=(i4)-(i3); j5= (j4)-(j3);
i6=(i5)-(i4); j6= (j5)-(j4);
image_graphics.drawString("1", i1, j1);
image_graphics.drawString("2", i2, j2);
image_graphics.drawString("3", i3, j3);
image_graphics.drawString("4", i4, j4);
image_graphics.drawString("5", i5, j5);
image_graphics.drawString("6", i6, j6);
// image_graphics.fill( new Ellipse2D.Double( -8, -8, 16, 16 ) ) ;
// The following two lines draw a red hub for the wheel.
image_graphics.setPaint( Color.red ) ;
image_graphics.fill( new Ellipse2D.Double( -8, -8, 16, 16 ) ) ;
// The picture of the wheel is printed onto the panel.
graphics2D.drawImage( wheel_image,
wheel_upper_left_corner.x,
wheel_upper_left_corner.y, null ) ;
graphics2D.setPaint( Color.GREEN ) ;
graphics2D.fill( new Ellipse2D.Double(
wheel_upper_left_corner.x + 16,
wheel_upper_left_corner.y + 16, 20, 20 ) ) ;
}
}
WheelPanel.java
package com.cts.Test;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ; // Classes GeneralPath etc.
import java.awt.image.* ;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.* ;
class WheelPanel extends JPanel implements Runnable
{
Thread thread_to_rotate_the_wheels ;
boolean thread_must_be_active = false ;
int wheel_radius ;
//int i=0;
Wheel wheel_on_this_panel ;
public WheelPanel( int given_wheel_radius )
{
wheel_radius = given_wheel_radius ;
wheel_on_this_panel = new Wheel( wheel_radius ) ;
setLayout( new BorderLayout() ) ;
JButton button_to_gear_up = new JButton( "Start" ) ;
button_to_gear_up.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
Wheel.i=0;
start_animation_thread();
}
} ) ;
JPanel wheel_control_panel = new JPanel() ;
wheel_control_panel.add( button_to_gear_up ) ;
add( "South", wheel_control_panel ) ;
}
public void start_animation_thread()
{
if ( thread_to_rotate_the_wheels == null )
{
thread_to_rotate_the_wheels = new Thread( this ) ;
thread_must_be_active = true ;
thread_to_rotate_the_wheels.start() ;
}
}
public void stop_animation_thread()
{
if ( thread_to_rotate_the_wheels != null )
{
thread_must_be_active = false ;
thread_to_rotate_the_wheels = null ;
}
}
public void run()
{
int randomNum = (1 + (int)(Math.random() * 12))*30;
//System.out.println("Random"+randomNum);
while ( thread_must_be_active == true )
{
int counter=wheel_on_this_panel.rotate() ;
repaint() ;
//System.out.println(counter);
if(counter>=randomNum)
{
thread_must_be_active = false ;
thread_to_rotate_the_wheels = null ;
}
try
{
Thread.sleep( 40 ) ; // 25 times per second
}
catch ( InterruptedException caught_exception )
{
// No actions to handle the exception.
}
}
}
public void paintComponent( Graphics graphics )
{
super.paintComponent( graphics ) ;
Graphics2D graphics2D = (Graphics2D) graphics ;
setBackground( Color.white ) ;
// The 'earth level' for the wheels is 75 points from the
// southern border of the panel.
Point wheel_upper_left_corner = new Point(
getWidth() / 2 - wheel_radius,
getHeight() - wheel_radius * 2 - 75 ) ;
wheel_on_this_panel.draw( graphics2D, wheel_upper_left_corner ) ;
// Now we'll print a "mudguard" for the wheel.
// The mudguard is an open arc whose distance from the wheel
// is 0.2 times the radius of the wheel.
graphics2D.setStroke( new BasicStroke( 8 ) ) ;
graphics2D.draw( new Arc2D.Double(
wheel_upper_left_corner.x - wheel_radius * 0.2 + 4,
wheel_upper_left_corner.y - wheel_radius * 0.2 + 4,
wheel_radius * 2 * 1.2 - 8,
wheel_radius * 2 * 1.2 - 8,
0, 180, Arc2D.OPEN ) ) ;
graphics2D.setPaint( Color.red ) ;
graphics2D.setStroke( new BasicStroke( 2 ) ) ;
graphics2D.draw( new Arc2D.Double(
wheel_upper_left_corner.x - wheel_radius * 0.2 + 4,
wheel_upper_left_corner.y - wheel_radius * 0.2 + 4,
wheel_radius * 2 * 1.2 - 8,
wheel_radius * 2 * 1.2 - 8,
0, 180, Arc2D.OPEN ) ) ;
}
}
WheelsApplet.java
package com.cts.Test;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.* ;
import java.awt.geom.* ; // Classes GeneralPath etc.
import java.awt.image.* ;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.* ;
public class WheelsApplet extends JApplet
{
WheelPanel left_wheel_panel, right_wheel_panel ;
public void init()
{
Container content_pane_of_this_applet = getContentPane() ;
content_pane_of_this_applet.setLayout( new GridLayout( 1, 2 ) ) ;
left_wheel_panel = new WheelPanel( 125 ) ; // wheel radius: 125 points
content_pane_of_this_applet.add( left_wheel_panel ) ;
}
public void start()
{
left_wheel_panel.stop_animation_thread() ;
}
public void stop()
{
left_wheel_panel.stop_animation_thread() ;
}
}
Note: Run the WheelApplet.java to get the output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.