sppining wheel Java applet program.. anyone .. Solution // WheelsApplet.java (c)
ID: 3540073 • Letter: S
Question
sppining wheel Java applet program.. anyone ..
Explanation / Answer
// WheelsApplet.java (c) 2005 Kari Laitinen // 2006-01-27 File created. // 2006-02-05 Latest modification. // This is a Java Swing applet that demonstrates Java 2D features. 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 rotation_increment = 6 ; // in degrees int current_rotation = 0 ; // in degrees BufferedImage wheel_image ; public Wheel( int given_radius ) { wheel_radius = given_radius ; // A BufferedImage object is used only by the draw() method, but // it is best to create the image only once as creating it // something like 25 times a second inside the draw() method // requires quite a lot of processing power and memory. wheel_image = new BufferedImage( wheel_radius * 2, wheel_radius * 2, BufferedImage.TYPE_INT_RGB ) ; } public void rotate() // This is to be called by an animation thread. { if ( rotation_increment >= 0 ) { // The wheel is rotating clockwise. if ( current_rotation -12 ) { rotation_increment = rotation_increment - 3 ; } } public void draw( Graphics2D graphics2D, Point wheel_upper_left_corner ) { // As a Wheel object is not supposed to be moved on the screen, // it does not remember its position, i.e., wheel_upper_left_corner // is not a field of this class. This value is given as a parameter // for this method. 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( 8 ) ) ; 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, 30, // arc between_spokes Arc2D.PIE ) ) ; image_graphics.setPaint( Color.yellow ) ; 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, 30, // arc between_spokes Arc2D.PIE ) ) ; right_spoke_angle += 30 ; } // 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 ) ; } } class WheelPanel extends JPanel implements Runnable { Thread thread_to_rotate_the_wheels ; boolean thread_must_be_active = false ; int wheel_radius ; 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( "GEAR UP" ) ; JButton button_to_gear_down = new JButton( "GEAR DOWN" ) ; button_to_gear_up.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { wheel_on_this_panel.increase_speed() ; } } ) ; button_to_gear_down.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { wheel_on_this_panel.decrease_speed() ; } } ) ; JPanel wheel_control_panel = new JPanel() ; wheel_control_panel.add( button_to_gear_down ) ; 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() { while ( thread_must_be_active == true ) { wheel_on_this_panel.rotate() ; repaint() ; 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.5 times the radius of the wheel. graphics2D.setStroke( new BasicStroke( 8 ) ) ; graphics2D.draw( new Arc2D.Double( wheel_upper_left_corner.x - wheel_radius * 0.5 + 4, wheel_upper_left_corner.y - wheel_radius * 0.5 + 4, wheel_radius * 2 * 1.5 - 8, wheel_radius * 2 * 1.5 - 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.5 + 4, wheel_upper_left_corner.y - wheel_radius * 0.5 + 4, wheel_radius * 2 * 1.5 - 8, wheel_radius * 2 * 1.5 - 8, 0, 180, Arc2D.OPEN ) ) ; } } 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 right_wheel_panel = new WheelPanel( 75 ) ; // wheel radius: 75 points content_pane_of_this_applet.add( left_wheel_panel ) ; content_pane_of_this_applet.add( right_wheel_panel ) ; } public void start() { left_wheel_panel.start_animation_thread() ; right_wheel_panel.start_animation_thread() ; } public void stop() { left_wheel_panel.stop_animation_thread() ; right_wheel_panel.stop_animation_thread() ; } } /***** 2006-02-05 I tried drawing the wheel directly with to Graphics2D. It seems, however, that the drawing operations consume more computing power when the BufferedImage object is not in use. The following code was used in my experiments: // graphics2D.translate( wheel_radius, wheel_radius ) ; int right_spoke_angle = 0 ; while ( right_spoke_angle < 360 ) { graphics2D.setPaint( Color.black ) ; graphics2D.setStroke( new BasicStroke( 8 ) ) ; graphics2D.draw( new Arc2D.Double( wheel_upper_left_corner.x + 4, wheel_upper_left_corner.y + 4, wheel_radius * 2 - 8, wheel_radius * 2 - 8, right_spoke_angle + current_rotation, 30, // arc between_spokes Arc2D.PIE ) ) ; graphics2D.setPaint( Color.yellow ) ; graphics2D.setStroke( new BasicStroke( 2 ) ) ; graphics2D.draw( new Arc2D.Double( wheel_upper_left_corner.x + 4, wheel_upper_left_corner.y + 4, wheel_radius * 2 - 8, wheel_radius * 2 - 8, right_spoke_angle + current_rotation, 30, // arc between_spokes Arc2D.PIE ) ) ; right_spoke_angle += 30 ; } // The following two lines draw a red hub for the wheel. graphics2D.setPaint( Color.red ) ; graphics2D.fill( new Ellipse2D.Double( wheel_upper_left_corner.x - 8, wheel_upper_left_corner.y - 8, 16, 16 ) ) ; ****/Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.