Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

image loading

Posted on 2000-04-07
6
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

715 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