添加到其他JPanel时,JPanel无法呈现

时间:2015-05-26 03:46:36

标签: java swing jframe jpanel paintcomponent

这是我的游戏类:

package Game;

import java.awt.Color;
import java.awt.Graphics;

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

public class Game extends JPanel implements Runnable {
    Thread thread;
    private boolean running = true;
    private double FPS = 1.0/60.0;
    private Level level;

    public Game(){
        level = new LevelOne();
        level.setBackground(Color.BLACK);
        add(level);
    }

    public synchronized void start() {
        thread = new Thread(this, "Game");
        thread.start();
    }

    public synchronized void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        double firstTime = 0;
        double lastTime = System.nanoTime() / 1000000000.0;
        double passedTime = 0;
        double updateTime = 0;
        double timer = System.nanoTime() / 1000000000.0;
        int rendered = 0;
        int updates = 0;


        while (running) { 
            firstTime = System.nanoTime() / 1000000000.0;
            passedTime = firstTime - lastTime;
            lastTime = firstTime;

            updateTime += passedTime;

            while(updateTime > FPS){
                updates++;
                update();
                updateTime -= FPS;
            }

            render();
            rendered++;
            if((System.nanoTime() / 1000000000) - timer > 1){
                timer += 1;
                System.out.println("FPS:"+rendered+" Updates:"+updates);
                updates = 0;
                rendered = 0;
            }
        }
    }

    private void update() {

    }

    private void render() {
        this.repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        level.repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLocationRelativeTo(null);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setTitle("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Game game = new Game();
        frame.add(game);
        game.start();
    }

}

我的LevelOne课程:     包游戏;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LevelOne extends Level {
    private BufferedImage spriteSheet;
    private BufferedImage[] player = new BufferedImage[4]; //0 = down, 1 = right, 2 = up, 3 = left
    private int dir = 0;
    private String UP = "W";
    private String DOWN = "S";
    private String LEFT = "A";
    private String RIGHT = "D";
    private int speedX = 0;
    private int speedY = 0;

    public LevelOne(){
        try {
            spriteSheet = ImageIO.read(new File("images/sprites.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 4; i++) {
            player[i] = spriteSheet.getSubimage((i * 18), 0, 18, 28);
        }

        this.addKeyListener(new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {
                String key = e.getKeyText(e.getKeyCode());
                if (key.equals(UP)) {
                    dir = 2;
                    speedY = -5;
                } else if (key.equals(LEFT)) {
                    dir = 3;
                    speedX = -5;
                } else if (key.equals(RIGHT)) {
                    dir = 1;
                    speedX = 5;
                } else if (key.equals(DOWN)) {
                    dir = 0;
                    speedY = 5;
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                String key = e.getKeyText(e.getKeyCode());
                if (key.equals(UP)) {
                    speedY = 0;
                } else if (key.equals(LEFT)) {
                    speedX = 0;
                } else if (key.equals(RIGHT)) {
                    speedX = 0;
                } else if (key.equals(DOWN)) {
                    speedY = 0;
                }
            }
            @Override
            public void keyTyped(KeyEvent e) {

            }
        });
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(player[dir], 0, 0, 18, 28, null);
    }
}

LevelOne扩展Level,当前为空(并扩展JPanel),我不确定是否需要向Level添加任何内容。我只是不知道我需要做什么来让玩家形象出现......

抱歉,如果我的代码很草率,我正试图进入更高级别的Java,我不确定我是否只是接近错误。

感谢。

1 个答案:

答案 0 :(得分:2)

六件事......

  1. 不要在level.repaint的{​​{1}}方法中调用paintComponent,这可能会导致无限循环的重新绘制请求,这会影响您的帧速率。考虑在你的渲染方法中调用它
  2. Game永远不应该paintComponent,没有理由让任何人直接调用它。
  3. 使用public上的键绑定,它们将解决与焦点相关的问题。 How to Use Key Bindings
  4. KeyListener之后移动frame.setVisible,事实上,你应该做的最后一件事
  5. 确保您的图片正确加载
  6. frame.add(game);的布局管理器设置为Game