- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 Java 编写一个简单的游戏,我需要处理 KeyEvents。不幸的是,我的 Java 知识相当低,我无法让它工作。我在这里做错了什么?我还尝试在从 JPanel
扩展时实现 KeyListener
只是为了看看它是否有效,我什至覆盖了 addNotify()
到 requestFocus()
却什么也没有。其他一切都正常。
以下是应该处理事件的类:
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class BasicRobot extends JComponent{
public static enum FACING{
NORTH, WEST, SOUTH, EAST
}
int xLoc, yLoc;
FACING facing;
int beepers;
public BasicRobot(int xLoc, int yLoc, FACING facing, int beepers){
this.xLoc = xLoc;
this.yLoc = yLoc;
this.facing = facing;
this.beepers = beepers;
World w = new World(this);
addKeyListener();
setFocusable(true);
requestFocusInWindow();
}
public void move(){
switch(facing){
case NORTH:
yLoc++;
break;
case WEST:
xLoc--;
break;
case SOUTH:
yLoc--;
break;
case EAST:
xLoc++;
break;
}
}
public void turnLeft(){
switch(facing){
case NORTH:
facing=FACING.WEST;
break;
case WEST:
facing=FACING.SOUTH;
break;
case SOUTH:
facing=FACING.EAST;
break;
case EAST:
facing=FACING.NORTH;
break;
}
}
void addKeyListener(){
getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "move");
getActionMap().put("move", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
move();
}
});
}
}
这是 World
类(不确定它有多相关,但也许我做了一些非常愚蠢的事情,所以就在这里):
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.border.LineBorder;
public class World extends JPanel implements Runnable{
private final int ROWS=30, COLS=30, BLOCK_SCALE=24, WALL_WIDTH=BLOCK_SCALE/3;
private final Dimension DIMS = new Dimension(COLS*BLOCK_SCALE, ROWS*BLOCK_SCALE);;
private final JFrame frame = new JFrame("World");
private final Polygon NORTH_TRIANGLE = new Polygon(new int[]{5,BLOCK_SCALE/2,BLOCK_SCALE-5}, new int[]{BLOCK_SCALE-3,2,BLOCK_SCALE-3}, 3);
private final Polygon WEST_TRIANGLE = new Polygon(new int[]{1,BLOCK_SCALE-4,BLOCK_SCALE-4}, new int[]{BLOCK_SCALE/2,5,BLOCK_SCALE-5}, 3);
private final Polygon SOUTH_TRIANGLE = new Polygon(new int[]{5,BLOCK_SCALE/2,BLOCK_SCALE-5}, new int[]{3,BLOCK_SCALE-2,3}, 3);
private final Polygon EAST_TRIANGLE = new Polygon(new int[]{4,4,BLOCK_SCALE-1}, new int[]{BLOCK_SCALE-5,5,BLOCK_SCALE/2}, 3);
private final int obsticleMap[][] = new int[COLS][ROWS];
private final Wall obsticles[] = {
new Wall(1, 1, 5, WALL_WIDTH, Wall.ALIGNMENT_HORIZONTAL),
new Wall(1, 1, 5, WALL_WIDTH, Wall.ALIGNMENT_VERTICAL),
new Wall(1, 5, 3, WALL_WIDTH, Wall.ALIGNMENT_HORIZONTAL)
}; //The Wall class is just data nothing of interest there
BasicRobot robot;
public World(BasicRobot robot){
super(null);
this.robot = robot;
initThis();
initFrame();
initObsticleMap();
}
private void initThis(){
setBackground(Color.white);
setSize(DIMS);
setLocation(1, 1);
setBorder(new LineBorder(Color.black));
Thread t = new Thread(this);
t.start();
}
private void initFrame(){
frame.setContentPane(new Container(){
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
g2d.drawRect(0, 0, DIMS.width+1, DIMS.height+1);
g2d.drawLine(0, DIMS.height+2, DIMS.width+1, DIMS.height+2);
g2d.setFont(new Font("Serif", Font.BOLD, 18));
g2d.drawString(String.format("Loc X = %d", robot.xLoc), 20, DIMS.height+22);
g2d.drawString(String.format("Loc Y = %d", robot.yLoc), 20, DIMS.height+42);
g2d.drawString(String.format("Beepers in bag = %d", robot.beepers), 150, DIMS.height+22);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(DIMS.width+2,DIMS.height+50));
frame.setResizable(false);
frame.getContentPane().add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void initObsticleMap(){
for(int i=0; i<obsticles.length; i++){
switch(obsticles[i].alignment){
case Wall.ALIGNMENT_HORIZONTAL:
for(int j=0; j<obsticles[i].span; j++){
obsticleMap[obsticles[i].xLoc+j][obsticles[i].yLoc] = 1;
}
break;
case Wall.ALIGNMENT_VERTICAL:
for(int j=0; j<obsticles[i].span; j++){
obsticleMap[obsticles[i].xLoc][obsticles[i].yLoc+j] = 1;
}
break;
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
drawGrid(g2d);
drawObsticles(g2d);
drawRobot(g2d);
}
private void drawGrid(Graphics2D g){
for(int i=0; i<COLS; i++){
for(int j=0; j<ROWS; j++){
g.drawRect(i*BLOCK_SCALE, j*BLOCK_SCALE, BLOCK_SCALE-1, BLOCK_SCALE-1);
}
}
}
private void drawObsticles(Graphics2D g2d){
for(int i=0; i<obsticles.length; i++){
switch(obsticles[i].alignment){
case Wall.ALIGNMENT_HORIZONTAL:
g2d.fillRoundRect(obsticles[i].xLoc*BLOCK_SCALE-BLOCK_SCALE/2, (ROWS-obsticles[i].yLoc)*BLOCK_SCALE-obsticles[i].width/2, BLOCK_SCALE*obsticles[i].span, obsticles[i].width, 2, 2);
break;
case Wall.ALIGNMENT_VERTICAL:
g2d.fillRoundRect(obsticles[i].xLoc*BLOCK_SCALE-obsticles[i].width/2, (ROWS-obsticles[i].yLoc-obsticles[i].span+1)*BLOCK_SCALE-BLOCK_SCALE/2, obsticles[i].width, BLOCK_SCALE*obsticles[i].span, 2, 2);
break;
}
}
}
private void drawRobot(Graphics2D g){
Polygon translated;
switch(robot.facing){
case NORTH:
translated = new Polygon(NORTH_TRIANGLE.xpoints, NORTH_TRIANGLE.ypoints, 3);
break;
case WEST:
translated = new Polygon(WEST_TRIANGLE.xpoints, WEST_TRIANGLE.ypoints, 3);
break;
case SOUTH:
translated = new Polygon(SOUTH_TRIANGLE.xpoints, SOUTH_TRIANGLE.ypoints, 3);
break;
case EAST:
translated = new Polygon(EAST_TRIANGLE.xpoints, EAST_TRIANGLE.ypoints, 3);
break;
default:
translated = new Polygon(EAST_TRIANGLE.xpoints, EAST_TRIANGLE.ypoints, 3);
}
translated.translate(robot.xLoc*BLOCK_SCALE-BLOCK_SCALE/2, (ROWS-robot.yLoc)*BLOCK_SCALE-BLOCK_SCALE/2);
g.setColor(Color.yellow);
g.fillPolygon(translated);
g.setColor(Color.black);
g.drawPolygon(translated);
}
public void run() {
for(;;){
try{
frame.repaint();
Thread.sleep(40);
}catch(Exception e){e.toString();}
}
}
}
PS:首选方法是使用按键绑定(bind)。谢谢!
最佳答案
getInputMap(WHEN_IN_FOCUSED_WINDOW)
消除任何与焦点相关的问题...addKeyListener()
以便注册按键绑定(bind)。Thread.sleep
事实上,这看起来很可疑......
public void run() {
for(;;){
try{
frame.repaint();
Thread.sleep(40);
}catch(Exception e){e.toString();}
}
}
看来你实际上并没有更新任何东西的状态,它所做的只是占用 CPU 周期,而这些周期可以得到更好的利用......
其他...
因为您从未将 BasicRobot
添加到显示的任何内容(如窗口),所以它永远不会将其监听器注册到事件队列中,这意味着键绑定(bind)永远不会起作用。
您不应该从 JComponent
扩展 BasicRobot
,而应该简单地从任何东西扩展它,这样不会获得任何好处。
相反,将键绑定(bind)注册到World
并从那里调用BasicRobot#move
例如......抱歉,我可能会让你的代码稍微向后,但基本思想就在那里......
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class World extends JPanel implements Runnable {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
new World(new BasicRobot(0, 0, BasicRobot.FACING.SOUTH, 100));
}
});
}
private final int ROWS = 30, COLS = 30, BLOCK_SCALE = 24, WALL_WIDTH = BLOCK_SCALE / 3;
private final Dimension DIMS = new Dimension(COLS * BLOCK_SCALE, ROWS * BLOCK_SCALE);
private final JFrame frame = new JFrame("World");
private final Polygon NORTH_TRIANGLE = new Polygon(new int[]{5, BLOCK_SCALE / 2, BLOCK_SCALE - 5}, new int[]{BLOCK_SCALE - 3, 2, BLOCK_SCALE - 3}, 3);
private final Polygon WEST_TRIANGLE = new Polygon(new int[]{1, BLOCK_SCALE - 4, BLOCK_SCALE - 4}, new int[]{BLOCK_SCALE / 2, 5, BLOCK_SCALE - 5}, 3);
private final Polygon SOUTH_TRIANGLE = new Polygon(new int[]{5, BLOCK_SCALE / 2, BLOCK_SCALE - 5}, new int[]{3, BLOCK_SCALE - 2, 3}, 3);
private final Polygon EAST_TRIANGLE = new Polygon(new int[]{4, 4, BLOCK_SCALE - 1}, new int[]{BLOCK_SCALE - 5, 5, BLOCK_SCALE / 2}, 3);
private final int obsticleMap[][] = new int[COLS][ROWS];
// private final Wall obsticles[] = {
// new Wall(1, 1, 5, WALL_WIDTH, Wall.ALIGNMENT_HORIZONTAL),
// new Wall(1, 1, 5, WALL_WIDTH, Wall.ALIGNMENT_VERTICAL),
// new Wall(1, 5, 3, WALL_WIDTH, Wall.ALIGNMENT_HORIZONTAL)
// }; //The Wall class is just data nothing of interest there
BasicRobot robot;
public World(BasicRobot robot) {
super(null);
this.robot = robot;
initThis();
initFrame();
initObsticleMap();
}
private void initThis() {
setBackground(Color.white);
setSize(DIMS);
setLocation(1, 1);
setBorder(new LineBorder(Color.black));
// Thread t = new Thread(this);
// t.start();
initKeyBindings();
}
private void initFrame() {
frame.setContentPane(new Container() {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
g2d.drawRect(0, 0, DIMS.width + 1, DIMS.height + 1);
g2d.drawLine(0, DIMS.height + 2, DIMS.width + 1, DIMS.height + 2);
g2d.setFont(new Font("Serif", Font.BOLD, 18));
g2d.drawString(String.format("Loc X = %d", robot.xLoc), 20, DIMS.height + 22);
g2d.drawString(String.format("Loc Y = %d", robot.yLoc), 20, DIMS.height + 42);
g2d.drawString(String.format("Beepers in bag = %d", robot.beepers), 150, DIMS.height + 22);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(DIMS.width + 2, DIMS.height + 50));
frame.setResizable(false);
frame.getContentPane().add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void initObsticleMap() {
// for (int i = 0; i < obsticles.length; i++) {
// switch (obsticles[i].alignment) {
// case Wall.ALIGNMENT_HORIZONTAL:
// for (int j = 0; j < obsticles[i].span; j++) {
// obsticleMap[obsticles[i].xLoc + j][obsticles[i].yLoc] = 1;
// }
// break;
// case Wall.ALIGNMENT_VERTICAL:
// for (int j = 0; j < obsticles[i].span; j++) {
// obsticleMap[obsticles[i].xLoc][obsticles[i].yLoc + j] = 1;
// }
// break;
// }
// }
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
drawGrid(g2d);
drawObsticles(g2d);
drawRobot(g2d);
}
private void drawGrid(Graphics2D g) {
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
g.drawRect(i * BLOCK_SCALE, j * BLOCK_SCALE, BLOCK_SCALE - 1, BLOCK_SCALE - 1);
}
}
}
private void drawObsticles(Graphics2D g2d) {
// for (int i = 0; i < obsticles.length; i++) {
// switch (obsticles[i].alignment) {
// case Wall.ALIGNMENT_HORIZONTAL:
// g2d.fillRoundRect(obsticles[i].xLoc * BLOCK_SCALE - BLOCK_SCALE / 2, (ROWS - obsticles[i].yLoc) * BLOCK_SCALE - obsticles[i].width / 2, BLOCK_SCALE * obsticles[i].span, obsticles[i].width, 2, 2);
// break;
// case Wall.ALIGNMENT_VERTICAL:
// g2d.fillRoundRect(obsticles[i].xLoc * BLOCK_SCALE - obsticles[i].width / 2, (ROWS - obsticles[i].yLoc - obsticles[i].span + 1) * BLOCK_SCALE - BLOCK_SCALE / 2, obsticles[i].width, BLOCK_SCALE * obsticles[i].span, 2, 2);
// break;
// }
// }
}
private void drawRobot(Graphics2D g) {
Polygon translated;
switch (robot.facing) {
case NORTH:
translated = new Polygon(NORTH_TRIANGLE.xpoints, NORTH_TRIANGLE.ypoints, 3);
break;
case WEST:
translated = new Polygon(WEST_TRIANGLE.xpoints, WEST_TRIANGLE.ypoints, 3);
break;
case SOUTH:
translated = new Polygon(SOUTH_TRIANGLE.xpoints, SOUTH_TRIANGLE.ypoints, 3);
break;
case EAST:
translated = new Polygon(EAST_TRIANGLE.xpoints, EAST_TRIANGLE.ypoints, 3);
break;
default:
translated = new Polygon(EAST_TRIANGLE.xpoints, EAST_TRIANGLE.ypoints, 3);
}
translated.translate(robot.xLoc * BLOCK_SCALE - BLOCK_SCALE / 2, (ROWS - robot.yLoc) * BLOCK_SCALE - BLOCK_SCALE / 2);
g.setColor(Color.yellow);
g.fillPolygon(translated);
g.setColor(Color.black);
g.drawPolygon(translated);
}
public void run() {
// for (;;) {
// try {
// frame.repaint();
// Thread.sleep(40);
// } catch (Exception e) {
// e.toString();
// }
// }
}
public void initKeyBindings() {
getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "move");
getActionMap().put("move", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("move");
robot.move();
repaint();
}
});
}
public static class BasicRobot {
public static enum FACING {
NORTH, WEST, SOUTH, EAST
}
int xLoc, yLoc;
FACING facing;
int beepers;
public BasicRobot(int xLoc, int yLoc, FACING facing, int beepers) {
this.xLoc = xLoc;
this.yLoc = yLoc;
this.facing = facing;
this.beepers = beepers;
}
public void move() {
// try {
// Thread.sleep(300);
// } catch (Exception e) {
// e.toString();
// }
switch (facing) {
case NORTH:
yLoc++;
break;
case WEST:
xLoc--;
break;
case SOUTH:
yLoc--;
break;
case EAST:
xLoc++;
break;
}
}
public void turnLeft() {
// try {
// Thread.sleep(300);
// } catch (Exception e) {
// e.toString();
// }
switch (facing) {
case NORTH:
facing = FACING.WEST;
break;
case WEST:
facing = FACING.SOUTH;
break;
case SOUTH:
facing = FACING.EAST;
break;
case EAST:
facing = FACING.NORTH;
break;
}
}
}
}
已更新...
这是个人的事情,但我更喜欢使用 KeyEvent
虚拟键代码......
getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "move");
否则我认为你可能需要使用(但我不是 100% 确定)
getInputMap(WHEN_IN_FOCUSED_WINDOW).put("pressed UP, "move");
关于java - 处理 Java 按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467906/
我正在尝试将 WPF CodeBehid 事件(如 Event、Handler、EventSetter)转换为 MVVM 模式。我不允许使用 System.Windows.Controls,因为我使用
我可能误解了 Backbone 中的事件系统,但是当我尝试以下代码时什么也没有发生。当我向 Backbone.Events 扩展对象添加新属性时,它不应该触发某种更改、更新或重置事件吗?就像模型一样吗
我遇到了一个简单的问题,就是无法弄清楚为什么它不起作用。我有一个子组件“app-buttons”,其中我有一个输入字段,我想听,所以我可以根据输入值过滤列表。 如果我将输入放在我有列表的根组件中,一切
System.Timers.Timer 的 Elapsed 事件实际上与 System.Windows.Forms.Timer 的 Tick 事件相同吗? 在特定情况下使用其中一种比使用另一种有优势吗
嗨,这个 javascript 代码段是什么意思。(evt) 部分是如此令人困惑.. evt 不是 bool 值。这个怎么运作? function checkIt(evt) { evt
我正在使用jquery full calendar我试图在事件被删除时保存它。 $('calendar').fullCalendar ({
我有两个链接的鼠标事件: $('body > form').on("mousedown", function(e){ //Do stuff }).on("mouseup", function(
这是我的代码: $( '#Example' ).on( "keypress", function( keyEvent ) { if ( keyEvent.which != 44 ) {
我尝试了 dragOver 事件处理程序,但它没有正常工作。 我正在研究钢琴,我希望能够弹奏音符,即使那个键上没有发生鼠标按下。 是否有事件处理程序? 下面是我正在制作的钢琴的图片。 最佳答案 您应该
当悬停在相邻文本上时,我需要使隐藏按钮可见。这是通过 onMouseEnter 和 onMouseLeave 事件完成的。但是当点击另外的文本时,我需要使按钮完全可见并停止 onMouseLeave
我有ul标签内 div标签。我申请了mouseup事件 div标记和 click事件 ul标签。 问题 每当我点击 ul标签,然后都是 mouseup和 click事件被触发。 我想要的是当我点击 u
我是 Javascript 和 jQuery 的新手,所以我有一个非常愚蠢的疑问,请耐心等待 $(document).click(function () { alert("!"); v
我有一个邮政编码解析器,我正在使用 keyup 事件处理程序来跟踪输入长度何时达到 5,然后查询服务器以解析邮政编码。但是我想防止脚本被不必要地调用,所以我想知道是否有一种方法可以跟踪 keydown
使用事件 API,我有以下代码来发布带有事件照片的事件 $facebook = new Facebook(array( "appId" => "XXX", "se
首次加载 Microsoft Word 时,既不会触发 NewDocument 事件也不会触发 DocumentOpen 事件。当 Word 实例已打开并打开新文档或现有文档时,这些事件会正常触发。
我发现了很多相关问题(这里和其他地方),但还没有具体找到这个问题。 我正在尝试监听箭头键 (37-40) 的按键事件,但是当以特定顺序使用箭头键时,后续箭头不会生成“按键”事件。 例子: http:/
给定的 HTML: 和 JavaScript 的: var $test = $('#test'); $test.on('keydown', function(event) { if (eve
我是 Node.js 的新手,希望使用流运行程序。对于其他程序,我必须同时启动一个服务器(mongodb、redis 等),但我不知道我是否应该用这个运行一个服务器。请让我知道我哪里出了问题以及如何纠
我正在尝试使用 Swift 和 Cocoa 创建一个适用于 OS X 的应用程序。我希望应用程序能够响应关键事件,而不将焦点放在文本字段上/文本字段中。我在 Xcode 中创建了一个带有 Storyb
我有以下代码: (function(w,d,s,l,i){ w[l]=w[l]||[];w[l].push({
我是一名优秀的程序员,十分优秀!