如何添加一个说升级的按钮?

时间:2013-05-25 15:41:34

标签: java button jframe jbutton containers

我已经尝试了很多方法来为以下代码添加一个按钮,它运行一个太空入侵者游戏,而且没有一个有效。有人可以看看是否有解决方案吗?

提前谢谢。

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;

 public class Game extends Canvas {
 /** Image variables to be drawn */
Image imgLevel;
Image imgAliens;
Image img;
JFrame container;  
private BufferStrategy strategy;
private boolean gameRunning = true;
private ArrayList entities = new ArrayList();
private ArrayList removeList = new ArrayList();
private Entity ship;
private double moveSpeed = 500;
private long lastFire = 0;
private long firingInterval;
private long ExtraFiringInterval = 100;
    private long bombFiringInterval = 500;
private int alienCount;
private String message = "";
private boolean waitingForKeyPress = true;
private boolean leftPressed = false;
private boolean rightPressed = false;
    private boolean upPressed = false;
    private boolean downPressed = false;
private boolean firePressed = false;
    private boolean fireExtraPressed = false;
    private boolean bombFired = false;
private boolean logicRequiredThisLoop = false;
    static int level = 8;
    private int Armour;
    Toolkit tk = Toolkit.getDefaultToolkit();  
            int xSize = ((int) tk.getScreenSize().getWidth());  
            int ySize = ((int) tk.getScreenSize().getHeight());
/**
 * Construct our game and set it running.
 */
public Game() {
        img = Toolkit.getDefaultToolkit().createImage("Bg.gif");
    container = new JFrame("Space Invaders Mod");
            container.setSize(xSize,ySize);  
            container.show();
    JPanel panel = (JPanel) container.getContentPane();
    panel.setSize(xSize, ySize);
    panel.setLayout(null);
            container.setSize(xSize, ySize);
    setBounds(0,0,xSize,ySize);
    panel.add(this);
    setIgnoreRepaint(true);
    container.pack();
    container.setResizable(true);
    container.setVisible(true);
    container.addWindowListener(new WindowAdapter() {
                    @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    addKeyListener(new KeyInputHandler());

    requestFocus();

    createBufferStrategy(2);
    strategy = getBufferStrategy();
            initEntities();
}


/**
 * Start a fresh game, this should clear out any old data and
 * create a new set.
 */
private void startGame() {
    entities.clear();
    initEntities();

    leftPressed = false;
    rightPressed = false;
            upPressed = false;
            downPressed = false;
    firePressed = false;
            fireExtraPressed = false;
            bombFired = false;
            firingInterval = 100;
}

/**
 * Initialise the starting state of the entities (ship and aliens). Each
 * entitiy will be added to the overall list of entities in the game.
 */
private void initEntities() {
    ship = new ShipEntity(this,"sprites/ship.png",370,ySize - 130);
    entities.add(ship);  


    alienCount = 0;
            if (level == 1) {
                Armour = 10;
            for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
            entities.add(alien3);
                            alienCount++;
            }
    }
            }
            if (level == 2) {
                Armour = 20;
            for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
            }
    }
            }
            if (level == 3) {
                Armour = 30;
           for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
            }
    }
                            }

            if (level == 4) {
                Armour = 40;
            for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
            }
    }
            }
            if (level == 5) {
                Armour = 50;
            for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
            }
    }
            }
            if (level == 6) {
                Armour = 60;
            for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
                            entities.add(alien8);
                            alienCount++;
            }
    }
            }
            if (level == 7) {
                Armour = 70;
    for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
                            Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
                            entities.add(alien8);
                            alienCount++;
                            entities.add(alien9);
                            alienCount++;
            }
    }
            }
            if (level == 8) {
                Armour = 80;
                for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
                            Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
                            Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
                            entities.add(alien8);
                            alienCount++;
                            entities.add(alien9);
                            alienCount++;
                            entities.add(alien10);
                            alienCount++;

            }
    }
}
            if (level == 9) {
                Armour = 90;
                ExtraFiringInterval --;
                for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
                            Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
                            Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
                            Entity alien11 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(80)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
                            entities.add(alien8);
                            alienCount++;
                            entities.add(alien9);
                            alienCount++;
                            entities.add(alien10);
                            alienCount++;
                            entities.add(alien11);
                            alienCount++;
            }
    }
}
            if (level == 10){
                Armour = 100;
                ExtraFiringInterval --;
                for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
                            Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
                            Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
                            Entity alien11 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(80)+row*30);
                            Entity alien12 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(90)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
                            entities.add(alien8);
                            alienCount++;
                            entities.add(alien9);
                            alienCount++;
                            entities.add(alien10);
                            alienCount++;
                            entities.add(alien11);
                            alienCount++;
                            entities.add(alien12);
                            alienCount++;
            }
    }
    }
            if (level == 11){
                Armour = 110;
                for (int row=0;row<1;row++) {
                    for (int x=0;x<25;x++) {
            Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
                            Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
                            Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
                            Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
                            Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
                            Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
                            Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
                            Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
                            Entity alien11 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(80)+row*30);
                            Entity alien12 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(90)+row*30);
                            Entity alien13 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(100)+row*30);
                            entities.add(alien3);
                            alienCount++;
                            entities.add(alien4);
                            alienCount++;
                            entities.add(alien5);
                            alienCount++;
                            entities.add(alien6);
                            alienCount++;
                            entities.add(alien7);
                            alienCount++;
                            entities.add(alien8);
                            alienCount++;
                            entities.add(alien9);
                            alienCount++;
                            entities.add(alien10);
                            alienCount++;
                            entities.add(alien11);
                            alienCount++;
                            entities.add(alien12);
                            alienCount++;
                            entities.add(alien13);
                            alienCount++;
            }
    }
            }
    }

