Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Animation keeps flickering

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Animation keeps flickering

    Hi my application animation keeps flickering I would put my code in but I keep getting this


    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();
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Animation keeps flickering

    Extend JPanel, not JFrame.

    Override paintComponent(), not paint().

    What does that call to Toolkit.sync() do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Flickering Checkbox in jtable ?
    By harshilshah in forum AWT / Java Swing
    Replies: 12
    Last Post: April 26th, 2013, 06:03 AM
  2. my Button is flickering
    By poldz123 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 20th, 2013, 02:30 PM
  3. Screen is flippering / flickering.
    By Ulixava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 5th, 2012, 12:54 PM
  4. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  5. JPanel flickering
    By Ghosth in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2011, 10:52 AM