我制作了一个射击游戏,随机出现粘液,你必须向它们射击。然而,它是使用Applet
制作的,我无法将其嵌入网页。所以我试图把它变成JFrame
。除了添加主要方法并扩展JFrame
而不是Applet
之外,我还应该做些什么呢?
package ss;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import sun.audio.*;
@SuppressWarnings("serial")
public class Slimeshooter extends Applet implements Runnable, MouseListener{
AudioStream stream = null;
AudioStream stream2 = null;
public Graphics d;
public BufferedImage redS, blueS, orangeS, greenS, purpleS, yellowS, chosenSlime, goat;
public Image offscreen;
public boolean slimeSpawn;
public boolean changeSlime;
public int posX;
public int posY;
public int hits, misses;
public static int secondsleft = 60;
public static Thread th2;
public boolean finished = false;
@SuppressWarnings("static-access")
public void run(){
String bgmusic = "bgmusic.wav";
InputStream in2 = null;
try {
in2 = new FileInputStream(bgmusic);
} catch (FileNotFoundException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
try {
stream2 = new AudioStream(in2);
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
AudioPlayer.player.start(stream2);
slimeSpawn = true;
try {
blueS = ImageIO.read(new File("slimeBlue.png"));
redS = ImageIO.read(new File("slimeRed.png"));
yellowS = ImageIO.read(new File("slimeYellow.png"));
orangeS = ImageIO.read(new File("slimeOrange.png"));
purpleS = ImageIO.read(new File("slimePurple.png"));
greenS = ImageIO.read(new File("slimeGreen.png"));
goat = ImageIO.read(new File("goat.png"));
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(secondsleft > 0){
th2 = new Thread();
try {
th2.sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
}
secondsleft--;
} else {
finished = true;
repaint();
}
if(slimeSpawn == true){
posX = (int) (Math.random() * 1860);
posY = (int) (Math.random() * 958);
int slimeColor = (int) (Math.random() * 6);
if(slimeColor == 0){
chosenSlime = blueS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 1){
chosenSlime = yellowS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 2){
chosenSlime = purpleS;
slimeSpawn = false;;
repaint();
}
if(slimeColor == 3){
chosenSlime = greenS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 4){
chosenSlime = orangeS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 5){
chosenSlime = redS;
slimeSpawn = false;
repaint();
}
}
}
}
public void spawnSlime(){
if(changeSlime == true){
repaint();
posX = (int) (Math.random() * 1860);
posY = (int) (Math.random() * 958);
int slimeColor = (int) (Math.random() * 6);
if(slimeColor == 0){
chosenSlime = blueS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 1){
chosenSlime = yellowS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 2){
chosenSlime = purpleS;
slimeSpawn = false;;
repaint();
}
if(slimeColor == 3){
chosenSlime = greenS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 4){
chosenSlime = orangeS;
slimeSpawn = false;
repaint();
}
if(slimeColor == 5){
chosenSlime = redS;
slimeSpawn = false;
repaint();
}
}
changeSlime = false;
repaint();
}
public void init(){
setSize(2000,2000);
Thread th = new Thread(this);
th.start();
addMouseListener(this);
offscreen = createImage(2000,2000);
d = offscreen.getGraphics();
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
spawnSlime();
d.setColor(Color.GRAY);
d.fillRect(0, 0, 2000, 2500);
d.drawImage(chosenSlime, posX, posY, this);
g.drawImage(offscreen, 0, 0, this);
g.setColor(Color.BLACK);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("Hits: " + Integer.toString(hits), 10, 40);
g.drawString("Misses: " + Integer.toString(misses), 10, 80);
g.drawString("Time Left: " + Integer.toString(secondsleft), 10, 120);
if(finished == true){
int divide = hits/misses;
String divided = Integer.toString(divide);
g.drawString("You finished with the hit miss ratio of ABOUT... " + divided, 10, 160);
}
}
public void update(Graphics g){
paint(g);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
String gunsound = "gun.wav";
InputStream in = null;
try {
in = new FileInputStream(gunsound);
} catch (FileNotFoundException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
try {
stream = new AudioStream(in);
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
AudioPlayer.player.start(stream);
// Test if the player clicked the slime
if((e.getX() < posX + 50 && e.getY() < posY + 50) && (e.getX() > posX && e.getY() > posY)){
changeSlime = true;
hits++;
}
else {
misses++;
}
}
public void mouseReleased(MouseEvent e) {
}
}