Solved

repaint() called too much

Posted on 2000-05-10
8
190 Views
Last Modified: 2010-04-01
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
Comment
Question by:java_kevin
  • 4
  • 3
8 Comments
 

Accepted Solution

by:
dpinn earned 100 total points
ID: 2799163
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2799743
> awt.image.redrawrate system property

where can I find more info about these 'strange' system properties :)
0
 

Expert Comment

by:dpinn
ID: 2802596
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
 

Author Comment

by:java_kevin
ID: 2803059
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:dpinn
ID: 2809216
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
 

Author Comment

by:java_kevin
ID: 2813950
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
 

Expert Comment

by:dpinn
ID: 2815389
The page looks good to me.  Cute pics!  Your back and next buttons are cool blue images.  Well done.
0
 

Author Comment

by:java_kevin
ID: 2820194
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now