Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Displaying an Image

Posted on 1998-06-17
5
Medium Priority
?
180 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
[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
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

609 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