2 Replies Latest reply: Jun 3, 2013 11:43 PM by r035198x RSS

    Animation flickers

    PinHead
      Hi,
      I'm having trouble when my application runs my animation starts to flicker for a few seconds and then it runs smooth help would be appreciated
      package starLogoMain;
      
      import java.awt.Color;
      import java.awt.Graphics;
      import java.awt.Toolkit;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      
      import javax.imageio.ImageIO;
      import javax.swing.JFrame;
      import javax.swing.Timer;
      
      public class StarLogoMain extends JFrame implements ActionListener 
      {
      
           /**
            * 
            */
           private static final long serialVersionUID = 1L;
           
           private BufferedImage[] image;
           private Timer timer;
           
           private int counter;
      
           public StarLogoMain() 
           {
                // TODO Auto-generated constructor stub
                super();
                setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                getContentPane().setBackground( Color.BLACK );
                
                loadImage();
                
                timer = new Timer( 250, this );
                
                timer.start();
                setSize( 500, 500 );
                setVisible( true );
           }
           
           public void loadImage()
           {
                try
                {
                     image = new BufferedImage[ 6 ];               
                                    
                     for( int i = 0; i < image.length; i++ )
                     {
                          image[ i ] = ImageIO.read( new File( "C://Users//Genesis//Desktop//cv//Game Menu Designs//" +
                                    "StarLogo Menu//Star Startup Menu Design//AnimationReferences//StarLogo//starLogo1 " + (i + 1) + ".png" ) );
                          
                     }
                }
                catch( IOException io )
                {
                     System.out.println( "Cannot Find Images: " + io );
                }
           }
           
           public BufferedImage[]getLoadedImages()
           {
                return image;
           }
      
           @Override
           public void paint( Graphics g )
           {
                super.paint( g );
                if( getLoadedImages()[ counter ] != null )
                {
                     g.drawImage( getLoadedImages()[ counter ], this.getWidth() / 2 - 150, this.getHeight() / 2 - 100, null );
                     Toolkit.getDefaultToolkit().sync();
                }
           }
           
           @Override
           public void actionPerformed( ActionEvent e )
           {
                // TODO Auto-generated method stub
                
                if( counter < 5 )
                {
                     counter = counter + 1;
                }
                else
                {
                     counter = 0;
                }          
                
                repaint();
           }
      
           /**
            * @param args
            */
           public static void main(String[] args) 
           {
                // TODO Auto-generated method stub
                new StarLogoMain();
           }
      
      }
        • 1. Re: Animation flickers
          r035198x
          For starters, build your GUI on the EDT and do IO on a different thread. Here are some sections of the Java tutorial to get you started

          http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

          http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html

          http://docs.oracle.com/javase/tutorial/2d/images/index.html
          • 2. Re: Animation flickers
            EJP
            image[ i ] = ImageIO.read( new File( "C://Users//Genesis//Desktop//cv//Game Menu Designs//" +
                "StarLogo Menu//Star Startup Menu Design//AnimationReferences//StarLogo//starLogo1 " + (i + 1) + ".png" ) );
            Why all the //'s? / is sufficient.