eclipse applet空白屏幕

时间:2014-03-07 17:18:24

标签: java eclipse applet awt

我试图在Eclipse中使用类型键在Windows 8上运行游戏。 每当我使用扩展Applet运行应用程序时,我只会得到一个空白屏幕。我可以更改背景的颜色,但我的图像都没有显示。我甚至可以使用应用程序并使用我的箭头键来获得回报,但我无法在屏幕上看到任何内容。我不确定是否应该扩展Applet或JApplet。 这是我的代码

package kiloboltgame;

import java.applet.Applet;
import java.awt.*;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics;
import java.net.URL;

public class StartingClass extends Applet implements Runnable, KeyListener {

    private Robot robot;    
    private Image image, currentSprite, character, characterDown, characterJumped, background;
    private Graphics second;
    private URL base;
    private static Background bg1, bg2; 

    @Override
    public void init() {

        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Q-Bot Alpha");
        try {
            base = getDocumentBase();
        } catch (Exception e){
            //TODO: handle exception
        }

        character = getImage(base, "data/character.png");
        characterDown = getImage(base, "data/down.png");
        characterJumped = getImage(base, "data/jumped.png");
        currentSprite = character;
        background = getImage(base, "data/background.png");
    }

    @Override
    public void start() {
        bg1 = new Background(0,0);
        bg2 = new Background(2160, 0);
        robot = new Robot();

        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void stop() {
        //super.stop();
    }

    @Override
    public void destroy() {
        //super.destroy();
    }

    @Override
    public void run() {
        while (true) {
            robot.update();
            if (robot.isJumped()){
                currentSprite = characterJumped;
            } else if (robot.isJumped() == false && robot.isDucked() == false){
                currentSprite = character;
            }
            bg1.update();
            bg2.update();
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @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){
        g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
        g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
        g.drawImage(currentSprite, robot.getCenterX()-61, robot.getCenterY()-63, this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Move Up");
            break;

        case KeyEvent.VK_DOWN:
            currentSprite = characterDown;
            if (robot.isJumped() == false){
                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;
        }
    }

    @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;
            break;

        case KeyEvent.VK_LEFT:
            robot.stopLeft();
            break;

        case KeyEvent.VK_RIGHT:
            robot.stopRight();
            break;

        case KeyEvent.VK_SPACE:
            //System.out.println("Stop jumping");
            break;
        }
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    public static Background getBg1(){
        return bg1;
    }

    public static Background getBg2(){
        return bg2;
    }
}

0 个答案:

没有答案