Results 1 to 12 of 12
  1. #1
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Animation problem

    Hi, I am trying to make an animation for a graphical equalizer. So far I can get it to compile an run. But the animation only runs smoothly when the JPanel is focused with the mouse cursor. If I move the mouse cursor off the JPanel - animation flickers. So my question is how do I fix it so the animation runs smoothly all the time?

    Code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    import java.net.*;
    
    public class myPanel extends JPanel{
    
    	Image[] images = new Image[7];
    	InputStream[] isimg = new InputStream[7];
     	
    	String filename;
    
    	Boolean isUp = true;
    	Boolean isDown=false;
    	
    	int i;
    	int y=0;
    
    	public myPanel(){
    		super();
    		setLocation(0,0);
    		setSize(208, 248);
    		filename = new String("voice");
    		getImages();
    		
    		setBackground(Color.BLACK);
    		
    		timer.start();
    	}
    public final Timer timer = new Timer(18, new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
    	   
     	
         	repaint();
    	revalidate();
    			
    			   }
    
    });
    
    public void getImages(){
    	
    	for(i=0; i<7; i++){
    	try{
    	// Read from a URL
        	//URL url = new URL("/home/aubrey/kitt7.2/Jetty2/"+filename+i+".gif");
    
    	// Read from a File
    	File url = new File(filename+i+".gif");    	
    	images[i] = ImageIO.read(url);
    	} catch (IOException e) {
    	}
    	//Read from a JAR
    	//isimg[i] = imageApplet.class.getResourceAsStream(filename+i+".gif");
    	//images[i] = ImageIO.read(is[i]);
    	//System.out.println(filename+i+".gif");
    
    	}
    
    
    }
     public void paintComponent(Graphics g) {
              		
    	//super.paintComponent(g);
    
    				
    
    		if(isUp){
    		if(y==5){
    		isUp=false;
    		isDown=true;
    		}
    		g.drawImage(images[y], 0, 0, null);
    		//System.out.println("printing: "+y);
    		y++;	
    		}
    		if(isDown){
    		if(y==1){
    		isDown=false;
    		isUp=true;
    		}
    		g.drawImage(images[y], 0, 0, null);
    		//System.out.println("printing: "+y);
    		y--;	
    		}
    	}
    
    public static void main(String[] args){
    
    			SwingUtilities.invokeLater(
        new Runnable(){
            public void run(){
              JFrame f = new JFrame();
    			
    				
    			f.setSize(208, 248);
    			myPanel p = new myPanel();
    			//p.setDoubleBuffered(false);
    			System.out.println("Double Buffered: "+p.isDoubleBuffered());
    		
    			f.add(p);
    			
    		
    			f.setVisible(true);
            }
        }
    );
        
     }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Animation problem

    Hard to test without the set of images.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Animation problem

    Moved from New to Java.

    Improve your code formatting by using consistent indents and more people are likely to read your code.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: Animation problem

    Quote Originally Posted by Norm View Post
    Hard to test without the set of images.
    Ok, have compressed them into a zip file for you (or anyone who wants to try running the code!). Hi Darryl, liking this new forums.

    Regards
    Aubrey.
    Attached Files Attached Files

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Animation problem

    How can you tell it flickers? The changing of the images produces a flickering effect.

  6. #6
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: Animation problem

    No not exactly flickering, sorry. What I meant to say is that when the mouse cursor is over the animation - it runs full speed. But when I take the cursor off the JPanel it slows down considerably. I can get a nice effect when the mouse is over the JPanel. But without the mouse focus it looks very "blocky" or "chunky". Not smooth like it should

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Animation problem

    Why the revalidate() in the Timer's actionPerformed? Are you adding or removing any components that the container needs to be revalidated?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: Animation problem

    The main reason its there is because I can notice a huge difference in the animation when focused. I don't know why, but it does. And yes this program is just an example - i actually am adding/hiding/showing/removing/disposing this animation in the main part of my real app.

    Do you understand my problem? It would be fairly obvious if you copy the code and run it, using the images in attached zip.

    Regards
    Aubrey.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Animation problem

    I ran the code and do not see what you are saying.

  10. #10
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: Animation problem

    No way, are you sure? You dont notice a difference in the animation speed on and off mouse focus? Can anyone else confirm this? What JRE are you using? Im using openJDK 1.6 on Linux - so perhaps i should try another jvm?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Animation problem

    I'm using jdk1.6.0_29 on Windows XP

  12. #12
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: Animation problem

    Ok Norm, I might just try another jdk here. Thanks for the feedback.

Similar Threads

  1. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 06:52 PM
  2. Replies: 14
    Last Post: 12-22-2010, 12:14 AM
  3. Problem with my animation applet
    By cblue in forum Java 2D
    Replies: 0
    Last Post: 08-24-2009, 05:24 PM
  4. Animation Delay - Thread problem
    By wererabit in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 11:35 PM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 04:37 AM

Tags for this Thread

Posting Permissions

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