Add an image to java applet
ok, i have no idea how to add an image to the java applet. I can do it using objectdraw, but my teacher wont let me use objectdraw, so i need to know how to do it with something else. Can someone give me the basic code to add an image to the applet? Ive searched for the code everywhere on google, ask geeves and other tutorials and even other forums. Noone seems to know how.
How to boil an egg
This as basic as it gets, (no swing)
The source is attatched, see it here:
Still having problems
ok, so i created a new project (a basica java applet) and called it ImageApplet, and then i coppied the exact ImageApplet.java file that you gave me and then i pasted it into the new ImageApplet that i created and replaced the line ------------------------LINE 25-------------------
String imagePath = this.getParameter("Image_path");
String imagePath = this.getParameter("RiskBoard800x505.jpg");
RiskBoard800x505.jpg is the picture that i want to show up, and i have it stored in the class folder of my projeect. Is this the only line of code that i have to change? If not what else do i have to change and if it is the only line i have to change then why do i get some text when i load the applet saying "Image load failed: null"?
Thanks for the reply, i really appreciate it because, so far you are the only source i have on this information since i couldnt find it anywhere else when i searched.
Oh, one more thing.
What does line one do?
Line one defined the package; when you compile this source file the class file will
be saved in a folder named stubs6, a subfolder of the source file's folder.
You should not change anything in the code, only the html. The code:
String imagePath = this.getParameter("image_path");
gets the value of the parameter named "image_path", - check the html and set
the value of this parameter to your file name:
To set this up as is you must upload the ImageApplet.html to a folder on your server,
codebase = "."
code = "stubs6.ImageApplet.class"
name = "TestApplet"
width = "800"
height = "505"
hspace = "0"
vspace = "0"
align = "middle"
<param name = "image_path" value = "stubs6/images/RiskBoard800x505">
Then you must create a subfolder of this names "stubs6", and a folder: "images"
under the stubs6 folder, and upload files like:
on unix you may have to set the mode for the class file to "rwe"
Last edited by sjalle; 05-17-2005 at 12:14 AM.
alright. thanks for your help.
Doesn't java come with an 'Image' class? I simply created my own image and then filled it with my image:
Image RiskBoard = getImage(getDocumentBase(),"RiskBoard800x505.jpg");
...now to draw it to the graphics object (using object graphics g)...
// 200 and 200 are the x and y coordinates
If this is far too simple for what you're doing, my apologies in advance. If its far too advanced, the same. To be honest I've never seen what you're trying to do.
No, thats actually perfect.
Thanks MikeKomo. Thats perfect. Thats exactly what i wanted. Something really simple. Thank you very much. But now, how do i make this image a button? I need something to happen when the image is clicked. So im assuming that i need to add an action listener to it? But i dont know how to do that..so if u have some free time i would appriciate the extra help. Thanks again.
You must implement the mouselistener interface for the component that you display
your image in, leave all thos methods empty except the mousePressed or the meouseClicked where you dor your stuff.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center