将类文件转换为applet

时间:2013-11-26 08:20:43

标签: java applet

我有一个运行完美的java类但现在我需要将这个类作为Web应用程序运行,所以我需要将这个类转换为applet如何将这个类转换为applet。 我对applet的生命周期知之甚少 的init()

开始()

漆()

停止()

破坏()

并运行applet 小程序代码=“LifeTest.class”

所以任何人都可以帮助我将这个类转换为applet,如果不可能那么任何建议作为替代

import java.io.ByteArrayInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;

class Server {

AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 8000;



public static void main(String args[]) throws Exception {


DatagramSocket serverSocket = new DatagramSocket(50005);

/**
 * Formula for lag = (byte_size/sample_rate)*2
 * Byte size 9728 will produce ~ 0.45 seconds of lag. Voice slightly broken.
 * Byte size 1400 will produce ~ 0.06 seconds of lag. Voice extremely broken.
 * Byte size 4000 will produce ~ 0.18 seconds of lag. Voice slightly more broken then 9728.
 */

byte[] receiveData = new byte[5000];

format = new AudioFormat(sampleRate, 16, 1, true, false);

while (status == true) {
    DatagramPacket receivePacket = new DatagramPacket(receiveData,
            receiveData.length);

    serverSocket.receive(receivePacket);

    ByteArrayInputStream baiss = new ByteArrayInputStream(
            receivePacket.getData());

    ais = new AudioInputStream(baiss, format, receivePacket.getLength());
    toSpeaker(receivePacket.getData());

}



}

public static void toSpeaker(byte soundbytes[]) {
try {

    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
    SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

    sourceDataLine.open(format);

    FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
    volumeControl.setValue(6.0206f);

    sourceDataLine.start();
    sourceDataLine.open(format);

    sourceDataLine.start();

    System.out.println("format? :" + sourceDataLine.getFormat());

    sourceDataLine.write(soundbytes, 0, soundbytes.length);
    System.out.println(soundbytes.toString());
    sourceDataLine.drain();
    sourceDataLine.close();
} catch (Exception e) {
    System.out.println("Not working in speakers...");
    e.printStackTrace();
}
}
}

1 个答案:

答案 0 :(得分:1)

您需要延长Applet。让我给你一些示例代码。

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

使用某些文本编辑器创建MANIFEST.MF文件。将其放在.java文件所在的同一目录中。它的内容应该是。

Manifest-Version: 1.0
Permissions: all-permissions
Application-Name: Name of your application

现在您需要编译代码并需要在其中附加MANIFEST.MF文件。

javac HelloWorld.java
jar cvfm MANIFEST.MF HelloWorld.jar *.class

现在创建一个.html文件并在其中加载<applet>标记。

<applet name="HelloWorld" code="HelloWorld.class"
        archive="HelloWorld.jar" width="100" height="100">          
</applet>