|
-
September 30th, 2011, 09:45 PM
#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!
-
October 1st, 2011, 05:27 AM
#2
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
-
October 1st, 2011, 08:07 AM
#3
Re: Java Applet with Image Help
Also The .java extension in the code= tag looks wrong.
Norm
-
October 1st, 2011, 08:30 AM
#4
Re: Java Applet with Image Help
 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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|