www.pudn.com > gundongdexiaoqiu.zip > gundongdexiaoqiu.java


import javax.swing.*;  
import java.awt.*;  
import java.awt.geom.*;  
 
class RollingBall extends JPanel {  
    Ellipse2D.Float ball = new Ellipse2D.Float( -100, 100, 50, 50 );  
 
    public void paintComponent( Graphics g ) {  
        super.paintComponent( g );  
        Graphics2D g2 = ( Graphics2D ) g;  
 
        // Draw the ball  
        g2.fill( ball );  
        // Draw the rotating ellipse by skewing the Device Space  
        double angdeg =     // One rotation per ball's travelling over its perimeter  
            ball.x++ % ( Math.PI * ball.width ) / ( Math.PI * ball.width ) * 360;  
        g2.rotate( Math.toRadians( angdeg ), ball.getCenterX( ), ball.getCenterY( ) );  
        g2.scale( .5, 1 );  
        g2.translate( ball.getCenterX( ), 0 );  
        g2.setColor( Color.gray );  
        g2.fill( ball );  
    }  
 
    public void roll( ) throws Exception {  
        while( true ) {  
            repaint( );  
            Thread.sleep( 8 );  
        }  
    }  
 
    public static void main( String[ ] args ) throws Exception {  
        JFrame f = new JFrame( );  
        RollingBall rb = new RollingBall( );  
 
        f.setSize( 999, 185 );  
        f.getContentPane( ).add( rb );  
        f.setVisible( true );  
        rb.roll( );  
    }  
}