Results 1 to 4 of 4
  1. #1
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default 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:
    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);
    		}
    		
    	}
    
    }
    and the Window.java:

    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);
    	}
    
    }
    When you run it, the ball moves, but it's flickering so badly it looks really ugly. Any help would be appreciated.

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

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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)

  3. #3
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default 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.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default 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.

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

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 3
    Last Post: 02-29-2012, 09:38 PM
  2. Item Flickering
    By vincy in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-25-2011, 07:48 AM
  3. Replies: 0
    Last Post: 04-11-2011, 05:28 PM
  4. trying to animate jpanel with button
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-16-2009, 12:02 AM
  5. Applet flickering
    By samson in forum Java 2D
    Replies: 3
    Last Post: 09-21-2007, 10:51 PM

Posting Permissions

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