dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Flickering - Drawing Graphics onto a Java Applet

  1. #1
    Join Date
    Mar 2010
    Posts
    48

    Flickering - Drawing Graphics onto a Java Applet

    I am having problems with flickering when drawing graphics onto a java applet. The code I am using to do this is as follows:

    Code:
            public static void testDrawingLoop()
            {
                IntPtr hWnd = windowInterface.retrieveJavaApplet();
                Graphics g = retrieveGraphicsHandle(hWnd);
                int time = System.Environment.TickCount+(1000/60);
                while (true)
                {
                    if (System.Environment.TickCount >= time)
                    {
                        time = System.Environment.TickCount + (1000 / 60);
                        drawBitmapOnApplet(g, Bitmap.FromFile("buttonPopup.bmp"), 100, 100);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep((1000 / 60));
                    }
                }
            }
    hWnd is a handle to the Java Applet, g will be the graphics handle. What happens is when I run this, it draws the Bitmap, though it is flickering. As you see, I have tried to syncronise it with the refresh rate of my monitor, however it does the same thing.

    It could be that the java applet runs on a different graphics device or a different refresh rate. Any help would be greatly appreciated.

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Flickering - Drawing Graphics onto a Java Applet

    It could be a number of things, but only you can tell for sure.

    I don't know what exactly drawBitmapOnApplet() does, since you've posted no code.
    Maybe it causes the Java applet to redraw completely?
    Does the image that the default drawing method creates (the background) shows very briefly before buttonPopup.bmp becomes visible?

    Generally, flickering problems are reduced or eliminated if a back buffer is used. You first draw everything you have to draw on an invisible surface (a simple bitmap will do), and once the drawing is done, you show that (you can just draw that on the client area).

  3. #3
    Join Date
    Mar 2010
    Posts
    48

    Re: Flickering - Drawing Graphics onto a Java Applet

    I've been doing a lot of self research into this problem. "drawBitmapOnApplet()" does this:

    g.DrawImageUnscaled(b, 100, 100);

    With b being a Bitmap and g being the Graphics object. Essentially, what I think is happening is the java applet(Window) I'm drawing onto refreshes at a variable rate, so when I draw my Bitmap onto it, it may repaint before I draw another Bitmap on, hence creating a flickering effect.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Flickering - Drawing Graphics onto a Java Applet

    Can you code the applet's drawing behavior? I'm not sure there's much else you can do...
    You can try using BufferedGraphics and BufferedGraphicsContext classes.

    Something like this, but I'm not sure if it'll work:

    IntPtr hWnd = windowInterface.retrieveJavaApplet();
    Graphics g = retrieveGraphicsHandle(hWnd);

    BufferedGraphicsContext context;
    BufferedGraphics bg;

    context = BufferedGraphicsManager.Current;
    context.MaximumBuffer = new Size(width+1, height+1); // see the MSDN article for explanation of this line

    bg = context.Allocate(g, new Rectangle( 0, 0, width, height ));


    (Please follow the links provided for more info.)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)