It lets BeanShell dynamically create classes on the fly. 8)
The trouble was that when you run a beanshell script that generates a class from a Web-Started VM, the script throws a SecurityException since the created class is unsigned which is illegal in WebStart (but is OK with this dummy security manager).
With this change you should be able to paste the below code into my webstarted beanshell front-end app (the only webstartable beanshell editor I’ve ever found), click ‘Run’ and then see a game window. Before it was impossible.
Thanks for the interest :),
Keith
webstart: http://www.freewebs.com/commanderkeith/SlaveBot/SlaveBot_jvm1.4+.jnlp
code:
// originally written by zeroone, and then made 1.4 compatible
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class M extends JFrame {
boolean[] K = new boolean[65535]; // pressed keys
public M() {
final int WIDTH = 640;
final int HEIGHT = 480;
final int FRAMES_PER_SECOND = 60;
final long FRAME_PERIOD = 1000000000L / FRAMES_PER_SECOND;
Random random = new Random();
// Define model
final int BALLS = 5;
final int BALL_SIZE = WIDTH / 10;
final int PADDLE_WIDTH = BALL_SIZE * 2;
final int PADDLE_HEIGHT = HEIGHT / 20;
final int BALL_VELOCITY = 2;
final int PADDLE_VELOCITY = 10;
int[][] balls = new int[BALLS][4]; // [ball index][{x, y, Vx, Vy}]
for(int i = 0; i < BALLS; i++) {
balls[i] = new int[] {
random.nextInt(WIDTH - BALL_SIZE), // x
random.nextInt(HEIGHT - BALL_SIZE - PADDLE_HEIGHT), // y
random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, // Vx
random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, // Vy
};
}
int paddleX = (WIDTH - PADDLE_WIDTH) / 2;
// Setup frame
JPanel panel = (JPanel)getContentPane();
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setLocationRelativeTo(null);
show();
// Create off-screen image for rendering
Image image = createImage(WIDTH, HEIGHT);
Graphics imageGraphics = image.getGraphics();
long nextFrameStart = System.currentTimeMillis()*1000000; //System.nanoTime();
while(true) {
do {
// -- UPDATE MODEL BEGIN -------------------------------------------------------
// Move the balls
for(int i = 0; i < BALLS; i++) {
int[] ball = balls[i];
if (ball[0] >= WIDTH - BALL_SIZE) {
ball[2] = -BALL_VELOCITY;
} else if (ball[0] <= 0) {
ball[2] = BALL_VELOCITY;
}
if (ball[1] >= HEIGHT - BALL_SIZE - PADDLE_HEIGHT) {
ball[3] = -BALL_VELOCITY;
} else if (ball[1] <= 0) {
ball[3] = BALL_VELOCITY;
}
ball[0] += ball[2];
ball[1] += ball[3];
}
// Move the paddle
if (K[KeyEvent.VK_LEFT] && paddleX > 0) {
paddleX -= PADDLE_VELOCITY;
}
if (K[KeyEvent.VK_RIGHT] && paddleX < WIDTH - PADDLE_WIDTH) {
paddleX += PADDLE_VELOCITY;
}
// -- UPDATE MODEL END ---------------------------------------------------------
nextFrameStart += FRAME_PERIOD;
} while(nextFrameStart < System.currentTimeMillis()*1000000);
// -- RENDER FRAME BEGIN -------------------------------------------------------
imageGraphics.setColor(Color.BLACK);
imageGraphics.fillRect(0, 0, WIDTH, HEIGHT);
// Render balls
imageGraphics.setColor(Color.RED);
for(int i = 0; i < BALLS; i++) {
int[] ball = balls[i];
imageGraphics.fillOval(ball[0], ball[1], BALL_SIZE, BALL_SIZE);
}
// Render paddle
imageGraphics.setColor(Color.YELLOW);
imageGraphics.fillRect(
paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
// Draw to screen
Graphics panelGraphics = panel.getGraphics();
if (panelGraphics != null) {
panelGraphics.drawImage(image, 0, 0, null);
panelGraphics.dispose();
}
// -- RENDER FRAME END ---------------------------------------------------------
long remaining = nextFrameStart - System.currentTimeMillis()*1000000;//System.nanoTime();
if (remaining > 0) {
try {
Thread.sleep(remaining / 1000000);
} catch(Throwable t) {
}
}
}
}
public void processKeyEvent(KeyEvent e) {
K[e.getKeyCode()] = e.getID() == 401;
}
public static void main(String[] args) {
new M();
}
}
M.main(null);