Results 1 to 3 of 3
  1. #1
    ladygagads is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default slowing the animation of LogoAnimator.java

    hi, how can i slow the animation of LogoAnimator.java from the book. i already set the animationDelay to 1000000 but it wont work. any advice or hint about my situation? here is the source code:

    Java Code:
    import java.awt.*;
    import java.awt.Event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class LogoAnimator extends JPanel implements ActionListener{
    			protected ImageIcon images[];
    			protected int totalImages = 2, currentImage = 0, animationDelay = 1000000, width, height;
    			protected String imageName = "pic";
    			protected Timer animationTimer;
    			
    	public LogoAnimator()
    	{
    		initializeAnimation();
    	}
    	protected void initializeAnimation()
    	{
    		images = new ImageIcon[totalImages];
    	
    		for(int count = 0; count < images.length; ++count)
    			images[count] = new ImageIcon("/home/"+imageName+count+".gif");
    		width = images[0].getIconWidth();
    		height = images[0].getIconHeight();
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		
    		images[currentImage].paintIcon(this, g, 0, 0);
    		currentImage = (currentImage + 1) % totalImages;
    	}
    	
    	public void actionPerformed(ActionEvent actionEvent)
    	{
    		repaint();
    	}
    	public void startAnimation()
    	{
    		if(animationTimer == null)
    		{
    			currentImage = 0;
    			animationTimer = new Timer(animationDelay, this);
    			animationTimer.start();
    		}
    		else
    			if(!animationTimer.isRunning())
    			{
    				animationTimer.restart();
    			}
    	}
    	public void stopAnimation()
    	{
    		animationTimer.stop();
    	}
    	public Dimension getMinimumSize()
    	{
    		return getPreferredSize();
    	}
    	public Dimension getPreferredSize()
    	{
    		return new Dimension(width, height);
    		
    	}
    	public static void main(String args[])
    	{
    		LogoAnimator animation = new LogoAnimator();
    		
    		JFrame window = new JFrame("dsaf");
    		
    		Container container = window.getContentPane();
    		container.add(animation);
    		
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		window.pack();
    		Insets insets = window.getInsets();
    		
    		window.setSize(animation.getPreferredSize().width+insets.left+insets.right,
    				animation.getPreferredSize().height+ insets.top+insets.bottom);
    		
    		window.setVisible(true);
    		animation.startAnimation();
    	}
    }
    Last edited by JosAH; 08-26-2011 at 11:19 AM. Reason: added [code] ... [/code] tags.

  2. #2
    ladygagads is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    uhmmm... by the way, it is possible to not use Thread.sleep() command? thanks in advance

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

Similar Threads

  1. Replies: 6
    Last Post: 02-13-2012, 09:57 PM
  2. slowing down playback of mp3
    By paddala in forum Advanced Java
    Replies: 0
    Last Post: 10-17-2010, 12:14 AM
  3. Can we animation in Java Swing?
    By Riaz Ali in forum AWT / Swing
    Replies: 5
    Last Post: 05-06-2010, 03:03 AM
  4. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •