java 键盘事件,键盘事件Java

I have recently started learning java.I want to make a game like https://sites.google.com/site/millseagles/home/Games/multiplayer/tron

I have made it in c++ once using a simple graphics lib. I have the graphics part down i plan to use small images and use http://horstmann.com/sjsu/graphics/ this basic graphics lib.I can't figure out keyboard input i want it so if you press an arrow the picture adds a small green square(I have a green.png).I can't figure out to use keyboard listeners.I get all these errors.I just need a simple lib that i can say getKey() or something and i can use if() to figure out the action.this is the code I have.I was messing with the key event but don't understand it.

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

}

}

}

I know this may be messy.I have tested my code it works it just breaks when i try to implement keyboard events.If you can point me to a much more beginner friendly lib that would be great.

解决方案

You simply have to add the listener to something (e.g. the window where the game is being played).

I will give you an example, where we will simply display the code of the key being stroked.

This is the class where you produce the interface:

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);

}

}

And this is the class where we have the listener:

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 */ }

}

Note: when running the program press the keys to see the text appearing on the window.

I didn't get around the library you were using, but I used the most popular one called swing (tutorial)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值