Reputation: 4635
I simply put the class file and the html file in the same directory. And I call the applet with this:
<p align="center">
<applet code="/ShowImage.class" width="200" height="200">
</applet>
</p>
Obviously this doesn't work. What is the most convenient way to setup local development?
edit:
My applet code:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
/**
*
* @author PCKhoi
*/
public class ShowImage extends Applet {
private BufferedImage img;
public void init() {
try {
URL url = new URL(getCodeBase(), "what_I_think.jpg");
img = ImageIO.read(url);
} catch (IOException e) {
}
}
public void paint(Graphics g){
g.drawImage(img,20,20, null);
}
}
Upvotes: 0
Views: 440
Reputation: 4635
I think I know why it doesn't load now. The applet was wrapped inside a complex netbeans project, that's why putting the class file and the html file inside the same directory didn't work.
My solution is to use a simple IDE such as DrJava if you don't need project functionality.
Upvotes: 0
Reputation: 168825
Some notes:
To compile and run..
prompt> javac ShowImage.java
prompt> appletviewer ShowImage.java
Code (note that the image name will need to be changed back).
//<applet code="ShowImage" width="200" height="200"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URL;
/**
* @author PCKhoi
*/
public class ShowImage extends Applet {
private BufferedImage img;
public void init() {
try {
URL url = new URL(getCodeBase(), "icon.png");
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g){
g.drawImage(img,20,20, null);
}
}
The important line of the source, in relation to your question, is the first, commented line. It supplies an HTML element that the Applet Viewer will parse and use as a pseudo-HTML.
Upvotes: 1
Reputation: 240900
Try this
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET ALIGN="CENTER" CODE="ShowImage.class" WIDTH="800" HEIGHT="500"></APPLET>
</BODY>
</HTML>
and please post your applet code
Upvotes: 1