CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Java Applet with Image Help

    I have created an applet that, at this point, only displays an image. Running it in the AppletViewer I get the desired results, the applet opens at the correct resolution and displays the image without flaw. However, when I create an HTML file that loads the applet, it simply shows an error in the webpage that opens. How can I get the image to open in the HTML file? (Eventually I would like to have this applet embedded on a website, so if they are different please share with me how to do both) (The image is 800x600 and so is the window)

    HTML File Contents:

    <applet code="WebDemoMain.java" width="800" height="600">
    </applet>

    Applet Code:
    <code>
    package webDemo;
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;

    import javax.swing.*;

    @SuppressWarnings("serial")
    public class WebDemoMain extends Applet{

    Image background;
    MediaTracker mediaTracker;
    public WebDemoMain(){
    }

    public void paint(Graphics g){
    setSize(800, 600);
    mediaTracker = new MediaTracker(this);
    background = getImage(getCodeBase(), "BlankDesktop.png");
    mediaTracker.addImage(background, 0);
    g.drawImage(background, 0, 0, this);
    }
    }
    </code>

    The code is of little concern as of how simple it is. I suppose my summed up question is how to load images into applets using an html file or when it is on a webpage? (I have also tried setting the image name as a parameter in the HTML file and using the: getParameter(x) method, to no avail) I appreciate the help!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Applet with Image Help

    Your applet is in a package so you need to specify this as part of the code name. You may also need to use the codebase tag if the html file isn't in the same directory as the class file. ie

    Code:
    <applet code="webDemo.WebDemoMain.java" width="800" height="600">
    BTW code tags have square brackets
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java Applet with Image Help

    Also The .java extension in the code= tag looks wrong.
    Norm

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Applet with Image Help

    Quote Originally Posted by Norm
    Also The .java extension in the code= tag looks wrong.
    Yes, definitely. Oops didn't spot that.

    The html tag should be:
    Code:
    <applet code="webDemo.WebDemoMain.class" width="800" height="600">
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured