Results 1 to 12 of 12
Thread: Animation problem
- 02-18-2012, 11:15 AM #1
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
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); } } ); } }
- 02-18-2012, 01:53 PM #2
Re: Animation problem
Hard to test without the set of images.
- 02-18-2012, 02:50 PM #3
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-19-2012, 01:34 AM #4
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
- 02-19-2012, 02:18 AM #5
Re: Animation problem
How can you tell it flickers? The changing of the images produces a flickering effect.
- 02-19-2012, 04:56 AM #6
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
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
- 02-19-2012, 05:59 AM #7
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-19-2012, 09:09 AM #8
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
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.
- 02-19-2012, 01:45 PM #9
Re: Animation problem
I ran the code and do not see what you are saying.
- 02-19-2012, 01:49 PM #10
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
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?
- 02-19-2012, 01:51 PM #11
Re: Animation problem
I'm using jdk1.6.0_29 on Windows XP
- 02-19-2012, 02:04 PM #12
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
Animation
By ryainad in forum Advanced JavaReplies: 1Last Post: 04-04-2011, 05:52 PM -
Animation randomly changing speed problem - Please Help!
By andyman99008 in forum Java 2DReplies: 14Last Post: 12-21-2010, 11:14 PM -
Problem with my animation applet
By cblue in forum Java 2DReplies: 0Last Post: 08-24-2009, 04:24 PM -
Animation Delay - Thread problem
By wererabit in forum Advanced JavaReplies: 3Last Post: 04-10-2009, 10:35 PM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks