[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Displaying an Image

Posted on 1998-06-17
5
Medium Priority
?
185 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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