Solved

Displaying an Image

Posted on 1998-06-17
5
152 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, I'm sorry it took me so long to respond
0

Featured Post

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

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

762 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

7 Experts available now in Live!

Get 1:1 Help Now