Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2003

Tip: Looking for answers? Try searching our database.

Java applet flickering when using own class that extends canvas

Thread view: 
Jaakko Lauri - 27 Aug 2003 09:30 GMT
Hi!

I'm making an applet which uses AWT. I'm using double buffering so
normal images won't flicker. I have made my own class ImageButton
which extends canvas. Every time ImageButton is drawn it flickers.
What i should do that my applet won't flicker?

Here is code of ImageButton:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ImageButton extends Canvas
{
private Image img;
   private String teksti="";
   private int vaaka=40,pysty=20;
   Gfx gfx;
   Color vari= new Color(64, 76, 130);
   
    /**
    * ImageButton(Image initImg)
    *
    * Constructor.
    */
    public ImageButton(Image initImg)
    {
        img = initImg;
        setSize(img.getWidth(this), img.getHeight(this));
    }
       public ImageButton(Image initImg, String teksti)
       {
               img = initImg;
               setSize(img.getWidth(this), img.getHeight(this));
               this.teksti=teksti;
       }

       public ImageButton(String teksti){

         this.teksti=teksti;

       }

    /**
    * int getWidth()
    *
    * Returns the width of the ImageButton (width of Image).
    */
    public int getWidth()
    {
        return getSize().width;
    }

    /**
    * int getHeight()
    *
    * Returns the height of the ImageButton (height of Image).
    */
    public int getHeight()
    {
        return getSize().height;
    }

       
    /**
    * void paint(Graphics g)
    *
    */
    public void paint(Graphics g)
    {

       g.setColor(vari);
       Font f = new Font("Helvetica", Font.BOLD, 14);
            g.setFont(f);
           g.drawImage(img, 0, 0,this);   
           g.drawString(teksti,vaaka,pysty);
       
   }

}

J L Timonen
Babu Kalakrishnan - 29 Aug 2003 19:16 GMT
> Hi!
>
> I'm making an applet which uses AWT. I'm using double buffering so
> normal images won't flicker. I have made my own class ImageButton
> which extends canvas. Every time ImageButton is drawn it flickers.
> What i should do that my applet won't flicker?

[code snipped]

You aren't really using double buffering at all (going by the code you
posted).

To perform real double buffering :

You create a BufferedImage corresponding to what is to be displayed on the
object.

All your display modifications are done on that BufferedImage - never
directly on the actual Graphics object that represents the screen.

And all you do in the paint method is to repaint the image on to the
Graphics object that is passed to you.

If you take a second look at your code, you'll realize that you're
writing directly to the Graphics object (the DrawString call), so the
whole purpose of double-buffering is defeated.

Look at the examples in the java tutorial for examples for how to do
it right.

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.