java – 如何在Intellij IDEA中运行Applet?

前端之家收集整理的这篇文章主要介绍了java – 如何在Intellij IDEA中运行Applet?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为我的网络课程运行 these applets.当我尝试在浏览器中从链接运行这些时,他们什么也没做.所以我决定尝试在IntelliJ中编译它们,但是当我运行代码时它没有做任何事情.没有返回错误消息.我从源代码中更改代码的唯一方法添加main方法删除包声明.以下是我试图运行的Applet:

JAVA代码

  1. ///////////////////////////////////////
  2. //LineSimApllet
  3. //written by David Grangier,Institut Eurecom,France
  4. //david.grangier@eurecom.fr
  5. ///////////////////////////////////////
  6. //imports
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.awt.image.*;
  11. import java.applet.*;
  12. import java.util.*;
  13.  
  14. //Applet Class
  15. public class LineSimApplet extends Applet {
  16. //buttons
  17. Button start = new Button("Start");
  18. Button stop = new Button("Reset");
  19. //features lists
  20. MyChoice length = new MyChoice(new String[]{"10 km","100 km","1000 km"},new double[]{10E3,100E3,1E6},3);
  21. MyChoice rate = new MyChoice(new String[]{"512 kps","1 Mbps","10 Mbps","100 Mbps"},new double[]{512E3,1E6,10E6,100E6},2);
  22. MyChoice size = new MyChoice(new String[]{"100 Bytes","500 Bytes","1 kBytes"},new double[]{8E2,4E3,8E3},1);
  23. //to simulate time
  24. Thread timerThread;
  25. TickTask timerTask;
  26. boolean simulationRunning = false;
  27. //communication line
  28. Line myLine;
  29.  
  30. public void init() {
  31. try {
  32. setBackground(Color.white);
  33. add(new Label("Length",Label.RIGHT));
  34. add(length);
  35. add(new Label("Rate",Label.RIGHT));
  36. add(rate);
  37. add(new Label("Packet size",Label.RIGHT));
  38. add(size);
  39. //start
  40. start.addActionListener(
  41. new ActionListener() {
  42. public void actionPerformed(ActionEvent event) {
  43. launchSim();
  44. }
  45. });
  46. add(start);
  47. //stop
  48. Button stop = new Button("Reset");
  49. stop.addActionListener(
  50. new ActionListener() {
  51. public void actionPerformed(ActionEvent event) {
  52. stopSim();
  53. //clear line
  54. myLine.sendTime(0);
  55. //redraw cleared line
  56. LineSimApplet.this.repaint();
  57. }
  58. });
  59. add(stop);
  60. //line
  61. myLine = new Line(40,50,450,10);
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. public void paint(Graphics g) {
  68. update(g); // eliminate flashing : update is overriden
  69. }
  70.  
  71. public void update(Graphics g) { //work on a offscreen image
  72.  
  73. Dimension offDimension = getSize();
  74. Image offImage = createImage(offDimension.width,offDimension.height);
  75. Graphics offGraphics = offImage.getGraphics();
  76. myLine.drawLine(offGraphics);
  77.  
  78. //sender
  79. offGraphics.setColor(Color.blue);
  80. offGraphics.fillRect(10,40,30,30);
  81. offGraphics.setColor(Color.black);
  82. offGraphics.drawString("Sender",5,90);
  83. offGraphics.drawRect(10,30);
  84.  
  85. //receiver
  86. offGraphics.setColor(Color.blue);
  87. offGraphics.fillRect(490,30);
  88. offGraphics.setColor(Color.black);
  89. offGraphics.drawString("Receiver",485,90);
  90. offGraphics.drawRect(490,30);
  91.  
  92. offGraphics.drawString("Propagation speed : 2.8 x 10^8 m/sec",175,105);
  93. //display offscreen image
  94. g.drawImage(offImage,this);
  95. }
  96.  
  97. private void launchSim() {
  98. setupEnabled(false);
  99. //setup line
  100. myLine.setup(length.getVal(),rate.getVal());
  101. myLine.emitPacket(size.getVal(),0);
  102. //setup timer
  103. timerTask = new TickTask(1E-5,myLine.totalTime());
  104. timerThread = new Thread(timerTask);
  105. //start simulation
  106. simulationRunning = true;
  107. timerThread.start();
  108. }
  109.  
  110. private void stopSim() {
  111. timerTask.endNow();
  112. simulationRunning = false;
  113. setupEnabled(true);
  114. }
  115.  
  116. public void setupEnabled(boolean value) {
  117. start.setEnabled(value);
  118. length.setEnabled(value);
  119. rate.setEnabled(value);
  120. size.setEnabled(value);
  121. }
  122.  
  123. //my choice
  124. class MyChoice extends Choice {
  125. private double vals[];
  126.  
  127. public MyChoice(String items[],double values[],int defaultValue) {
  128. for (int i = 0; i < items.length; i++) {
  129. super.addItem(items[i]);
  130. }
  131. vals = values;
  132. select(defaultValue - 1);
  133. }
  134.  
  135. public double getVal() {
  136. return vals[super.getSelectedIndex()];
  137. }
  138. }
  139.  
  140. //tickTask
  141. class TickTask implements Runnable {
  142. private double counter;
  143. private double length;
  144. private double tick;
  145.  
  146. public TickTask(double t,double l) {
  147. length = l;
  148. tick = t;
  149. counter = 0;
  150. }
  151.  
  152. public void run() {
  153. while (LineSimApplet.this.simulationRunning) {
  154. counter += tick;
  155. LineSimApplet.this.myLine.sendTime(counter);
  156. LineSimApplet.this.repaint();
  157. if (counter >= length) {
  158. LineSimApplet.this.myLine.clearPackets();
  159. LineSimApplet.this.timerThread.suspend();
  160. }
  161. try {
  162. LineSimApplet.this.timerThread.sleep(50);
  163. } catch (Exception e) {
  164. }
  165. }
  166. }
  167.  
  168. public void endNow() {
  169. length = counter;
  170. }
  171. }
  172. }
  173.  
  174. //Line class
  175. class Line {
  176. //graphic variables
  177. private int gX;
  178. private int gY;
  179. private int gWidth;
  180. private int gHeight;
  181. //characteristic variables
  182. final double celerity = 2.8E+8;
  183. private double length;
  184. private double rate;
  185. //simulation variables
  186. private double time;
  187. private Packet myPacket;
  188.  
  189. public Line(int x,int y,int w,int h) {
  190. //graphic init
  191. gX = x;
  192. gY = y;
  193. gWidth = w;
  194. gHeight = h;
  195. }
  196.  
  197. public void setup(double l,double r) {
  198. length = l;
  199. rate = r;
  200. }
  201.  
  202. void sendTime(double now) {
  203. time = now; //update time
  204. removeReceivedPackets(now);
  205. }
  206.  
  207. void emitPacket(double s,double eT) {
  208. myPacket = new Packet(s,eT);
  209. }
  210.  
  211. private void removeReceivedPackets(double now) {
  212. if (!(myPacket == null)) {
  213. if (now > myPacket.emissionTime + (myPacket.size / rate) + length * celerity) {
  214. clearPackets();
  215. }
  216. }
  217. }
  218.  
  219. public void clearPackets() {
  220. myPacket = null;
  221. }
  222.  
  223. public double totalTime() {
  224. double emmissionTime = (myPacket.size / rate);
  225. double onLineTime = (length / celerity);
  226. return (emmissionTime + onLineTime);
  227. }
  228.  
  229. public void drawLine(Graphics g) {
  230. g.setColor(Color.white);
  231. g.fillRect(gX,gY + 1,gWidth,gHeight - 2);
  232. g.setColor(Color.black);
  233. g.drawRect(gX,gY,gHeight);
  234. g.setColor(Color.red);
  235. g.drawString(timeToString(time),gX + gWidth / 2 - 10,gY + gHeight + 15);
  236. drawPackets(g);
  237. }
  238.  
  239. private void drawPackets(Graphics g) {
  240. if (!(myPacket == null)) {
  241. double xfirst;
  242. double xlast;
  243. //compute time units
  244. xfirst = time - myPacket.emissionTime;
  245. xlast = xfirst - (myPacket.size / rate);
  246. //compute position
  247. xfirst = xfirst * celerity * gWidth / length;
  248. xlast = xlast * celerity * gWidth / length;
  249. if (xlast < 0) {
  250. xlast = 0;
  251. }
  252. if (xfirst > gWidth) {
  253. xfirst = gWidth;
  254. }
  255. //draw
  256. g.setColor(Color.red);
  257. g.fillRect(gX + (int) (xlast),(int) (xfirst - xlast),gHeight - 2);
  258. }
  259. }
  260.  
  261. static private String timeToString(double now) {
  262. String res = Double.toString(now * 1000);
  263. int dot = res.indexOf('.');
  264. String deci = res.substring(dot + 1) + "000";
  265. deci = deci.substring(0,3);
  266. String inte = res.substring(0,dot);
  267. return inte + "." + deci + " ms";
  268. }
  269.  
  270. public static void main(String[] args) {
  271. LineSimApplet ls = new LineSimApplet();
  272. ls.init();
  273. }
  274. }
  275.  
  276. class Packet {
  277. double size;
  278. double emissionTime;
  279.  
  280. Packet(double s,double eT) {
  281. size = s;
  282. emissionTime = eT;
  283. }
  284. }

如何使用IntelliJ运行此applet?

解决方法

在IntelliJ中运行Applet的最简单方法creatingApplet Run/Debug Configuration.只需单击显示要执行的主类名称的小下拉列表,单击“编辑配置…”,然后单击绿色按钮,然后选择“Applet”并将LineSimApplet指定为目标Applet类.

你的主要方法方法不起作用的原因是因为Applets are not meant to be run as standalone applications,但是如果你想以这种方式运行它,最简单的方法add it to a JFrame.

猜你在找的IDEA相关文章