• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

repaint() called too much

I tested the code with .println and discovered the calling of paint is not by my codings at all. How do i rectify it?
The codes here are from my program. "fad" was nto printed out at all. And the error (paint called umpteen times) happened just like that. I don't understand it. It was just suiddenly called and called...
Thanks.

      public void processHTMLParameters() {
            String n;
            n=getParameter("noofimage");
            no= Integer.parseInt(n);
            names=new String[no];
            for (int i=0;i<no;i++) {
                  names[i]=getParameter(""+(i+1));
                  //System.out.println(names[i]+" " +no);
                  if (names[i] == null) {
                        ok=false;
                        showStatus("Error: Names of images");
                        break;
                  }
            }
            if (no == 0) {
                  showStatus("Error: Number of images not given");
                  ok=false;
            }
      }
      
      public void init() {
            processHTMLParameters();
            if (!ok)
                  destroy();
            setBackground(Color.black);
            lBack = new Label("Back", Label.CENTER);
            lNext = new Label("Next", Label.CENTER);
            lRandom = new Label("Random", Label.CENTER);
            
            lBack.setBackground(Color.green);
            lNext.setBackground(Color.green);
            lRandom.setBackground(Color.green);
            
            bot = new Panel(new GridLayout(1,3));
            bot.setBackground(Color.black);
            bot.add(lBack);
            bot.add(lRandom);
            bot.add(lNext);
            //top = new Panel();
            
            track = new MediaTracker (this);
            img = new Image[no];
            for (int i=0;i<no;i++) {
                  img[i]=getImage(getDocumentBase(),names[i]);
                  track.addImage(img[i],i);
            }
            setLayout(new BorderLayout());
            
            add(bot, BorderLayout.NORTH);
            
            lNext.addMouseListener(this);
            lBack.addMouseListener(this);
            lRandom.addMouseListener(this);
            
            lNext.addMouseListener(this);
            lBack.addMouseListener(this);
            lRandom.addMouseListener(this);
            
            addMouseListener(this);
            try {
                  showStatus("Loading images"+current);
                  track.waitForID(0);
            }
            catch (InterruptedException e) {}

            System.out.println(""+getWidth() + " " +getHeight());
            //t=new Thread(this);
      }
      
      public void paint(Graphics g) {            
            int w=img[current].getWidth(this);
            int h=img[current].getHeight(this);
            System.out.println(""+w+" "+h);
            if (w < getWidth())
                  w= (getWidth()-w)/2;
            else w=0;
            
            if (h < getHeight()-50)
                  h= (getHeight()-h)/2;
            else h=0;
            System.out.println(""+w+" "+h);
            
            g.drawImage(img[current],w,h, this);
      }
      
      public void mouseClicked(MouseEvent e){
            if (e.getSource() == lBack) {
                  current=--current%no;
                  showStatus("Getting previous image...");
            }
            else if (e.getSource() == lNext) {
                  current=++current%no;
                  if (!track.checkID(current))
                        showStatus("Getting next image...");
            }
            else if (e.getSource() == lRandom) {
                  current=(int) (Math.random()*no);
                  showStatus("Getting random image "+(current+1)+"...");
                  System.out.println(""+current);
            }
            if (!track.checkID(current)) {
                  t=new Thread(this);
                  t.start();
            }
            else {
                  System.out.println("fad");
                  repaint();
            }
      }
      public void mousePressed(MouseEvent e){}
      public void mouseReleased(MouseEvent e){}
      public void mouseEntered(MouseEvent e){
            if (e.getSource() == lBack) {
                  showStatus("Previous image");
                  lBack.setBackground(Color.blue);
                  lNext.setBackground(Color.green);
                  lRandom.setBackground(Color.green);
            }
            else if (e.getSource() == lNext) {
                  showStatus("Next image");
                  lBack.setBackground(Color.green);
                  lNext.setBackground(Color.blue);
                  lRandom.setBackground(Color.green);
            }
            else if (e.getSource() == lRandom) {
                  showStatus("Random image");
                  lBack.setBackground(Color.green);
                  lNext.setBackground(Color.green);
                  lRandom.setBackground(Color.blue);
            }
            else {
                  showStatus("Getting image done");
                  lBack.setBackground(Color.green);
                  lNext.setBackground(Color.green);
                  lRandom.setBackground(Color.green);
            }
      }            
      public void mouseExited(MouseEvent e){}
      
      public void run() {
            //while (true) {
                  try {
                        Thread.sleep(1000);
                        try {
                              track.waitForID(current);
                              t=null;
                              System.out.println("fad");
                              repaint();
                        }
                        catch (InterruptedException e) {}
                  }
                  catch (InterruptedException e) {}
            }
