Solved

Displaying an Image

Posted on 1998-06-17
5
160 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
Java DateChooser? 3 36
varialbe initialization 11 33
servlet example issue 6 40
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

778 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