Results 1 to 7 of 7
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Image Animation flickering

    I've tried to look up on google about flickering but nothing seems to make this disappear.

    This is what I got right now:

    Java Code:
    //This is just a method/function I picked up from an old (Applet) project with flickering problem
    	public void update(Graphics g){
    		paint(g);
    		if (dblImage == null) {
    			dblImage = createImage(this.getSize().width, this.getSize().height);
    			dbg = dblImage.getGraphics();
    		}
    		dbg.setColor(getBackground());
    		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
    		dbg.setColor(getForeground());
    		paint(dbg);
    
    		g.drawImage(dblImage, 0, 0, this);
    	}
    	
    	public void paint(Graphics g){
    		super.paint(g);
    		//adds ALL images to my storage (player,mob,level etc.)
    		game.draw();
    		g.fillRect(0, 0, screenwidth, screenheight);
    		//draws all the images in my storage
    		imh.draw(g);
    		if(debug.debug){
    			debug.draw(g);
    		}
    		g.dispose();
    		//clear my image storage
    		imh.resetImages();
    	}
    game.draw() is just a method that adds images to my ImageHandeler (imh) which stores all images it gets and draws them when imh.draw(g) is called.
    debug.draw(g) simply draws rectangles for collision debugging.

    The flickering I'm having is when the image of the player is changed it flickers. The way I'm changing images is like this:
    Java Code:
    if(frame >= time){
    	frame = 0;
    	if(image == imagewalk1){
    		image = imagewalk2;
    	}else{
    		image = imagewalk1;
    	}
    }else{
    	frame++;
    }
    This is just a ruff code I just made but this is kinda how I change my images. And it seems like when it changes my player flickers.

    I've tried paintComponent(Graphics g), didn't work.

    If you help me, THANKS!

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,805
    Rep Power
    19

    Default Re: Image Animation flickering

    Stick with paintComponent().
    That's part 1, and what anyone else would say as well.
    Don't override paint().

    Don't dispose the graphics either.
    They're not yours to dispose of.

    How are you calling repaint()?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Image Animation flickering

    The repaint is called in a while loop:

    Java Code:
    	public void run(){
    		requestFocus(); 
    		timer = new Timer(30,1,true);
    		Timer timer2 = new Timer(1,1,false);
    		int frame = 0;
    		while(running){
    			if(timer.hasTimeGone()){
    				game.update();				
    			}
    			repaint();
    			frame++;
    			if(timer2.hasTimeGone()){
    				ups = timer.getCount();
    				fps = frame;
    				System.out.println(fps + " : " + ups);
    				timer.resetCount();
    				frame = 0;
    				
    			}
    		}
    	}
    I have coded the timer myself but that has no effect to the drawing just to the updating of the player, mobs etc.

    This is actually not a Thread just a regular method that pretty much never end.

    This is pretty much what the code is like:

    Java Code:
    public void run(){
    	while(running){
    		repaint();
    	}
    }

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

    Default Re: Image Animation flickering

    Can you post a small compilable example which demonstrates the flicker? I haven't had that much of a flicker problem in my graphics (even when I fail to calculate clip regions) but I have done a fair amount of animations. If you post the code I will see what I can find out.

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

  5. #5
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Image Animation flickering

    For some reason I can't reproduce the flickering, grrr. Could the flicker come from to many images drawing or many things updating. I've noticed that the flickering goes away after about 10 seconds after moving the player around. Something that I didn't point out was that it is only the player that is flickering and nothing else.

    What is really the cause of flickering. Why can't the computer just render it without it?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: Image Animation flickering

    Well, a couple of things you might try.

    1. Don't call paint directly from update(). Use super.update(g). Especially since you will no longer be using paint but paintComponent. Note: I may be wrong about this and if so, you'll soon find out.
    2. I haven't used update since I went from AWT to Swing. I am not saying you don't have too, but I have never needed to use it. So you might try moving move of your painting code into paintComponent. And if that doesn't work you may want to try calculating a clip region for painting.

    Also check out the following links.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Painting in AWT and Swing.

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

  7. #7
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Image Animation flickering

    I've fixed it now.
    Java Code:
    //old code
    image = ImageIcon("path").getImage()
    
    //new code
    try {
    	image = ImageIO.read(URL);
    } catch (IOException e) {
    	e.printStackTrace();
    }
    Apparently using ImageIcon doesn't render the image the first time it draws the graphics to the screen. This made me think that it was a flickering issue because it was an animation which contained 9 different stages and all of them had to disappear once. ImageOI.read() don't have this weird feature/bug.

    Thanks for all of your help though

Similar Threads

  1. How to animate without flickering?
    By Farscience in forum Java 2D
    Replies: 3
    Last Post: 05-16-2013, 05:13 AM
  2. Flickering on a 2D Sprite
    By driiper in forum Java 2D
    Replies: 5
    Last Post: 05-06-2012, 05:30 PM
  3. Replies: 3
    Last Post: 02-29-2012, 09:38 PM
  4. Item Flickering
    By vincy in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-25-2011, 07:48 AM
  5. Screen Flickering
    By dewitrydan in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 07:45 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
  •