Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Image loading

Thread view: 
s.magnien@gmail.com - 06 Jun 2005 01:37 GMT
Hello, I have two classes: main & room

//main.java
import java.awt.*;
import java.applet.*;
public class main extends Applet
   {
   Image image;
   room current_room;
   public main() {}
   public void init()
       {
        image=getImage(getCodeBase(), "someimage.png");
        current_room=new room(image);
       }
   public void paint(Graphics g)
       {
        current_room.paint(g);
       }
   }

//room.java
import java.awt.*;
import java.applet.*;

public class room extends Applet
    {
    Image image;
    public room(Image _image)
      {
         image=_image;
      }
   public void init()
       {
       }
   public void paint(Graphics g)
       {
        g.drawImage(this.image, 100, 100, null);
       }
    }

This works like a charm. However, I'd like room to load its image(s)
directly, either in room() - gather it ain't possible from tutorials I
saw, in init(), didn't work when I tried, in paint() - neither, or in a
custom method LoadTile(), didn't work also; I tried every combination I
could think of, and though it never worked, I didn't get any exception
so the image must have loaded, yet it didn't show on screen.

I have browsed Applet, Image, Graphics in the API - something I must
have missed, but difficult to know what's missing till you have some
knowledge of the language - which I don't - yet. Any pointers?

Thank you very much.
Knute Johnson - 06 Jun 2005 02:37 GMT
> Hello, I have two classes: main & room
>
[quoted text clipped - 49 lines]
>
> Thank you very much.

You don't need two applets to do the job of one.  The 'this' in the
ImageObserver place in the drawImage() is very important to making it
all work.  Take a look at the API docs for getImage().  getImage()
returns immediately and the ImageObserver interface of Applet takes care
of the image download and presentation to the paint method.

import java.awt.*;
import java.applet.*;

public class room extends Applet {
    Image image;

    public void init() {
        image = getImage(getCodeBase(),"saturn.jpg");
    }

    public void paint(Graphics g) {
        g.drawImage(image,0,0,640,480,this);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Andrew Thompson - 06 Jun 2005 11:01 GMT
> Hello, I have two classes: main & room
>
[quoted text clipped - 38 lines]
>
> This works like a charm.

The first thing that you need to ask yourself is ..why?

Now, to give you a quick shortcut, I suspect the reason
is as follows.

Images (generally) load asynchronously, Java loads them while continuing
to do other things.  The image is not fully loaded by the time applet
'main'  (poor name for it, BTW*) has finished it's init().  

The way this code is doing it, loading a second applet, delays the
display just long enough to load the image.  This is a very fragile
way to do it, since different VM's and image loading conditions
will break it.  E.G. The MSVM (a very small and ancient VM -
quick to load) loading the images off the net would probably break again.

*
1) There is a ..
 public static void main(String[] args)
..method that starts Java _applications_, a class name of 'main'
is confusing.
2) The naming conventions in Java are to spell class names EveryWordCapital
and method names firstWordLowerCase().

>..However, I'd like room to load its image(s)
> directly, either in room() - gather it ain't possible from
> tutorials I saw, ..

..what else were those tutorials saying?

> I have browsed Applet, Image, Graphics in the API - something I must
> have missed, ...

The 'MediaTracker' class is one way to track the loading of images.
Did you see that in your travels?

OTOH, I had problems with Sun's entire image loading strategy
in terms of image caching (it becomes a problem if you want
to browse hundreds of images), so instead I go to the URL's
directly and get the bytes (synchonously), then use the Toolkit
methods that stamp out Image's from the byte arrays.

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

s.magnien@gmail.com - 06 Jun 2005 22:37 GMT
First, thanks Andrew and Knute;

the problem is my images are tiles - the fattest one weighs 270 bytes -
so it almost certainly isn't a network loading time problem, especially
testing it on 127.0.0.1 .

No, I think, for some reason the whole construct is flawed, like you
can't load images in constructor(), perhaps you can't load them in some
less documented places.

But don't you worry; I will find a way. Perhaps a JApplet as main
class, and a JPanel for the secondary class, and JPanel could load the
Images on its own, perhaps as ImageIcons?

I'm really too new to Java to say, but there must be a way!
Thomas G. Marshall - 21 Jun 2005 01:02 GMT
s.magnien@gmail.com coughed up:

...[rip]...

> No, I think, for some reason the whole construct is flawed, like you
> can't load images in constructor(), perhaps you can't load them in
> some less documented places.

Perhaps a quick read through this technical article describing how to chose
among the differing image strategies in java.

http://java.sun.com/developer/technicalArticles/Media/imagestrategies/index.html

...[rip]...

Signature

"Well, ain't this place a geographical oddity!
Two weeks from everywhere!"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.