gpt4 book ai didi

java - 将类文件转换为小程序

转载 作者:行者123 更新时间:2023-12-01 13:47:54 27 4
gpt4 key购买 nike

我有一个运行完美的 java 类,但现在我需要将此类作为 Web 应用程序运行,因此我需要将此类转换为小程序,如何将此类转换为小程序。我对小程序知之甚少,比如它的生命周期初始化()

开始()

绘制()

停止()

销毁()

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

所以任何人都可以帮助我将此类转换为小程序,如果不可能,那么任何建议作为替代品

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

最佳答案

您需要扩展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>

关于java - 将类文件转换为小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com