Solved

Displaying an Image

Posted on 1998-06-17
5
165 Views
Last Modified: 2010-05-18
The title says it all. How do I display an image in J++1.1, in an applet
0
Comment
Question by:MikeP090797
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1223318
In an applet an image can be acquired with one of two methods:

public Image getImage(URL url);   or
public Image getImage(URL url,String name);

both of these are in the Applet class.
Displaying the image occurs in the paint routine. You can use a method from the Graphics class; normally:

g.drawImage(Image img,int x,int y,ImageObserver obs);

The last argument is the registration of a party interested in the progress of the image loading. In your case you will very likely wish to fill in: "this" for the last argument, i.e. the Applet. That will allow "progressive" updates to the screen as the image is loaded (which doesn't occur until you execute the drawImage method).

0
 
LVL 8

Author Comment

by:MikeP090797
ID: 1223319
Ok, I've tried this, an all it shows is some dots in the left top corner:

      public void paint(Graphics g)
      {
            try
            {
                  Image img = getImage(new URL("c:\\niggaz.jpg"));
                  g.drawImage(img, 10, 10, 50, 50, Color.blue, this);
            }

            catch(MalformedURLException m)
            {
                  System.out.println("URL was not found");
            }
      }



0
 
LVL 16

Expert Comment

by:imladris
ID: 1223320
Have you checked your console? I tried this just to be triple sure; a filename is not a valid URL. The MalformedURLException will get thrown. An applet is not allowed, in general, to read files. You can do it, for certain purposes (like testing). The rule is that it can only access the server from which it was loaded. If you loaded it locally you can access files. In that case, to load an Image, you would use the AWT toolkit:

Image img=Toolkit.getDefaultToolkit().getImage(String filename);

However, this won't, of course, work if the applet is loaded from a (remote) webserver into a browser.


Also, as a side note, it is not a good idea to do the getImage in the paint routine. This would cause the image to be reloaded every time paint was called (even though it didn't change). (paint can get called, for instance, because the applet was hidden, and is then brought back to the foreground. Restoring the display is done through a call to paint).


0
 
LVL 16

Expert Comment

by:imladris
ID: 1223321
P.S. having the getImage in the paint method, might even cause a loop, since the drawImage references "this" (for progressive painting). Each progressive update, could now also cause another getImage. I'm not dead certain of the consequences of doing a getImage on an Image you already have. Suffice it to say it is not desirable.

0
 
LVL 8

Author Comment

by:MikeP090797
ID: 1223322
Thanks, I'm sorry it took me so long to respond
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

830 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