Solved

Loading multiple images from One Image Map

Posted on 2000-02-17
7
362 Views
Last Modified: 2008-02-26
Hi there.

Here's my problem: I need to load many images into my gui. I would like to load one gif file that contains an imageMap of all the icons needed, and load one text file that tells me the xy coordinates of all these icons. I then would like to create a seperate image for each icon, and place them in a repository (hashtable). I figured I would save time by only loading one gif file as opposed to 50 to 100 gif files. The less the program needs to access the harddrive the better right?

I can load the file fine, and get the text description ok, but I'm stuck on how to create the seperate images from the one imageMap. Normally, if my class extended Component, then I could just call createImage, but I'm under the impression that this will only work if that component is visible.  Besides I'm trying to have a small class here, and I don't need the overhead of Component.

Does anyone have a clue how I would do this??

Thanks


0
Comment
Question by:stalefish
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 2533845
You have here a method of mine which can read an gif image wich contains 45 icons arranged in line. You see in the code "treeBuilder" object. This is the instantiation of a class which extends java.applet.Applet;
Also I supose that each image (icon) is a square with 16 width&heigth ( non english speaker).

    public void buildImgVector()
    {
      if(iconArray == null)
          iconArray = new Image[45];
      Object obj;
      try
      {
          Image image = treeBuilder.getImage(treeBuilder.getCodeBase(), "iconmap.gif");
          treeBuilder.prepareImage(image, treeBuilder);
          MediaTracker mediatracker = new MediaTracker(treeBuilder);
          mediatracker.addImage(image, 0);
          mediatracker.waitForID(0);
          int j = 0;
          do
              iconArray[j] = treeBuilder.getToolkit().createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(j * 16, 0, 16, 16)));
          while(++j < 45);
          return;
      }
      catch(Exception _ex)
      {
          obj = null;
      }
    }
0
 
LVL 8

Expert Comment

by:diakov
ID: 2534059
yeah, this would do.

Nik
0
 
LVL 2

Author Comment

by:stalefish
ID: 2535688
It looks like it should work but I must be doing something  wrong. I load the imagemap in correctly and I verify that it is the correct size (in this case 640X480). And then I attempt to create the smaller images out of the larger one using your method, but all the images return that the Width and Height are -1.

Heres what I used:

iTemp = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(map.getSource(), new CropImageFilter(x, y, w, h)));

System.out.println("Temp : W = " + iTemp.getWidth(this) + " H=" + iTemp.getHeight(this));

where x,y,w, and h are all known and correct values.

Why does it always return -1?

Thanks
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Author Comment

by:stalefish
ID: 2535750
Ok I just figured out that if I put a mediaTracker after each iteration of iTemp, then I get the correct image back.

Is there any way of avoiding the media tracker? Why do I have to use it in my code, and you don't in yours?

I'm trying to make this method as fast as possible.

Thanks
0
 
LVL 2

Author Comment

by:stalefish
ID: 2535858
Ok I just figured out that if I put a mediaTracker after each iteration of iTemp, then I get the correct image back.

Is there any way of avoiding the media tracker? Why do I have to use it in my code, and you don't in yours?

I'm trying to make this method as fast as possible.

Thanks
0
 
LVL 8

Expert Comment

by:diakov
ID: 2536608
The problem stems from the fact that the getImage returns immediatelly and you cannot be sure the bits that comprise the image have arrived. That is why MediaTracker is needed. If this 'loading' makes you application GUI freeze, use another thread, and change the GUI as appropriate when the image arrives.

Cheers,
  Nik
0
 
LVL 9

Accepted Solution

by:
Ovi earned 100 total points
ID: 2541285
..I'm glad that is working...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 182
starter POM and spring-boot-starter,  spring-boot-web 2 53
stackato and cloud 4 85
diffSum example 4 36
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
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.

813 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