Solved

image loading

Posted on 2000-04-07
6
236 Views
Last Modified: 2008-03-06
how can i make my image loading smoother and faster. And also how can i make sure that it would be loaded.
0
Comment
Question by:pogs
6 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2693002
How is this different from what I proposed? What is "smooth loading" exactly?
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 50 total points
ID: 2692883
I'm not sure what you mean by loading it smoother, but you can't load it faster than your internet connection allows it.
About loading an image before you actually display it (in which case it will just paint it entirely on the first attempt) it is done like this:


public static void loadImage(Image im, Component forThisComponent){
  MediaTracker tracker = new MediaTracker(forThisComponent);
  tracker.addImage(im,1);
  tracker.waitForID(1);
}


Here is an example of more MediaTracker functionality given in the documentation:

import java.applet.Applet;
      import java.awt.Color;
      import java.awt.Image;
      import java.awt.Graphics;
      import java.awt.MediaTracker;

      public class ImageBlaster extends Applet implements Runnable {
             MediaTracker tracker;
             Image bg;
             Image anim[] = new Image[5];
             int index;
             Thread animator;

             // Get the images for the background (id == 0)
             // and the animation frames (id == 1)
           // and add them to the MediaTracker
             public void init() {
                 tracker = new MediaTracker(this);
                 bg = getImage(getDocumentBase(),
                       "images/background.gif");
                 tracker.addImage(bg, 0);
                 for (int i = 0; i < 5; i++) {
                     anim[i] = getImage(getDocumentBase(),
                           "images/anim"+i+".gif");
                     tracker.addImage(anim[i], 1);
                 }
             }

             // Start the animation thread.
             public void start() {
                 animator = new Thread(this);
                 animator.start();
             }

             // Stop the animation thread.
             public void stop() {
                 animator = null;
             }

             // Run the animation thread.
             // First wait for the background image to fully load
           // and paint.  Then wait for all of the animation
             // frames to finish loading. Finally, loop and
             // increment the animation frame index.
             public void run() {
                 try {
                     tracker.waitForID(0);
                     tracker.waitForID(1);
                 } catch (InterruptedException e) {
                     return;
                 }
                 Thread me = Thread.currentThread();
                 while (animator == me) {
                     try {
                         Thread.sleep(100);
                     } catch (InterruptedException e) {
                         break;
                     }
                     synchronized (this) {
                         index++;
                         if (index >= anim.length) {
                             index = 0;
                         }
                     }
                     repaint();
                 }
             }

             // The background image fills the frame so we
             // don't need to clear the applet on repaints.
           // Just call the paint method.
             public void update(Graphics g) {
                 paint(g);
             }

             // Paint a large red rectangle if there are any errors
             // loading the images.  Otherwise always paint the
             // background so that it appears incrementally as it
           // is loading.  Finally, only paint the current animation
             // frame if all of the frames (id == 1) are done loading,
             // so that we don't get partial animations.
             public void paint(Graphics g) {
                 if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
                     g.setColor(Color.red);
                     g.fillRect(0, 0, size().width, size().height);
                     return;
                 }
                 g.drawImage(bg, 0, 0, this);
                 if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
                     g.drawImage(anim[index], 10, 10, this);
                 }
             }
      }
0
 
LVL 3

Expert Comment

by:rjackman
ID: 2692893
u ca use media tracker for smooth loading aswell as for making sure that image gets loaded
eg.
import java.applet.*;
import java.awt.*;
public class Image extends Applet
{
  private Image img;
  private MediaTracker mt;
  public void init()
{
  img  = getImage(getDocumentBase(),"imagename");
  mt = new MediaTracker(this);
  mt.addImage(img);
//other code
}
}

this is sample code for smooth startup of imageloading
RJackman
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2693063
How is this different from what I proposed? What is "smooth loading" exactly?
0
 
LVL 2

Expert Comment

by:stalefish
ID: 2693886
I don't know what type of files your using, but if your using a gif image, then a jpg can be much smaller (therefore faster) if the jpg quality settings are very low. However if you have a high definition jpg image, you would have a smaller picture if you used a gif.
0
 

Author Comment

by:pogs
ID: 2698718
thanks guys ...I think I have proposed 50 points for it. Anyway, I actually want to use this idea for loading an image from a browser to the application that I have created (for example use it as an icon for my button). Thanks again.

"ingats...." in our language "take care".
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

815 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

9 Experts available now in Live!

Get 1:1 Help Now