• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Displaying an image from a url

Can you tell me  how to get an image to appear when a user clicks on a button.
On the button click the program reads in a url from a textfield and then should display the appropriate image within the applet - how do I get this to work:

Here is the code, repaint does not repaint the applet and the image does not appear:

import java.awt.*;
import java.applet.*;
import java.net.*;

public class urlimageviewer extends Applet
 private Image image;
 private URL urladd;
 private Button loadImage;
 private TextField urlText;
 private String urlTextString;
    public void init()
        loadImage = new Button("Get Image");
        urlText = new TextField(30);
        setLayout(new FlowLayout());
    }//end of init method

    public void paint(Graphics g)
    boolean b = g.drawImage(image, 20,20,100,100, this);
    }//end of method paint

    public boolean action(Event event, Object arg)
    if (event.target == loadImage)
            urlTextString = urlText.getText();
            urladd = new URL(urlTextString);
            image = getImage(urladd);
            catch(java.lang.Exception ex)
        }//end of if statement
    return true;
    }//end of method action
}//end of class urlImageViewer

Please give me concrete code examples explained in detail, so I don't have to write back for more explanation

Thanks very much for your help

1 Solution
I have uploaded an applet with the source code to my homepage


In my example you can only loade one picture, but you just have to add the URL and TextField code from your own code.

Hope this is what you need.

PS. I used JDK 1.2 beta 3
First of all, your applet works fine in my appletviewer 1.1.5
and MSIE3 browser. But not in NSNav 4.05...
Two caveats: painting the image, while it could still be
undefined, especially since you don't use the mediatracker.
More important: security. In usk's example, you can see that he
loads images that have the same codebase.
But it seems like your applet doesn't even load applets from the
same codebase. I have some ideas, but it really is beyond my
short-term expertise:(
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now