public void updateLogic() {
    logicRequiredThisLoop = true;
}


public void removeEntity(Entity entity) {
    removeList.add(entity);
}


public void notifyDeath() {
    message = "Oh no! They got you, try again?";
    waitingForKeyPress = true;
            level = 0;
}


public void notifyWin() {
    message = "Well done! You beat level "+level;
    waitingForKeyPress = true;
            if (level == 10){
                message = "Well done! You actually beat level 10. Good luck!!";
            }
}


public void notifyAlienKilled() {
    // reduce the alient count, if there are none left, the player has won!
    alienCount = alienCount-1;
            ShotEntity.death = false;
            ShotEntityExtra.death = false;


    if (alienCount == 0) {
        notifyWin();
    }

    // if there are still some aliens left then they all need to get faster, so
    // speed up all the existing aliens
    for (int i=0;i<entities.size();i++) {
        Entity entity = (Entity) entities.get(i);

        if (entity instanceof AlienEntity) {
            // speed up by 2%
            entity.setHorizontalMovement(entity.getHorizontalMovement() * 1.02);
        }
    }
}


public void tryToFire() {

    if (System.currentTimeMillis() - lastFire < firingInterval) {
        return;
    }
            if (level > 6) {

    lastFire = System.currentTimeMillis();
    ShotEntity shot2 = new ShotEntity(this,"sprites/shot.png",ship.getX()+70,ship.getY()-30);
    entities.add(shot2);

    lastFire = System.currentTimeMillis();
    ShotEntity shot3 = new ShotEntity(this,"sprites/shot.png",ship.getX()-30,ship.getY()-30);
    entities.add(shot3);
            }
            if (level > 7){
                lastFire = System.currentTimeMillis();
    ShotEntity shot2 = new ShotEntity(this,"sprites/shot.png",ship.getX()+90,ship.getY()-30);
    entities.add(shot2);
            // if we waited long enough, create the shot entity, and record the time.
    lastFire = System.currentTimeMillis();
    ShotEntity shot3 = new ShotEntity(this,"sprites/shot.png",ship.getX()-50,ship.getY()-30);
    entities.add(shot3);
            }

    // if we waited long enough, create the shot entity, and record the time.
    lastFire = System.currentTimeMillis();
    ShotEntity shot = new ShotEntity(this,"sprites/shot.png",ship.getX()+20,ship.getY()-30);
    entities.add(shot);
            if (level > 3){
                // if we waited long enough, create the shot entity, and record the time.
    lastFire = System.currentTimeMillis();
    ShotEntity shot2 = new ShotEntity(this,"sprites/shot.png",ship.getX()+50,ship.getY()-30);
    entities.add(shot2);
            // if we waited long enough, create the shot entity, and record the time.
    lastFire = System.currentTimeMillis();
    ShotEntity shot3 = new ShotEntity(this,"sprites/shot.png",ship.getX()-10,ship.getY()-30);
    entities.add(shot3);
            }
}
    public void tryToFireExtra() {
    // check that we have waiting long enough to fire
    if (System.currentTimeMillis() - lastFire < ExtraFiringInterval) {
        return;
    }

    // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            ShotEntityExtra shotExtra = new ShotEntityExtra(this,"sprites/shotExtra.png",ship.getX(),ship.getY()-30);
    entities.add(shotExtra);
            // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            ShotEntityExtra shotExtra2 = new ShotEntityExtra(this,"sprites/shotExtra.png",ship.getX()+50,ship.getY()-30);
    entities.add(shotExtra2);
            if (level == 9){
                // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            ShotEntityExtra shotExtra3 = new ShotEntityExtra(this,"sprites/shotExtra.png",ship.getX()+25,ship.getY()-30);
    entities.add(shotExtra3);

            }
            if (level == 10){
                // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            ShotEntityExtra shotExtra3 = new ShotEntityExtra(this,"sprites/shotExtra.png", (int) (ship.getX()+12.5),ship.getY()-30);
    entities.add(shotExtra3);
            // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            ShotEntityExtra shotExtra4 = new ShotEntityExtra(this,"sprites/shotExtra.png", (int) (ship.getX()+37.5),ship.getY()-30);
    entities.add(shotExtra4);
            // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            ShotEntityExtra shotExtra5 = new ShotEntityExtra(this,"sprites/shotExtra.png", (int) (ship.getX()+25),ship.getY()-30);
    entities.add(shotExtra5);
            }
}
    public void tryToFireBomb() {
    // check that we have waiting long enough to fire
    if (System.currentTimeMillis() - lastFire < bombFiringInterval) {
        return;
    }

    // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            Bomb bomb = new Bomb(this,"sprites/bomb.png",ship.getX(),ship.getY()-30);
    entities.add(bomb);
            // if we waited long enough, create the shot entity, and record the time.
            lastFire = System.currentTimeMillis();
            Bomb bomb2 = new Bomb(this,"sprites/bomb.png",ship.getX()+10,ship.getY()-30);
    entities.add(bomb2);
}
public void gameLoop() {
    long lastLoopTime = System.currentTimeMillis();

    // keep looping round til the game ends
    while (gameRunning) {
        // work out how long its been since the last update, this
        // will be used to calculate how far the entities should
        // move this loop
        long delta = System.currentTimeMillis() - lastLoopTime;
        lastLoopTime = System.currentTimeMillis();

        // Get hold of a graphics context for the accelerated 
        // surface and blank it out
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
                    //g.drawImage(img, 0, 0,xSize, ySize, null);
                    g.drawRect(0, 0, xSize, ySize);
                    g.fillRect(0, 0, xSize, ySize);
                    g.setColor(Color.BLACK);
        // cycle round asking each entity to move itself
        if (!waitingForKeyPress) {
            for (int i=0;i<entities.size();i++) {
                Entity entity = (Entity) entities.get(i);

                entity.move(delta);
            }
        }

        // cycle round drawing all the entities we have in the game
        for (int i=0;i<entities.size();i++) {
            Entity entity = (Entity) entities.get(i);

            entity.draw(g);
        }

        // brute force collisions, compare every entity against
        // every other entity. If any of them collide notify 
        // both entities that the collision has occured
        for (int p=0;p<entities.size();p++) {
            for (int s=p+1;s<entities.size();s++) {
                Entity me = (Entity) entities.get(p);
                Entity him = (Entity) entities.get(s);

                if (me.collidesWith(him)) {
                    me.collidedWith(him);
                    him.collidedWith(me);
                }
            }
        }

        // remove any entity that has been marked for clear up
        entities.removeAll(removeList);
        removeList.clear();

        // if a game event has indicated that game logic should
        // be resolved, cycle round every entity requesting that
        // their personal logic should be considered.
        if (logicRequiredThisLoop) {
            for (int i=0;i<entities.size();i++) {
                Entity entity = (Entity) entities.get(i);
                entity.doLogic();
            }

            logicRequiredThisLoop = false;
        }

        // if we're waiting for an "any key" press then draw the 
        // current message 
        if (waitingForKeyPress) {
            g.setColor(Color.white);
            g.drawString(message,(800-g.getFontMetrics().stringWidth(message))/2,250);
            g.drawString("Press any key to start game",(800-g.getFontMetrics().stringWidth("Press any key to start game"))/2,300);
        }
                    //score
        g.setColor(Color.WHITE);
        g.drawString("" + alienCount,10,10);
                    g.drawString(" Aliens left", 25, 10);
                    //level
                    g.setColor(Color.WHITE);
        g.drawString("" + level,50,20);
        g.drawString(" Level:", 10, 20);
        // finally, we've completed drawing so clear up the graphics
        // and flip the buffer over
        g.dispose();

1 个答案:

答案 0 :(得分:1)

我建议您在继续之前阅读一些tutorials

以下是有关如何将JButton添加到JFrame的简单通用示例:

JFrame frame = new JFrame("FrameDemo");
JButton button = new JButton("upgrade"); //The JButton name.
frame.add(button); //Add the button to the JFrame.
frame.setSize(200, 200);
frame.setVisible(true);

如果您了解这5行是如何工作的,您可以将它应用于您正在处理的任何内容。