views:

20

answers:

3

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);
    }
}
+1  A: 

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

org.life.java
ehm, that doesn't fix it. Seem it can't find the class: "ClassNotFoundException: .ShowImage.class"
Khoi
@khoi does it has any package structure, can you post your DIR hierarchy
org.life.java
@org.life.java thanks a lot! You helped me figure out the solution.
Khoi
@khoi you are welcomed, you can mark this as answer if it is your answer really.
org.life.java
+1  A: 

Some notes:

  • The code as published (at this instant) does not compile
  • Do not swallow exceptions in broken code

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.

Andrew Thompson
A: 

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.

Khoi
I use TextPad as my editor for source files. It can compile or run from a few key strokes, and I have added a new keystroke to launch build files. For many small projects, there are more hassles than advantages to be had from a powerful IDE such as Netbeans.
Andrew Thompson