Solved

image loading

Posted on 2000-04-07
6
225 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

21 Experts available now in Live!

Get 1:1 Help Now