Jump to content

Java programming question


Korla

Recommended Posts

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

korla3eaqd3.jpg
Link to comment
Share on other sites

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

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?

korla3eaqd3.jpg
Link to comment
Share on other sites

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:

korla3eaqd3.jpg
Link to comment
Share on other sites

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...");}

korla3eaqd3.jpg
Link to comment
Share on other sites

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

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

korla3eaqd3.jpg
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.