键盘事件 java
Keyboard events java
我最近开始学习java.I想做一个类似https://sites.google.com/site/millseagles/home/Games/multiplayer/tron的游戏
我曾经使用一个简单的图形库在 C++ 中制作过它。我有图形部分我打算使用小图像并使用 http://horstmann.com/sjsu/graphics/ 这个基本图形 lib.I 无法弄清楚键盘输入我想要它所以如果你按箭头图片添加一个小绿色方块(我有一个 green.png)。我不知道如何使用键盘 listeners.I 得到所有这些 errors.I 只需要一个简单的库,我可以说 getKey() 或其他东西,我可以使用 if() 找出 action.this 是我 have.I 搞乱了关键事件但不理解的代码。
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 你可以指点我一个更适合初学者的库,这将是很棒的.
您只需将侦听器添加到某些东西(例如正在玩游戏的window)。
我会给你一个例子,我们将简单地显示被敲击的键的代码。
这是您生成界面的class:
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);
}
}
这是 class 我们有监听器的地方:
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 */ }
}
注:当运行程序按键时看到的文字出现在window.
我没有绕过你使用的库,但我使用了最流行的 swing (tutorial)
我最近开始学习java.I想做一个类似https://sites.google.com/site/millseagles/home/Games/multiplayer/tron的游戏 我曾经使用一个简单的图形库在 C++ 中制作过它。我有图形部分我打算使用小图像并使用 http://horstmann.com/sjsu/graphics/ 这个基本图形 lib.I 无法弄清楚键盘输入我想要它所以如果你按箭头图片添加一个小绿色方块(我有一个 green.png)。我不知道如何使用键盘 listeners.I 得到所有这些 errors.I 只需要一个简单的库,我可以说 getKey() 或其他东西,我可以使用 if() 找出 action.this 是我 have.I 搞乱了关键事件但不理解的代码。
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 你可以指点我一个更适合初学者的库,这将是很棒的.
您只需将侦听器添加到某些东西(例如正在玩游戏的window)。
我会给你一个例子,我们将简单地显示被敲击的键的代码。
这是您生成界面的class:
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);
}
}
这是 class 我们有监听器的地方:
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 */ }
}
注:当运行程序按键时看到的文字出现在window.
我没有绕过你使用的库,但我使用了最流行的 swing (tutorial)