//      }
}
0
java_kevin
Asked:
java_kevin
  • 4
  • 3
1 Solution
 
dpinnCommented:
Your MediaTracker is not waiting for *all* of the images you added to it.  As I will explain soon, this has the downstream effect of causing paint() to be called many times.

In the snippet of code below - taken from your question - the images get added with monotonically increasing IDs.  

      track = new MediaTracker (this);
      img = new Image[no];
      for (int i=0;i<no;i++) {
            img[i]=getImage(getDocumentBase(),names[i]);
            track.addImage(img[i],i);
      }

A little later on, you call waitForID(0) as follows:
      try {
            showStatus("Loading images"+current);
            track.waitForID(0);
      }
      catch (InterruptedException e) {}

Since you only wait for images with ID 0, the MediaTracker will only wait for the first of your images.  What you should do instead is call waitForAll(); it blocks the executing thread until all of the images have been fully downloaded - I assume that is your intention.

Since your MediaTracker is not waiting for all images to be downloaded, there are some that downloaded incrementally, via the asynchronous mechanism used by the drawImage(...) method.  When you pass this into drawImage, it starts an asynchronous download process which repeatedly calls imageUpdate(...) in java.awt.Component.  ImageUpdate(...) calls repaint(), which calls update(), which finally (whew!) calls paint().  It is possible to reduce the number of calls to paint() by changing the awt.image.redrawrate system property (defaults to 100ms) but you'll find that the call to waitForAll() will fix your problem anyway.
0
 
heyhey_Commented:
> awt.image.redrawrate system property

where can I find more info about these 'strange' system properties :)
0
 
dpinnCommented:
Good question.  I don't know of a place where all possible system properties used by the Java APIs are listed.  The particular one I mentioned in my answer is described in the imageUpdate() JavaDoc.  For your convenience, the following URL will take you straight there.

http://java.sun.com/products/jdk/1.2/docs/api/java/awt/Component.html#imageUpdate(java.awt.Image, int, int, int, int, int)

A list of the normal system properties can be found at the following URL, but I know what you're after and that ain't it.

http://java.sun.com/products/jdk/1.2/docs/api/java/lang/System.html#getProperties()
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
java_kevinAuthor Commented:
Thanks for the additional information.

If I use waiforall, it will kinda hang the program right? my purpose is that, it will load the first image first to display. then it will slowly download the rest of the images. Or will it? I mena, after adding to MT. I thot MT will slowly download the images?

So that, when the user asks for the next pix, it will either be "requested" to be downloaded immediately, or simply displayed on screen.

I mena, my class is meant to be, the user is allowed to choose the number of pix to be displayed. If the nubmer of around 10 and the sizes are pretty big in all, the downloading time will be tremnedous.

I also used a thread to keep track of whether that image has been downloaded. If yes, repaint will then be called. Isn't it so in the above case?
0
 
dpinnCommented:
Your mouseClicked(...) method changes the value of the current variable. The thread that you spin up to do the wait will diligently block until the whole image has been downloaded, but in the meantime, your paint method will get called in the normal order of things - paint is called to update the screen whenever it need to be updated; changing the status display is one example. The call to drawImage(...) then kicks off the asynchronous download process in the *original* thread; imageUpdate() gets called, and then repaint, etc, etc.  I think that's why you're not getting the behaviour you expect.

How about if you leave the value of the current variable alone until after it has been fetched by the run() method. Perhaps you could instead use a 'downloading' variable which is passed to the waitForID() method. When it finishes, set current = downloaded.
0
 
java_kevinAuthor Commented:
Hey, I forgot about that! I mean the "current" var... thanks.

And if you don't mind, can you take a look at a page and see if the "back" and "next" are images or purely g.drawBlaBla

http://www.homestead.com/alvin_ong/pic1.html
0
 
dpinnCommented:
The page looks good to me.  Cute pics!  Your back and next buttons are cool blue images.  Well done.
0
 
java_kevinAuthor Commented:
Thanks for visiting. The page isn't mine. If I were to use the images, and the images are in a polygon shape, do I need to specifically check if the mouse is clicking over that image's position?

I mean, if the image is star shaped, and the user clicked somewhere near the end its point, how do I ensure the mouse isn't outside the shape of the star?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now