Image

Imagedelusory wrote in Imagejava_dev really, really sleepy

Applet: Olympic Rings

For a class assignment, I am to write an applet that displays the Olympic rings and allows a user to play around with the colors of said rings. From what I see, I've gotten everything right... except that the buttons aren't doing anything. (However, if you're using Firefox, you'll see that something is happening. Once the applet is loaded and you click on "Random," try minimizing the browser or clicking on a different tab and then going back to the applet. The rings have changed color.)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class OlympicRings extends Applet implements ActionListener {

     Random r = new Random( );
     Button rand = new Button( "Random" );
     Button reset = new Button( "Reset" );
     OlympicRingsCanvas canvas;
     Color ringColors[ ];

     public OlympicRings( ) {
          super( );
          ringColors = ( new Color[] {
               Color.BLUE, Color.BLACK, Color.RED, Color.YELLOW, Color.GREEN
          } );
     }

     public void init( ) {
          setBackground( Color.WHITE );
          canvas = new OlympicRingsCanvas( );
          Panel panel = new Panel( );

          rand.addActionListener( this );
          panel.add( rand );

          reset.addActionListener( this );
          panel.add( reset );

          setLayout( new BorderLayout( ) );
          add( panel, "South" );
          add( canvas, "Center" );
     }

     public void actionPerformed( ActionEvent e ) {
          if( e.getSource( ) == rand )
               ringColors = ( new Color[] {
                    new Color( r.nextInt( 256 ), r.nextInt( 256 ), r.nextInt( 256 ) ), new Color( r.nextInt( 256 ), r.nextInt( 256 ), r.nextInt( 256 ) ), new Color( r.nextInt( 256 ), r.nextInt( 256 ), r.nextInt( 256 ) ), new Color( r.nextInt( 256 ), r.nextInt( 256 ), r.nextInt( 256 ) ), new Color( r.nextInt( 256 ), r.nextInt( 256 ), r.nextInt( 256 ) )
               } );
          else if( e.getSource( ) == reset )
               ringColors = ( new Color[] {
                    Color.BLUE, Color.BLACK, Color.RED, Color.YELLOW, Color.GREEN
               } );

          repaint( );
     }

     class OlympicRingsCanvas extends Canvas {

          OlympicRingsCanvas( ) {
               super( );
               setBackground( Color.WHITE );
          }

          public void paint( Graphics g ) {
               setBackground( Color.WHITE );

               for( int i = 0; i < 3; i++ )
                    drawCircle( g, 10 + ( i * 110 ), 10, 100, ringColors[i] );

               for( int j = 0; j < 2; j++ )
                    drawCircle( g, 60 + ( j * 110 ), 60, 100, ringColors[j + 3] );

          }

          private void drawCircle( Graphics g, int i, int j, int k, Color color ) {
               g.setColor( color );
               for( int l = 9; l >= 0; l-- )
                    g.drawOval( i + l, j + l, k - 2 * l, k - 2 * l );
          }

     }

}

What am I doing wrong? I would appreciate any and all pointers. (Also. If you see something wrong or something that could be improved in my code, please point it out, too.)

Thanks a lot :)

--Allie

EDIT [12/13/2006]: Thanks for the responses, guys... but my professor told me that the problem was my call to repaint( ) in actionPerformed( ActionEvent e ). He said that my canvas should've been taking care of the repainting. He considers this to be a flaw in my design... but invoking the canvas's repaint( ) method by writing canvas.repaint( ) would [temporarily] do the trick. Thank you all for responding :D