Add an image to java applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Add an image to java applet

  1. #1
    Join Date
    May 2005
    Posts
    14

    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.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    How to boil an egg

    This as basic as it gets, (no swing)

    The source is attatched, see it here:

    http://home.no.net/sjalle/ImageApplet.html
    Attached Files Attached Files
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Posts
    14

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

  4. #4
    Join Date
    May 2005
    Posts
    14

    Oh, one more thing.

    What does line one do?
    -------------------
    package stubs6;
    -------------------

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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:

    Code:
    <applet
      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">
    To set this up as is you must upload the ImageApplet.html to a folder on your server,
    e.g "applets".
    Then you must create a subfolder of this names "stubs6", and a folder: "images"
    under the stubs6 folder, and upload files like:
    Code:
    applets
         ImageApplet.html
         stubs6
             ImageApplet.class 
             images
                  RiskBoard800x505.jpg
    on unix you may have to set the mode for the class file to "rwe"
    Last edited by sjalle; 05-16-2005 at 11:14 PM.
    eschew obfuscation

  6. #6
    Join Date
    May 2005
    Posts
    14

    Thanks

    alright. thanks for your help.

  7. #7
    Join Date
    May 2005
    Posts
    2
    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)...

    g.drawImage(RiskBoard,200,200,null);
    // 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.

  8. #8
    Join Date
    May 2005
    Posts
    14

    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.

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center