Korla Posted May 23, 2005 Share Posted May 23, 2005 I'm trying to get an image loaded into an applet from a URL...any image will do for now, but I just can't get it to work. I think I'm supposed to use this: public Image getImage(URL url, String name) { try { return getImage(new URL(url, name); } catch (MalformedURLException e) { return null; } } but how? If you could show me an example of an applet which loads an image, it'd be awesome. thanks Link to comment Share on other sites More sharing options...
____ Posted May 23, 2005 Share Posted May 23, 2005 Where' the image? If it's in the same dir as the applet you can just use: Image myImg = getImage(getCodeBase(), "my_img.gif"); (you can also use getDocumentBase() if the img is in the same dir as the html displaying the applet). Otherwise you have to use: Image myImg = getImage(new URL("http://www.my_url.com/my_img.gif")); Link to comment Share on other sites More sharing options...
Korla Posted May 23, 2005 Author Share Posted May 23, 2005 hey rick, knew I could trust you :) I am not making the applet for the same webpage the images come from or anything like that, I need them to be taken from an anohter place on the web, so the second one. what packages do I need? java.net, anything else? Link to comment Share on other sites More sharing options...
Korla Posted May 23, 2005 Author Share Posted May 23, 2005 import java.awt.*; import java.applet.*; import java.net.*; public class Face extends Applet { Image image; public void init() { image = getImage(new URL("http://www1.cs-manager.com/data/face.php?id=132236301510113")); } public void paint(Graphics g) { g.drawImage(image, 0, 0, this); } } gets me this error: Unreported exception java.net.MalformedURLException; must be caught or declared to be thrown I use BlueJ btw. What am I doing wrong? :oops: Link to comment Share on other sites More sharing options...
Korla Posted May 23, 2005 Author Share Posted May 23, 2005 well, fixed that error: import java.awt.*; import java.applet.*; import java.net.MalformedURLException; import java.net.URL; public class Face extends Applet { Image image; public void init() { try{ image = getImage(new URL("http://www1.cs-manager.com/data/face.php?id=141315101550003")); }catch(MalformedURLException e){System.out.println("Bad link...");} } public void paint(Graphics g) { g.drawImage(image, 0, 0, this); } } but now I get another error: exception: java.security.AccessControlException: access denied (java.net.SocketPermission www1.cs-manager.com resolve). if anyone's interested, I fixed it, using: try{ image = Toolkit.getDefaultToolkit().getImage(new URL("http://www1.cs-manager.com/data/face.php?id=" + gender + skinc + eyet + eyec + eyebt + eyebc + hairt + hairc + glasst + glassc + beardt + beardc + mouth)); }catch(MalformedURLException e){System.out.println("Ful lÃÆÃâÃâänk...");} Link to comment Share on other sites More sharing options...
____ Posted May 23, 2005 Share Posted May 23, 2005 Looks like you have to open a connection to the cs-manager.com server before your applet will be able to access it. Why not just get the images you want and dump them on the same server as the applet? Link to comment Share on other sites More sharing options...
Korla Posted May 24, 2005 Author Share Posted May 24, 2005 because there are zillions of them :) you see, there are 15 digits at the end, many of which have 5 possible values, it makes for a heck of alot of images, I tell you :D anyway, I fixed that by changing it from an applet to a javax.swing program instead, odesn't matter to me, it's just for school anyway, and this way it looks so much better :P Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now