如何使用JFrame将此 小程序 更改为 应用程序 ?我是java编程的新手。我实际上不知道如何将java applet转换为java桌面应用程序。所以,帮我一个人将这个小程序转换为一个应用程序。
package kiloboltgame;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import kiloboltgame.framework.Animation;
public class StartingClass extends Applet implements Runnable, KeyListener {
enum GameState {
Running, Dead
}
GameState state = GameState.Running;
private static Robot robot;
public static Heliboy hb, hb2;
public static int score = 0;
private Font font = new Font(null, Font.BOLD, 30);
private static Background bg1, bg2;
private Image image, currentSprite, character, character2, character3,
characterDown, characterJumped, background, heliboy, heliboy2,
heliboy3, heliboy4, heliboy5;
private URL base;
private Graphics second;
private Animation anim, hanim;
public static Image tilegrassTop, tilegrassBot, tilegrassLeft,
tilegrassRight, tiledirt;
private ArrayList<Tile> tilearray = new ArrayList<Tile>();
@Override
public void init() {
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Action Robot");
addKeyListener(this);
try {
base = getDocumentBase();
} catch (Exception e) {
}
character = getImage(base, "data/character.png");
character2 = getImage(base, "data/character2.png");
character3 = getImage(base, "data/character3.png");
characterDown = getImage(base, "data/down.png");
characterJumped = getImage(base, "data/jumped.png");
heliboy = getImage(base, "data/heliboy.png");
heliboy2 = getImage(base, "data/heliboy2.png");
heliboy3 = getImage(base, "data/heliboy3.png");
heliboy4 = getImage(base, "data/heliboy4.png");
heliboy5 = getImage(base, "data/heliboy5.png");
background = getImage(base, "data/background.png");
tiledirt = getImage(base, "data/tiledirt.png");
tilegrassTop = getImage(base, "data/tilegrasstop.png");
tilegrassBot = getImage(base, "data/tilegrassbot.png");
tilegrassLeft = getImage(base, "data/tilegrassleft.png");
tilegrassRight = getImage(base, "data/tilegrassright.png");
anim = new Animation();
anim.addFrame(character, 1250);
anim.addFrame(character2, 50);
anim.addFrame(character3, 50);
anim.addFrame(character2, 50);
hanim = new Animation();
hanim.addFrame(heliboy, 100);
hanim.addFrame(heliboy2, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy5, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy2, 100);
currentSprite = anim.getImage();
}
@Override
public void start() {
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
robot = new Robot();
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
// Initialize tiles
try {
loadMap("data/map1.txt");
} catch (Exception e) {
// TODO: handle exception
}
Thread thread = new Thread(this);
thread.start();
}
// Loading the tile map
private void loadMap(String filename) throws IOException {
ArrayList lines = new ArrayList();
int width = 0;
int height = 0;
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (true) {
String line = reader.readLine();
if (line == null) {
reader.close();
break;
}
if (!line.startsWith("!")) {
lines.add(line);
width = Math.max(width, line.length());
}
}
height = lines.size(); // it represents the total arraylist value size
for (int j = 0; j < 12; j++) {
String line = (String) lines.get(j);
for (int i = 0; i < width; i++) {
System.out.println(i + " is i");
if (i < line.length()) {
char ch = line.charAt(i);
Tile t = new Tile(i, j, Character.getNumericValue(ch));
tilearray.add(t);
}
}
}
}
@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
public void run() {
if (state == GameState.Running) {
while (true) {
robot.update();
if (robot.isJumped() == true) {
robot.setDucked(false);
currentSprite = characterJumped;
} else if (robot.isJumped() == false) {
currentSprite = anim.getImage();
}
if (robot.isDucked() == true) {
currentSprite = characterDown;
}
ArrayList<Projectile> projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
if (p.isVisible() == true) {
p.update();
} else {
projectiles.remove(i);
}
}
updateTiles();
hb.update();
hb2.update();
bg1.update();
bg2.update();
animate();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (robot.getCenterY() > 500) {
state = GameState.Dead;
}
}
}
}
public void animate() {
anim.update(10);
hanim.update(50);
}
// this update method is used for double buffering
// and it's called autometically internally
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g) {
if (state == GameState.Running) { // set for game state Running
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
paintTiles(g);
// g.drawRect((int)robot.rect.getX(), (int)robot.rect.getY(),
// (int)robot.rect.getWidth(), (int)robot.rect.getHeight());
// g.drawRect((int)robot.rect2.getX(), (int)robot.rect2.getY(),
// (int)robot.rect2.getWidth(), (int)robot.rect2.getHeight());
// g.drawRect((int)robot.rect3.getX(), (int)robot.rect3.getY(),
// (int)robot.rect3.getWidth(), (int)robot.rect3.getHeight());
// g.drawRect((int)robot.rect4.getX(), (int)robot.rect4.getY(),
// (int)robot.rect4.getWidth(), (int)robot.rect4.getHeight());
g.drawImage(currentSprite, robot.getCenterX() - 61,
robot.getCenterY() - 63, this);
g.drawImage(hanim.getImage(), hb.getCenterX() - 48,
hb.getCenterY() - 48, this);
g.drawImage(hanim.getImage(), hb2.getCenterX() - 48,
hb2.getCenterY() - 48, this);
ArrayList<Projectile> projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
g.setColor(Color.YELLOW);
g.fillRect(p.getX(), p.getY(), 10, 5);
}
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString("Score: " + Integer.toString(score), 640, 30);
// Set for Enemy Health
if (hb.enemyVisibility == true) {
g.drawString("Health: " + Integer.toString(hb.health), 450, 30);
} else if (hb2.enemyVisibility == true) {
g.drawString("Health: " + Integer.toString(hb2.health), 450, 30);
}
} else if (state == GameState.Dead) { // set for game state Dead
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.WHITE);
g.drawString("DEAD", 360, 240);
}
}
private void updateTiles() {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
t.update();
}
}
private void paintTiles(Graphics g) {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY(), this);
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("moving up");
break;
case KeyEvent.VK_DOWN:
if (robot.isJumped() == false) {
// currentSprite = anim.getImage();
robot.setDucked(true);
robot.setSpeedX(0);
}
break;
case KeyEvent.VK_LEFT:
robot.moveLeft();
robot.setMovingLeft(true);
break;
case KeyEvent.VK_RIGHT:
robot.moveRight();
robot.setMovingRight(true);
break;
case KeyEvent.VK_SPACE:
robot.jump();
break;
case KeyEvent.VK_CONTROL:
if (robot.isDucked() == false && robot.isJumped() == false) {
robot.shoot();
robot.setReadyToFire(false);
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("stop moving up");
break;
case KeyEvent.VK_DOWN:
currentSprite = character;
robot.setDucked(false);
break;
case KeyEvent.VK_LEFT:
robot.stopLeft();
break;
case KeyEvent.VK_RIGHT:
robot.stopRight();
break;
case KeyEvent.VK_SPACE:
break;
case KeyEvent.VK_CONTROL:
robot.setReadyToFire(true);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static Background getBg1() {
return bg1;
}
public static Background getBg2() {
return bg2;
}
public static Robot getRobot() {
return robot;
}
}
如何使用JFrame将此 Applet 更改为应用程序?我是java编程的新手。我实际上不知道如何将java applet转换为java桌面应用程序。所以,帮我一个人将这个小程序转换为一个应用程序。