Results 1 to 4 of 4
- 09-04-2012, 12:40 AM #1
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
How to animate without flickering?
Hi. I am trying to make a game, but whenever I repaint() after moving a sprite, I get really bad flickering. I made a small example:
and the Window.java:Java Code:import java.awt.Color; import java.awt.Graphics; import java.util.Random; public class Problem { public static void main(String[] args) throws InterruptedException{ Random rnd=new Random(); int x=0; int y=100; Window.init(); Graphics g=Window.win.getGraphics(); while(true){ g.setColor(new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); Window.win.repaint(); g.fillOval(x, y, 200, 200); x++; if(x>500){ x=0; } Thread.sleep(100); } } }
When you run it, the ball moves, but it's flickering so badly it looks really ugly. Any help would be appreciated.Java Code:import javax.swing.JFrame; public class Window { public static JFrame win=new JFrame(); public static void init(){ win.setTitle("Error"); win.setSize(500,450); win.setLocationRelativeTo(null); win.setResizable(false); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } }
P.S. Here's the code without the formatting/numbers (for copying):
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Problem {
public static void main(String[] args) throws InterruptedException{
Random rnd=new Random();
int x=0;
int y=100;
Window.init();
Graphics g=Window.win.getGraphics();
while(true){
g.setColor(new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
Window.win.repaint();
g.fillOval(x, y, 200, 200);
x++;
if(x>500){
x=0;
}
Thread.sleep(100);
}
}
}
Window.java:
import javax.swing.JFrame;
public class Window {
public static JFrame win=new JFrame();
public static void init(){
win.setTitle("Error");
win.setSize(500,450);
win.setLocationRelativeTo(null);
win.setResizable(false);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
win.setVisible(true);
}
}
- 09-04-2012, 01:50 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: How to animate without flickering?
I recommend reading up on how to correctly drawing using Swing components. You should never need to call getGraphics() unless you truly know what you are doing - override paintComponent and use the passed Graphics instance to do the drawing. If you need to repaint, then call the method with that name. See Trail: 2D Graphics (The Java™ Tutorials)
- 09-24-2012, 02:11 AM #3
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
Re: How to animate without flickering?
Sorry for the delay, I figured it out with double buffering. Thanks for the help, you pointed me in the right direction.
- 05-16-2013, 05:13 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 681
- Rep Power
- 1
Re: How to animate without flickering?
You should not need to worry about double buffering since Swing does that for you (AWT is another matter). You may have figured it but as was stated you should not be calling getGraphics() or doing paint outside of the paint methods. I don't normally provide completed code but here is a more conventional approach (but not optimal or threadsafe). Also, don't forget to check out the previous mentioned tutorials and also painting in the Java Tutorials (see signature), especially the section which describes painting in Swing.
Note: this is a simple example which I based on your code. Normally, I would write this so I could invoke the constructor on the Event Dispatch Thread. However, the design does not allow that in this case unless I use another thread as the control loop. So I left it as is.Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class Problem extends JPanel { JFrame frame = new JFrame(); Random rnd = new Random(); int x; int y = 100; public Problem() { frame.setPreferredSize(new Dimension(500,500)); frame.add(this); frame.pack(); frame.setLocationRelativeTo(null); // center on screen frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) throws InterruptedException { Problem prob = new Problem(); while (true) { prob.repaint(); Thread.sleep(50); prob.x++; if(prob.x>500){ prob.x=0; } } } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); g.fillOval(x, y, 200, 200); } }
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Flickering, need help finding a way to get it out
By Atynine in forum Java 2DReplies: 3Last Post: 02-29-2012, 09:38 PM -
Item Flickering
By vincy in forum CLDC and MIDPReplies: 0Last Post: 10-25-2011, 07:48 AM -
Alter Components Graphics and make them Animate
By santeron in forum Java 2DReplies: 0Last Post: 04-11-2011, 05:28 PM -
trying to animate jpanel with button
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-16-2009, 12:02 AM -
Applet flickering
By samson in forum Java 2DReplies: 3Last Post: 09-21-2007, 10:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks