gpt4 book ai didi

Java Applet 在浏览器中加载时卡住

转载 作者:行者123 更新时间:2023-12-01 15:52:51 26 4
gpt4 key购买 nike

我正在尝试在网页上加载小程序,但它无法正确加载。这是该页面的小程序标签:

<applet codebase="classes" code="Simulation.class" width="600" height="600"></applet>

我已经能够毫无问题地在网站上加载其他小程序,但是这个小程序要么什么也不做,要么无法通过加载屏幕(请参阅您自己 here )。小程序需要其他类,但它们仍然都在“classes”目录下,所以我认为这不是问题。以下是Simulation.java 文件的代码:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class Simulation
extends Applet
implements Runnable, MouseListener, MouseMotionListener, KeyListener{

private static final long serialVersionUID = -1914559969027349635L;

private boolean threadSuspended;
private Thread t = null;
private int startX;
private int startY;
private int endX;
private int endY;

private Environment env;
private int numAgents = -1;
private int width;
private int height;
private int stepTime;
private int currentParam;
private boolean start;
private boolean instructions;

private final double START_RANGE = 70.0;
private final double START_BUFFER = 5.0;
private int speed;
private final int[] SPEEDS = {4, 5, 6, 7, 8,
10, 12, 15, 18, 22,
26, 30, 35, 40, 50,
65, 80, 100, 150, 300};

public void init() {
start = true;
currentParam = 1;
initParams();
env = new Environment(width, height, numAgents, START_RANGE, START_BUFFER);
env.init();
resize(width, height);
setBackground(Color.WHITE);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
repaint();
}

public void restart() {
stop();
start = true;
double tempVD = env.getVelocityDiscount();
double tempHD = env.getHeadingDiscount();
env = new Environment(width, height, numAgents, env.getRange(), env.getBuffer());
env.init();
env.setVelocityDiscount(tempVD);
env.setHeadingDiscount(tempHD);
repaint();
}

private void initParams() {
start = true;
width = 600;
height = 600;
stepTime = 15;
speed = 8;
getNumAgents();
}

private void getNumAgents() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
if (numAgents < 1) {
System.out.print("Number of agents: ");
numAgents = Integer.parseInt(reader.readLine());
}
}
catch (Exception e) {
System.err.println("Error reading input. Please enter only positive integers.");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
System.err.println("Thread interrupted.");
System.exit(1);
}
getNumAgents();
}
if (numAgents < 1) {
System.err.println("Error reading input. Please enter only positive integers.");
getNumAgents();
}
}

public void start(){
if (t == null){
t = new Thread(this);
threadSuspended = false;
t.start();
}
else{
if(threadSuspended){
threadSuspended = false;
synchronized(this){
notify();
}
}
}
}

public void stop(){
threadSuspended = true;
}

public void run(){
numAgents = env.getAgents().size();
width = env.getWidth();
height = env.getHeight();
try {
stop();
while (true) {
if (threadSuspended){
synchronized(this){
while (threadSuspended){
wait();
}
}
}
if (this.getWidth() != env.getWidth() || this.getHeight() != env.getHeight()) {
env.setWidth(this.getWidth());
env.setHeight(this.getHeight());
}
env.cycle();
repaint();
for (int i = 0; i < stepTime / 5; i++) {
Thread.sleep(5);
}
}
}
catch (InterruptedException e) {
System.err.println("Runtime Error");
e.printStackTrace();
}
}

public void paint(Graphics g){
g.setFont(new Font("SansSerif", Font.PLAIN, 15));
ArrayList<Agent> agents = env.getAgents();
for (Agent i : agents) {
if (i.isSelected()) {
g.setColor(Color.RED);
g.fillOval((int) i.getX() - 2, (int) i.getY() - 2, 5, 5);
}
else {
switch (i.getGroup()) {
case 0:
g.setColor(Color.BLACK);
break;
case 1:
g.setColor(Color.BLUE);
break;
case 2:
g.setColor(Color.YELLOW);
break;
case 3:
g.setColor(Color.RED);
break;
case 4:
g.setColor(Color.GREEN);
break;
case 5:
g.setColor(Color.ORANGE);
break;
case 6:
g.setColor(Color.CYAN);
break;
case 7:
g.setColor(Color.PINK);
break;
case 8:
g.setColor(Color.MAGENTA);
break;
case 9:
g.setColor(Color.DARK_GRAY);
break;
}
g.drawOval((int) i.getX() - 1, (int) i.getY() - 1, 3, 3);
}
}
if (startX != -1 && startY != -1 && endX != -1 && endY != -1) {
g.setColor(Color.LIGHT_GRAY);
Rectangle r = getDraggedRect();
g.drawRect((int) r.getMinX(), (int) r.getMinY(), r.width, r.height);
}
g.setColor(Color.LIGHT_GRAY);
switch (currentParam) {
case 1:
g.drawString("Speed: " + (SPEEDS.length - speed), 2, 12);
break;
case 2:
g.drawString("Sensing range: " + env.getRange(), 2, 12);
break;
case 3:
g.drawString("Velocity discount: " + env.getVelocityDiscount(), 2, 12);
break;
case 4:
g.drawString("Heading discount: " + env.getHeadingDiscount(), 2, 12);
break;
case 5:
g.drawString("Buffer: " + env.getBuffer(), 2, 12);
break;
}
if (start || instructions) {
drawInstructions(g);
}
}

