简体   繁体   中英

how to deploy a java applet for local development

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);
    }
}

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM