2014-06-11 32 views
0

我想通過玩弄一些不同的東西來學習Java和遊戲編程。但現在我遇到了這個問題,當我的Java應用程序通過GraphicsDevice全屏時,KeyListeners不起作用。這就像我按下鍵盤上的按鈕時不會註冊任何東西。當應用程序不是全屏時,一切都按照預期運行。Java - JFrame全屏鍵盤記錄器不工作

我正在使用Mac。

的代碼是有點亂,但它應該是有點易於瀏覽等

Game.class

public class Game extends Canvas implements Runnable { 
    private static final long serialVersionUID = 1L; 

    // Render Vars 
    public static final int WIDTH = 1280; 
    public static final int HEIGHT = 800; //WIDTH/16 * 
    public static final int SCALE = 1; 
    public final static String TITLE = "Test Game - inDev 1.0.0"; 
    public static boolean fullscreen = false; 

    public static JFrame window = new JFrame(TITLE); 

    public static Font defaultFont = new Font("Dialog", Font.PLAIN, 12); 
    public static Color defaultColor = Color.gray; 

    // Thread Vars 
    public static boolean running = false; 
    private static Thread thread; 
    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 

    // Mechanic Vars 
    public static boolean mouseDown; 
    public static int mouseX; 
    public static int mouseY; 

    // Game Vars 
    public static GameState gameState = new Play(); 
    public static boolean keyPressed; 

    public static Game game = new Game(); 

    public static void main(String arghs[]) { 
     game.setSize(new Dimension(WIDTH, HEIGHT)); 
     game.setPreferredSize(new Dimension(WIDTH, HEIGHT)); 

     fullscreen = true; 

     window.add(game); 
     window.setUndecorated(true); 
     window.pack(); 
     window.setLocationRelativeTo(null); 
     window.setResizable(false); 
     window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     window.setVisible(true); 

     // HERE I GO FULLSCREEN 
     GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window); 

     game.addKeyListener(new KeyEventListener()); 
     game.addMouseListener(new MouseEventListener()); 

     game.start(); 
    } 

    public void run() { 

     init(); 

     long lastTime = System.nanoTime(); 
     final double numTicks = 100.0; 
     double ns = 1000000000/numTicks; 
     double delta = 0; 
     int updates = 0; 
     int frames = 0; 
     long timer = System.currentTimeMillis(); 

     while (running) { 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/ns; 
      lastTime = now; 
      if (delta >= 1) { 
       update(); 
       updates++; 
       delta--; 
      } 
      render(); 
      frames++; 

      if (System.currentTimeMillis() - timer > 1000) { 
       timer += 1000; 
       System.out.println(updates + " Ticks, FPS: " + frames); 
       updates = 0; 
       frames = 0; 
      } 
     } 
     stop(); 
    } 

    private synchronized void start() { 
     if (running) { 
      return; 
     } 
     running = true; 

     thread = new Thread(this); 
     thread.setName("My Game"); 
     thread.start(); 
    } 
    public synchronized static void stop() { 
     if (!running) { 
      return; 
     } 
     running = false; 

     thread = null; 
     System.exit(1); 
    } 

    private void init() { 
     gameState.init(); 
    } 

    private void update() { 
     gameState.update(); 
    } 

    private void render() { 
     BufferStrategy bs = this.getBufferStrategy(); 
     if (bs == null) { 
      createBufferStrategy(3); 
      return; 
     } 
     Graphics g = bs.getDrawGraphics(); 

     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 

     g.setColor(defaultColor); 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), this); 

     // Draw Content 
     gameState.render(g); 

     // Draw to Screen; 
     g.dispose(); 
     bs.show(); 
    } 

    public static void setGameState(GameState state) { 
     gameState = state; 
     gameState.init(); 
    } 
} 

KeyEventListener.class

public class KeyEventListener extends KeyAdapter { 
    public void keyPressed(KeyEvent e) { 
     if (!Game.keyPressed) { 
      Game.gameState.keyPressed(e); 
     } 
     Game.keyPressed = true; 
    } 
    public void keyReleased(KeyEvent e) { 
     Game.gameState.keyReleased(e); 
     Game.keyPressed = false; 
    } 
} 

MouseEventListener.class(只用於記錄鼠標位置)

public class MouseEventListener extends MouseAdapter { 

    public void mousePressed(MouseEvent e) { 
     Game.mouseDown = true; 
    } 
    public void mouseReleased(MouseEvent e) { 
     Game.mouseDown = false; 
    } 
    public void mouseMoved(MouseEvent e) { 
     Game.mouseX = e.getX(); 
     Game.mouseY = e.getY(); 
    } 
    public void mouseDragged(MouseEvent e) { 
     Game.mouseX = e.getX(); 
     Game.mouseY = e.getY(); 
    } 
} 

GameState.class

public abstract class GameState { 

    public abstract void init(); 
    public abstract void update(); 
    public abstract void render(Graphics g); 
    public void keyPressed(KeyEvent e) { 

    } 
    public void keyReleased(KeyEvent e) { 

    } 

} 

而且我Play.class這是當前gameState

public class Play extends GameState { 

    public static int key; 

    public void init() { 

    } 

    public void update() { 

    } 

    public void render(Graphics g) { 
     g.drawString("Key: " + key, 100, 100); 
    } 

    public void keyPressed(KeyEvent e) { 
     key = e.getKeyCode(); 
    } 

    public void keyReleased(KeyEvent e) { 

    } 

} 
+3

'KeyEventListener'和'MouseEventListener'在哪裏? 「GameState」在哪裏? – MadProgrammer

+0

'KeyEventListener','MouseEventListener'和'GameState'是我所做的三個單獨的類。當我的應用程序是全屏時,它們都可以正常工作,除了'KeyEventListener'這是我的問題。 – Phoenix1355

+0

是的,但這意味着我們無法運行該程序,或者當我們調整這些缺失的類時,我們可能得不到與您一樣的結果... – MadProgrammer

回答

2

KeyListener當組件被註冊到是可聚焦且有焦點將只對關鍵事件作出迴應。

後您已設置的窗口,全屏,嘗試添加...

game.requestFocusInWindow(); 

你也可能需要使用game.setFocusable(true)

如果它不是一個事實,即你正在使用一個Canvas,我建議使用key bindings API超過所有這些焦點問題

更新

我加...

window.addWindowFocusListener(new WindowAdapter() { 

    @Override 
    public void windowGainedFocus(WindowEvent e) { 
     System.out.println("gainedFocus"); 
     if (!game.requestFocusInWindow()) { 
      System.out.println("Could not request focus"); 
     } 
    } 

}); 

window它是可見的,但之後它被做全屏

更新

哦之前,你一定會喜歡這個.. 。

基於這樣一個問題:FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion

我接着加入setVisible(false)通過setVisible(true)設置窗口到全屏模式後,它似乎已經固定它...

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window); 
window.setVisible(false); 
window.setVisible(true); 

驗證運行在Mac上,使用Java 7

+0

嗯...它似乎沒有工作。它仍然沒有註冊任何關鍵事件。就像我剛剛學習編程java一樣。到目前爲止我沒有太多的經驗。而且我使用'Canvas',因爲我使用過的教程曾經使用過它。雖然該應用程序可以在'Windows操作系統上運行。所以我認爲這可能與我使用Mac的事實有關。 – Phoenix1355

+1

這很可能是一個時間問題,你需要在適當的時候趕上窗口...... – MadProgrammer

0

試試這個: 遊戲。addKeyListener(本);