private void drawInstructions(Graphics g) {
Color c = new Color(50, 50, 50, 200);
g.setColor(c);
int startX = (width / 2) - 152;
int startY = (width / 2) - 152;
g.fill3DRect(startX, startY, 304, 304, true);
g.setColor(Color.WHITE);
g.setFont(new Font("SansSerif", Font.PLAIN, 10));
g.drawString("Please set parameters before starting (parameters can be", startX + 3, startY + 13);
g.drawString("altered at any time - current selected parameter is in the", startX + 3, startY + 26);
g.drawString("upper left corner at all times):", startX + 3, startY + 39);
g.drawString("Press 'S' to change simulation speed", startX + 3, startY + 52);
g.drawString("Press 'R' to change sensing range", startX + 3, startY + 65);
g.drawString("Press 'V' to change velocity discount", startX + 3, startY + 78);
g.drawString("Press 'H' to change header discount", startX + 3, startY + 91);
g.drawString("Press 'B' to change buffer size", startX + 3, startY + 104);
g.drawString("Use the UP and DOWN arrow keys to alter parameters", startX + 3, startY + 117);
g.drawString("Press the FORWARD arrow while paused to move one step", startX + 3, startY + 143);
g.drawString("Press SPACEBAR to pause or resume", startX + 3, startY + 156);
g.drawString("Reset the simulation by pressing ENTER", startX + 3, startY + 169);
g.drawString("Groups:", startX + 3, startY + 195);
g.drawString("Assign agents to any group by pressing 0-9 when selected.", startX + 3, startY + 208);
g.drawString("CLICK on any agent to select it, or select several agents", startX + 3, startY + 221);
g.drawString("at once by DRAGGING a selection box around them.", startX + 3, startY + 234);
g.drawString("Agents will only cohere to others in their group, but will", startX + 3, startY + 247);
g.drawString("still avoid collisions with all agents in the simulation.", startX + 3, startY + 260);
g.drawString("Press SPACEBAR to begin", startX + 3, startY + 286);
g.setFont(new Font("SansSerif", Font.ITALIC, 10));
g.drawString("NOTE: these instructions can be brought up by pressing i", startX + 3, startY + 299);
}

private Rectangle getDraggedRect() {
int xS;
int width;
int yS;
int height;
if (startX > endX) {
xS = endX;
width = startX - endX;
}
else {
xS = startX;
width = endX - startX;
}
if (startY > endY) {
yS = endY;
height = startY - endY;
}
else {
yS = startY;
height = endY - startY;
}
return new Rectangle(xS - 3, yS - 3, width + 7, height + 7);
}

private double roundTwo(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
stop();
startX = e.getX();
startY = e.getY();
}

public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
Rectangle r = getDraggedRect();
Point temp;
for (Agent i : env.getAgents()) {
temp = new Point((int) i.getX(), (int) i.getY());
if (r.contains(temp)) {
i.select();
}
else {
i.setSelected(false);
}
}
startX = -1;
startY = -1;
endX = -1;
endY = -1;
repaint();
}

public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
}

public void mouseMoved(MouseEvent e) {}

public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case 10:
restart();
break;
case 32:
start = false;
if (threadSuspended) {
start();
}
else {
stop();
}
break;
case 39:
if (threadSuspended && !start) {
env.cycle();
repaint();
}
break;
case 83:
currentParam = 1;
repaint();
break;
case 82:
currentParam = 2;
repaint();
break;
case 86:
currentParam = 3;
repaint();
break;
case 72:
currentParam = 4;
repaint();
break;
case 66:
currentParam = 5;
repaint();
break;
case 65:
env.setAllSelected();
repaint();
break;
case 73:
if (!start) {
instructions = !instructions;
repaint();
}
break;
case 48:
env.setSelectedGroup(0);
break;
case 49:
env.setSelectedGroup(1);
break;
case 50:
env.setSelectedGroup(2);
break;
case 51:
env.setSelectedGroup(3);
break;
case 52:
env.setSelectedGroup(4);
break;
case 53:
env.setSelectedGroup(5);
break;
case 54:
env.setSelectedGroup(6);
break;
case 55:
env.setSelectedGroup(7);
break;
case 56:
env.setSelectedGroup(8);
break;
case 57:
env.setSelectedGroup(9);
break;
}
switch (currentParam) {
case 1:
if (code == 38 && speed > 0) {
speed--;
stepTime = SPEEDS[speed];
repaint();
}
if (code == 40 && speed < SPEEDS.length - 1) {
speed++;
stepTime = SPEEDS[speed];
repaint();
}
break;
case 2:
if (code == 38 && env.getRange() < 200) {
env.setRange(env.getRange() + 5);
repaint();
}
if (code == 40 && env.getRange() > 5) {
env.setRange(env.getRange() - 5);
repaint();
}
break;
case 3:
if (code == 38 && env.getVelocityDiscount() < 0.99) {
env.setVelocityDiscount(roundTwo(env.getVelocityDiscount() + 0.01));
repaint();
}
if (code == 40 && env.getVelocityDiscount() > 0.00) {
env.setVelocityDiscount(roundTwo(env.getVelocityDiscount() - 0.01));
repaint();
}
break;
case 4:
if (code == 38 && env.getHeadingDiscount() < 0.99) {
env.setHeadingDiscount(roundTwo(env.getHeadingDiscount() + 0.01));
repaint();
}
if (code == 40 && env.getHeadingDiscount() > 0.01) {
env.setHeadingDiscount(roundTwo(env.getHeadingDiscount() - 0.01));
repaint();
}
break;
case 5:
if (code == 38 && env.getBuffer() < 100) {
env.setBuffer(env.getBuffer() + 5);
repaint();
}
if (code == 40 && env.getBuffer() > 0) {
env.setBuffer(env.getBuffer() - 5);
repaint();
}
break;
}
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}
}

任何关于为什么无法正确加载的帮助或建议都将受到赞赏。谢谢!

最佳答案

启动 Java 控制台和小程序并观察异常。

关于Java Applet 在浏览器中加载时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671601/

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