Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Animated Gif Tearing?  (Read 3071 times)
0 Members and 1 Guest are viewing this topic.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Posted 2010-07-08 21:59:37 »

Got a bit of a problem, trying to play an animated gif in Java2D even though I am using double buffering the animated gif is still tearing every few frames (as if it were only partially drawn).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
        
        protected Image   gifImage;

   protected Image   offscreen;
   
   protected Thread   animationThread;

   
   public void start() {
     
                               animationThread = new Thread() {
               public void run() {
                  while(loaderThread != null) {
                     repaint();
                     MyApplet.this.sleep(100);
                  }
                  animationThread = null;
               }
            };
            animationThread.start();
   }
   
   
   ...

   public final void update(Graphics g) {
      paint(g);
   }
   
   public void paint(Graphics g) {
     
      // create offscreen if missing
     if (offscreen == null) {
         offscreen = createImage(getWidth(), getHeight());
      }

      // draw everything onto an image before drawing to avoid flicker
     Graphics og = offscreen.getGraphics();
     
      // set background color
     og.setColor(Color.black);
      og.fillRect(0, 0, getWidth(), getHeight());

      // get logo position so its in the middle of applet
     int x = (getWidth() - gifImage.getWidth(this)) / 2;
      int y = (getHeight() - gifImage.getHeight(this)) / 2;
     
      // draw animated gif
     og.drawImage(gifImage, x, y, null);
         
      og.dispose();

      // finally draw it all
     g.drawImage(offscreen, 0, 0, null);
   }  


it doesn't happen every frame but after random interval.

anyone got any idea's or fixes for this?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-07-09 08:33:06 »

In this ancient thread it seems like a Java2D bug, and there is code to grab frames from the GIF and roll your own animator.


Hope this helps Roll Eyes

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #2 - Posted 2010-07-09 10:50:42 »

In this ancient thread it seems like a Java2D bug, and there is code to grab frames from the GIF and roll your own animator.


Hope this helps Roll Eyes


Ah thx.

I was hoping to avoid rolling my own animation and timer from gif frames since code size is a limit in this case and would like it to remain as small and simple as possible.

Anyway I'll have a further look but I did notice that this seems to be some sort of Toolkit timer issue and the tearing is due to the gif image getting drawn as it switches frames. If I use an ImageObserver without using another thread to call repaint, e.g.
1  
og.drawImage(gifImage, x, y, this);

then the problem disappears and I get smooth tear free animation, however if I use two gifs both with their own ImageObservers then I get some tearing, probably due to one gif calling repaint while the other gif is changing frames.

Not sure where the timer is that updates the gif animation but finding that might be a good place to start.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #3 - Posted 2010-07-09 11:41:53 »

as its for the appletloader is it out of the question to load an array of images?

People could pass the name of the image, plus a (optional)count <param> to the applet  with a defaul value as 1.
if you want to keep coding to a minimum.

so people can still pass the gif, as it is currently implemented. without changing any html script.

but if they dont like the flicker, they can take the time to import a list of jpg's. and add the extra paramater.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2010-07-09 16:34:36 »

Ah thx.

I was hoping to avoid rolling my own animation and timer from gif frames since code size is a limit in this case and would like it to remain as small and simple as possible.

Anyway I'll have a further look but I did notice that this seems to be some sort of Toolkit timer issue and the tearing is due to the gif image getting drawn as it switches frames. If I use an ImageObserver without using another thread to call repaint, e.g.
1  
og.drawImage(gifImage, x, y, this);

then the problem disappears and I get smooth tear free animation, however if I use two gifs both with their own ImageObservers then I get some tearing, probably due to one gif calling repaint while the other gif is changing frames.

Not sure where the timer is that updates the gif animation but finding that might be a good place to start.


It might help to put each GIF in its own Component. They will have independent Toolkit-triggered repaints.

How to integrate that into your current project, plus using transparant GIFs on heavyweight components, is left as an exercise for the reader Wink


Seriously, it's probably easier to roll your own.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #5 - Posted 2010-07-10 00:39:24 »

http://www.permadi.com/tutorial/javaImgObserverAndAnimGif/
this explains how the listener handles the gif flags.

at the start of the paint method, set something like "painting = true;"

at the end set it to false;
and in the imageUpdate method
do something like:
1  
if (!painting) super.imageUpdate();


or possible override the image update method to do nothing, because all it seems to do is call repaint, but you set the appletlaoder to force repaint every 100ms anyway.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline DzzD
« Reply #6 - Posted 2010-07-10 09:40:28 »

I find result is pretty good using old way of ImageObserver

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
class MayApplet implements ImageObserver
....
....
public void paint(Graphics g)
{
 this.checkBackGraphics(); //create/resize backGraphics/backImage if needed
this.backGraphics.drawImage(this.myGif,0,0,this);
 g.drawImage(this.backImage,0,0);
}
...
...
public void imageUpdate(Image img,....,int flag)
{
 if(flag&ImageObserver.FRAMEBITS ) //new animated gif image ready ?
{
  this.paint(this.getGraphics()); //direct painting ( may be replaced by this.redraw() )
}
 return true; //return true to recevice further call to imageUpdate and continue animation of the gif
}

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #7 - Posted 2010-07-10 10:31:16 »

oh great, thx ppl, will test this out.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #8 - Posted 2010-07-10 12:31:59 »

ok brilliant, this method does fix the issue. plus only uses a minimal amount of code.

thx.
Offline bobjob

JGO Knight


Medals: 10
Projects: 6


David Aaron Muhar


« Reply #9 - Posted 2010-07-11 07:23:47 »

or possible override the image update method to do nothing, because all it seems to do is call repaint, but you set the appletlaoder to force repaint every 100ms anyway.
I tried this on the applet loader and it seems to work fine
all you need to add is:
1  
2  
3  
public boolean imageUpdate( Image i, int flags, int x, int y, int w, int h)  {
   return true;
}

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #10 - Posted 2010-07-11 11:18:53 »

yup its already fixed and patch is applied.

But yeh Applet does implement ImageObserver.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!