2015-05-24 188 views
-1

我最近開始學習java.I想製作一個類似https://sites.google.com/site/millseagles/home/Games/multiplayer/tron 的遊戲我用C++製作過一次使用簡單的圖形庫。我有圖形部分下來,我打算使用小圖像,並使用http://horstmann.com/sjsu/graphics/這個基本的圖形lib.I無法弄清楚我想要的鍵盤輸入,所以如果你按下箭頭圖片添加一個小的綠色方塊(我有一個綠色的。 PNG)。我無法弄清楚使用鍵盤監聽器。我得到所有這些錯誤。我只需要一個簡單的庫,我可以說getKey()或者什麼,我可以使用if()來找出action.this是我有我的代碼。我搞砸了關鍵事件,但不明白。鍵盤事件java

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.*; 

public class game implements KeyListener 
{ 

public void keyReleased(KeyEvent e){} 

public void keyTyped(KeyEvent e){} 
public game()//snake like game 
{ 

} 
public void test() 
{ 

    int x=30,y=30;//middle total 60x60 

    tile[] map=new tile[3600];//tile is a class i made that is a picture and some int and bool using the simple lib i linked 60 by 60 tiles 
    for(int i=0;i<3600;i++) 
    { 
     map[i]=new tile(); 
    } 


} 

public void keyPressed(KeyEvent e)//this does not work i want it to work when a key is clicked 
{ 
    while(x>0)//this part works when it is not in the keypressed function 
     { 

     map[(y*60)+x].load(4);//4 refrences a green rectangle image 
     map[(y*60)+x].draw(x,y,10);//draw it based on x and y 10 pixels sized tiles 
     x--;//make a line going left 



     } 

} 
} 

我知道這可能是messy.I過我的代碼,它工作時,我試圖實現鍵盤events.If你可以點我到一個更友好的初學者LIB將是巨大的,它只是破壞。

+1

做一個'AbstractAction',增加了圖像和*綁定*給你喜歡的關鍵。綁定更合適和簡潔。請參閱[如何使用鍵綁定](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)。 – ChiefTwoPencils

回答

0

您只需將偵聽器添加到某些內容(例如正在播放遊戲的窗口)。

我會給你一個例子,我們將簡單地顯示被撫摸的鍵的代碼。

這是類,你產生的界面:

import java.awt.Dimension; 
import javax.swing.JFrame; 

public class Game { 

    public static void main(String[] args) { 
     /* Creating a window (300x400) */ 
     JFrame frame = new JFrame("Add your own title"); 
     frame.setPreferredSize(new Dimension(300, 400)); 

     /* This is the part where we add the keyListener (notice that I am also sending 
     * this window as a parameter so that the listener can modify it)*/ 
     frame.addKeyListener(new ArrowListener(frame)); 

     /* Making the window visible */ 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

這是類,我們有聽衆:

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


public class ArrowListener implements KeyListener { 
    /* We keep the window as an instance variable so we can modify it once the event is triggered */ 
    JFrame frame; 

    /* This is the constructor */ 
    public ArrowListener(JFrame j) { 
     frame = j; 
    } 

    /* This is where the magic happens */ 
    public void keyPressed(KeyEvent e) { 
     /* Modify this with what you actually want it to do */ 

     /* We clear the panel so we can add new text without any other text behind it */ 
     frame.getContentPane().removeAll(); 

     /* We add some text that actually shows the keyCode (left arrow = 37, top = 38, right = 39, bottom = 40) */ 
     frame.add(new JLabel("Key Code #" + String.valueOf(e.getKeyCode()))); 

     /* Redrawing the window */ 
     frame.revalidate(); 
    } 

    /* These two are part of the contract we made when we decided to 
    * implement the KeyListener */ 
    public void keyTyped(KeyEvent e) { /* Do nothing */ } 
    public void keyReleased(KeyEvent e) { /* Do nothing */ } 
} 

注意:在運行程序時,按確認鍵查看文字出現在窗口上。

我沒有得到你使用的庫周圍,但我用了最流行的一種叫做擺動(tutorial

+0

謝謝。如果它很受歡迎,我會用swing。它對初學者友好嗎? –