Java 如何在Intellij IDEA中运行小程序?
我想参加我的网络课程。当我试图在浏览器中从链接运行这些时,它们什么也没做。所以我决定尝试用IntelliJ编译它们,但当我运行代码时,它什么也没做。没有返回错误消息。我从源代码中更改代码所做的唯一一件事就是添加main方法并删除包声明。以下是我尝试运行的小程序: JAVA代码Java 如何在Intellij IDEA中运行小程序?,java,intellij-idea,applet,Java,Intellij Idea,Applet,我想参加我的网络课程。当我试图在浏览器中从链接运行这些时,它们什么也没做。所以我决定尝试用IntelliJ编译它们,但当我运行代码时,它什么也没做。没有返回错误消息。我从源代码中更改代码所做的唯一一件事就是添加main方法并删除包声明。以下是我尝试运行的小程序: JAVA代码 /////////////////////////////////////// //Linesimaplet //大卫·格兰杰(David Grangier),法国欧雷科姆研究所 //大卫。grangier@eurecom
///////////////////////////////////////
//Linesimaplet
//大卫·格兰杰(David Grangier),法国欧雷科姆研究所
//大卫。grangier@eurecom.fr
///////////////////////////////////////
//进口
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.image.*;
导入java.applet.*;
导入java.util.*;
//小程序类
公共类LineSimApplet扩展小程序{
//钮扣
按钮启动=新按钮(“启动”);
按钮停止=新按钮(“重置”);
//功能列表
MyChoice长度=新的MyChoice(新字符串[]{“10公里”,“100公里”,“1000公里”},新双[]{10E3,100E3,1E6},3);
MyChoice速率=新的MyChoice(新字符串[]{“512 kps”、“1 Mbps”、“10 Mbps”、“100 Mbps”},新双[]{512E3、1E6、10E6、100E6},2);
MyChoice size=newmychoice(新字符串[]{“100字节”、“500字节”、“1kbytes”},新双精度[]{8E2、4E3、8E3},1);
//模拟时间
线程时间读取;
任务时间任务;
布尔模拟运行=false;
//通信线路
线糜棱线;
公共void init(){
试一试{
挫折地面(颜色:白色);
添加(新标签(“长度”,标签右侧));
加上(长度);
添加(新标签(“费率”,标签右侧));
增加(费率);
添加(新标签(“数据包大小”,Label.RIGHT));
添加(大小);
//开始
start.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件){
launchSim();
}
});
添加(开始);
//停止
按钮停止=新按钮(“重置”);
stop.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件){
stopSim();
//清晰的线条
myLine.sendTime(0);
//重绘清除线
LineSimApplet.this.repaint();
}
});
添加(停止);
//线
myLine=新线(40,50,450,10);
}捕获(例外e){
e、 printStackTrace();
}
}
公共空间涂料(图g){
更新(g);//消除闪烁:更新被覆盖
}
公共无效更新(图形g){//在屏幕外图像上工作
维度offDimension=getSize();
Image of Image=createImage(offDimension.width、offDimension.height);
Graphics offGraphics=offImage.getGraphics();
myLine.抽绳(非图形);
//寄件人
offGraphics.setColor(颜色:蓝色);
offGraphics.fillRect(10,40,30,30);
offGraphics.setColor(颜色:黑色);
抽绳(发送者),5,90);
offGraphics.drawRect(10,40,30,30);
//接受者
offGraphics.setColor(颜色:蓝色);
offGraphics.fillRect(490,40,30,30);
offGraphics.setColor(颜色:黑色);
抽绳(“接收器”,485,90);
offGraphics.drawRect(490,40,30,30);
拉丝(“传播速度:2.8x10^8米/秒”,175105);
//显示屏幕外图像
g、 drawImage(offImage,0,0,this);
}
私有void launchSim(){
setupEnabled(false);
//安装线
myLine.setup(length.getVal(),rate.getVal());
myLine.emitPacket(size.getVal(),0);
//设置计时器
timerTask=新任务(1E-5,myLine.totalTime());
timerThread=新线程(timerTask);
//开始模拟
模拟运行=真;
timerThread.start();
}
私有void stopSim(){
timerTask.endNow();
模拟运行=错误;
setupEnabled(true);
}
已启用公共无效设置(布尔值){
start.setEnabled(值);
长度.setEnabled(值);
rate.setEnabled(值);
size.setEnabled(值);
}
//我的选择
类MyChoice扩展了Choice{
私人双倍增值税[];
公共MyChoice(字符串项[],双值[],int-defaultValue){
对于(int i=0;i=长度){
LineSimApplet.this.myLine.clearPackets();
LineSimApplet.this.timerThread.suspend();
}
试一试{
LineSimApplet.this.timerRead.sleep(50);
}捕获(例外e){
}
}
}
公共无效endNow(){
长度=计数器;
}
}
}
//直线类
班级线{
//图形变量
私人int gX;
私营企业;
内特格威兹私人酒店;
普里夫
///////////////////////////////////////
//LineSimApllet
//written by David Grangier, Institut Eurecom, France
//david.grangier@eurecom.fr
///////////////////////////////////////
//imports
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import java.util.*;
//Applet Class
public class LineSimApplet extends Applet {
//buttons
Button start = new Button("Start");
Button stop = new Button("Reset");
//features lists
MyChoice length = new MyChoice(new String[]{"10 km", "100 km", "1000 km"}, new double[]{10E3, 100E3, 1E6}, 3);
MyChoice rate = new MyChoice(new String[]{"512 kps", "1 Mbps", "10 Mbps", "100 Mbps"}, new double[]{512E3, 1E6, 10E6, 100E6}, 2);
MyChoice size = new MyChoice(new String[]{"100 Bytes", "500 Bytes", "1 kBytes"}, new double[]{8E2, 4E3, 8E3}, 1);
//to simulate time
Thread timerThread;
TickTask timerTask;
boolean simulationRunning = false;
//communication line
Line myLine;
public void init() {
try {
setBackground(Color.white);
add(new Label("Length", Label.RIGHT));
add(length);
add(new Label("Rate", Label.RIGHT));
add(rate);
add(new Label("Packet size", Label.RIGHT));
add(size);
//start
start.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
launchSim();
}
});
add(start);
//stop
Button stop = new Button("Reset");
stop.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
stopSim();
//clear line
myLine.sendTime(0);
//redraw cleared line
LineSimApplet.this.repaint();
}
});
add(stop);
//line
myLine = new Line(40, 50, 450, 10);
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
update(g); // eliminate flashing : update is overriden
}
public void update(Graphics g) { //work on a offscreen image
Dimension offDimension = getSize();
Image offImage = createImage(offDimension.width, offDimension.height);
Graphics offGraphics = offImage.getGraphics();
myLine.drawLine(offGraphics);
//sender
offGraphics.setColor(Color.blue);
offGraphics.fillRect(10, 40, 30, 30);
offGraphics.setColor(Color.black);
offGraphics.drawString("Sender", 5, 90);
offGraphics.drawRect(10, 40, 30, 30);
//receiver
offGraphics.setColor(Color.blue);
offGraphics.fillRect(490, 40, 30, 30);
offGraphics.setColor(Color.black);
offGraphics.drawString("Receiver", 485, 90);
offGraphics.drawRect(490, 40, 30, 30);
offGraphics.drawString("Propagation speed : 2.8 x 10^8 m/sec", 175, 105);
//display offscreen image
g.drawImage(offImage, 0, 0, this);
}
private void launchSim() {
setupEnabled(false);
//setup line
myLine.setup(length.getVal(), rate.getVal());
myLine.emitPacket(size.getVal(), 0);
//setup timer
timerTask = new TickTask(1E-5, myLine.totalTime());
timerThread = new Thread(timerTask);
//start simulation
simulationRunning = true;
timerThread.start();
}
private void stopSim() {
timerTask.endNow();
simulationRunning = false;
setupEnabled(true);
}
public void setupEnabled(boolean value) {
start.setEnabled(value);
length.setEnabled(value);
rate.setEnabled(value);
size.setEnabled(value);
}
//my choice
class MyChoice extends Choice {
private double vals[];
public MyChoice(String items[], double values[], int defaultValue) {
for (int i = 0; i < items.length; i++) {
super.addItem(items[i]);
}
vals = values;
select(defaultValue - 1);
}
public double getVal() {
return vals[super.getSelectedIndex()];
}
}
//tickTask
class TickTask implements Runnable {
private double counter;
private double length;
private double tick;
public TickTask(double t, double l) {
length = l;
tick = t;
counter = 0;
}
public void run() {
while (LineSimApplet.this.simulationRunning) {
counter += tick;
LineSimApplet.this.myLine.sendTime(counter);
LineSimApplet.this.repaint();
if (counter >= length) {
LineSimApplet.this.myLine.clearPackets();
LineSimApplet.this.timerThread.suspend();
}
try {
LineSimApplet.this.timerThread.sleep(50);
} catch (Exception e) {
}
}
}
public void endNow() {
length = counter;
}
}
}
//Line class
class Line {
//graphic variables
private int gX;
private int gY;
private int gWidth;
private int gHeight;
//characteristic variables
final double celerity = 2.8E+8;
private double length;
private double rate;
//simulation variables
private double time;
private Packet myPacket;
public Line(int x, int y, int w, int h) {
//graphic init
gX = x;
gY = y;
gWidth = w;
gHeight = h;
}
public void setup(double l, double r) {
length = l;
rate = r;
}
void sendTime(double now) {
time = now; //update time
removeReceivedPackets(now);
}
void emitPacket(double s, double eT) {
myPacket = new Packet(s, eT);
}
private void removeReceivedPackets(double now) {
if (!(myPacket == null)) {
if (now > myPacket.emissionTime + (myPacket.size / rate) + length * celerity) {
clearPackets();
}
}
}
public void clearPackets() {
myPacket = null;
}
public double totalTime() {
double emmissionTime = (myPacket.size / rate);
double onLineTime = (length / celerity);
return (emmissionTime + onLineTime);
}
public void drawLine(Graphics g) {
g.setColor(Color.white);
g.fillRect(gX, gY + 1, gWidth, gHeight - 2);
g.setColor(Color.black);
g.drawRect(gX, gY, gWidth, gHeight);
g.setColor(Color.red);
g.drawString(timeToString(time), gX + gWidth / 2 - 10, gY + gHeight + 15);
drawPackets(g);
}
private void drawPackets(Graphics g) {
if (!(myPacket == null)) {
double xfirst;
double xlast;
//compute time units
xfirst = time - myPacket.emissionTime;
xlast = xfirst - (myPacket.size / rate);
//compute position
xfirst = xfirst * celerity * gWidth / length;
xlast = xlast * celerity * gWidth / length;
if (xlast < 0) {
xlast = 0;
}
if (xfirst > gWidth) {
xfirst = gWidth;
}
//draw
g.setColor(Color.red);
g.fillRect(gX + (int) (xlast), gY + 1, (int) (xfirst - xlast), gHeight - 2);
}
}
static private String timeToString(double now) {
String res = Double.toString(now * 1000);
int dot = res.indexOf('.');
String deci = res.substring(dot + 1) + "000";
deci = deci.substring(0, 3);
String inte = res.substring(0, dot);
return inte + "." + deci + " ms";
}
public static void main(String[] args) {
LineSimApplet ls = new LineSimApplet();
ls.init();
}
}
class Packet {
double size;
double emissionTime;
Packet(double s, double eT) {
size = s;
emissionTime = eT;
}
}