Solved

image loading

Posted on 2000-04-07
6
212 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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

 
LVL 7

Expert Comment

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

Expert Comment

by:stalefish
Comment Utility
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
Comment Utility
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

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

15 Experts available now in Live!

Get 1:1 Help Now