2015-05-24 246 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。它对初学者友好吗? –