当前位置: 技术问答>java相关
热心的masterz(),applet的键盘事件又遇到新问题请进来看看,谢谢
来源: 互联网 发布时间:2015-01-20
本文导语: 花了2周时间做了一个俄罗斯方块的applet(完成度较高),现放在 http://www.cnbole.com/ggworld/test/eluosi/eluosi.html 遇到的问题是:游戏当中按S键暂停,切换到别的应用程序等待一段时间后(等待的时间越长错误发生的概率越高),再...
花了2周时间做了一个俄罗斯方块的applet(完成度较高),现放在
http://www.cnbole.com/ggworld/test/eluosi/eluosi.html
遇到的问题是:游戏当中按S键暂停,切换到别的应用程序等待一段时间后(等待的时间越长错误发生的概率越高),再返回俄罗斯方块的页面.此时再按S键无效,无法继续游戏,只有刷新页面重新开始,不知是何原因?
如须查看代码,请留mail,谢谢.
http://www.cnbole.com/ggworld/test/eluosi/eluosi.html
遇到的问题是:游戏当中按S键暂停,切换到别的应用程序等待一段时间后(等待的时间越长错误发生的概率越高),再返回俄罗斯方块的页面.此时再按S键无效,无法继续游戏,只有刷新页面重新开始,不知是何原因?
如须查看代码,请留mail,谢谢.
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SketchPanel extends JPanel
implements KeyListener
{
InputMap mapaEntrada;
ActionMap mapaAction ;
HandlerKey hkLeft = new HandlerKey("LeftKey",KeyEvent.VK_LEFT);
HandlerKey hkRight = new HandlerKey("RightKey",KeyEvent.VK_RIGHT);
HandlerKey hkPageUp = new HandlerKey("PageUp",KeyEvent.VK_PAGE_UP);
HandlerKey hkPageDown = new HandlerKey("PageDown",KeyEvent.VK_PAGE_DOWN);
public SketchPanel()
{ addKeyListener(this);
mapaEntrada= getInputMap(WHEN_IN_FOCUSED_WINDOW);
mapaAction = getActionMap();
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"LeftKey");
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"RightKey");
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0),"PageUp");
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,0),"PageDown");
mapaAction.put("LeftKey",hkLeft);
mapaAction.put("RightKey",hkRight);
mapaAction.put("PageUp",hkPageUp);
mapaAction.put("PageDown",hkPageDown);
requestDefaultFocus();
}
public void keyPressed(KeyEvent evt)
{ int keyCode = evt.getKeyCode();
int d;
if (evt.isShiftDown())
d = 5;
else
d = 1;
if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
else if (keyCode == KeyEvent.VK_UP) add(0, -d);
else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
}
public void keyReleased(KeyEvent evt)
{}
public void keyTyped(KeyEvent evt)
{ char keyChar = evt.getKeyChar();
int d;
if (Character.isUpperCase(keyChar))
{ d = 5;
keyChar = Character.toLowerCase(keyChar);
}
else
d = 1;
if (keyChar == 'h') add(-d, 0);
else if (keyChar == 'l') add(d, 0);
else if (keyChar == 'k') add(0, -d);
else if (keyChar == 'j') add(0, d);
}
public boolean isFocusTraversable() { return true; }
public void add(int dx, int dy)
{ end.x += dx;
end.y += dy;
Graphics g = getGraphics();
g.drawLine(start.x, start.y, end.x, end.y);
g.dispose();
start.x = end.x;
start.y = end.y;
}
private Point start = new Point(50, 50);
private Point end = new Point(50, 50);
}
public class Sketch extends JApplet implements FocusListener, WindowListener
{
private Frame parentFrame;
public Sketch()
{
addFocusListener(this);
parentFrame = JOptionPane.getFrameForComponent(this);
parentFrame.addWindowListener(this);
}
public void jsForceFocus()
{
WindowEvent e = new WindowEvent(parentFrame,
WindowEvent.WINDOW_ACTIVATED);
parentFrame.dispatchEvent(e);
}
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SketchPanel());
FocusManager.getCurrentManager().focusNextComponent(contentPane);
}
public void focusLost(FocusEvent e)
{
FocusManager.getCurrentManager().focusNextComponent(getContentPane());
}
public void focusGained(FocusEvent e)
{
FocusManager.getCurrentManager().focusNextComponent(getContentPane());
}
public void windowOpened(WindowEvent e) {
//System.out.println("windowOpened");
}
public void windowClosing(WindowEvent e) {
//System.out.println("windowClosing");
}
public void windowClosed(WindowEvent e) {
//System.out.println("windowClosed");
}
public void windowIconified(WindowEvent e) {
//System.out.println("windowIconified");
}
public void windowDeiconified(WindowEvent e) {
//System.out.println("windowDeiconified");
}
public void windowActivated(WindowEvent e) {
//System.out.println("windowActivated");
if (parentFrame.getFocusOwner() != null ) {
parentFrame.getFocusOwner().requestFocus();
}
}
public void windowDeactivated(WindowEvent e) {
//System.out.println("windowDeactivated");
}
}
class HandlerKey extends AbstractAction
{//LEFT,RIGHT,PAGEUP,PAGE DOWN will travel the images
int nKeyCode=0;
public HandlerKey(String s)
{
super(s);
}
public HandlerKey(String s,int keycode)
{
super(s);
nKeyCode=keycode;
}
public void actionPerformed(ActionEvent e)
{
System.out.println(nKeyCode);
}
}//end of HandlerKey
import java.awt.event.*;
import javax.swing.*;
class SketchPanel extends JPanel
implements KeyListener
{
InputMap mapaEntrada;
ActionMap mapaAction ;
HandlerKey hkLeft = new HandlerKey("LeftKey",KeyEvent.VK_LEFT);
HandlerKey hkRight = new HandlerKey("RightKey",KeyEvent.VK_RIGHT);
HandlerKey hkPageUp = new HandlerKey("PageUp",KeyEvent.VK_PAGE_UP);
HandlerKey hkPageDown = new HandlerKey("PageDown",KeyEvent.VK_PAGE_DOWN);
public SketchPanel()
{ addKeyListener(this);
mapaEntrada= getInputMap(WHEN_IN_FOCUSED_WINDOW);
mapaAction = getActionMap();
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"LeftKey");
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"RightKey");
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0),"PageUp");
mapaEntrada.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,0),"PageDown");
mapaAction.put("LeftKey",hkLeft);
mapaAction.put("RightKey",hkRight);
mapaAction.put("PageUp",hkPageUp);
mapaAction.put("PageDown",hkPageDown);
requestDefaultFocus();
}
public void keyPressed(KeyEvent evt)
{ int keyCode = evt.getKeyCode();
int d;
if (evt.isShiftDown())
d = 5;
else
d = 1;
if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
else if (keyCode == KeyEvent.VK_UP) add(0, -d);
else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
}
public void keyReleased(KeyEvent evt)
{}
public void keyTyped(KeyEvent evt)
{ char keyChar = evt.getKeyChar();
int d;
if (Character.isUpperCase(keyChar))
{ d = 5;
keyChar = Character.toLowerCase(keyChar);
}
else
d = 1;
if (keyChar == 'h') add(-d, 0);
else if (keyChar == 'l') add(d, 0);
else if (keyChar == 'k') add(0, -d);
else if (keyChar == 'j') add(0, d);
}
public boolean isFocusTraversable() { return true; }
public void add(int dx, int dy)
{ end.x += dx;
end.y += dy;
Graphics g = getGraphics();
g.drawLine(start.x, start.y, end.x, end.y);
g.dispose();
start.x = end.x;
start.y = end.y;
}
private Point start = new Point(50, 50);
private Point end = new Point(50, 50);
}
public class Sketch extends JApplet implements FocusListener, WindowListener
{
private Frame parentFrame;
public Sketch()
{
addFocusListener(this);
parentFrame = JOptionPane.getFrameForComponent(this);
parentFrame.addWindowListener(this);
}
public void jsForceFocus()
{
WindowEvent e = new WindowEvent(parentFrame,
WindowEvent.WINDOW_ACTIVATED);
parentFrame.dispatchEvent(e);
}
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SketchPanel());
FocusManager.getCurrentManager().focusNextComponent(contentPane);
}
public void focusLost(FocusEvent e)
{
FocusManager.getCurrentManager().focusNextComponent(getContentPane());
}
public void focusGained(FocusEvent e)
{
FocusManager.getCurrentManager().focusNextComponent(getContentPane());
}
public void windowOpened(WindowEvent e) {
//System.out.println("windowOpened");
}
public void windowClosing(WindowEvent e) {
//System.out.println("windowClosing");
}
public void windowClosed(WindowEvent e) {
//System.out.println("windowClosed");
}
public void windowIconified(WindowEvent e) {
//System.out.println("windowIconified");
}
public void windowDeiconified(WindowEvent e) {
//System.out.println("windowDeiconified");
}
public void windowActivated(WindowEvent e) {
//System.out.println("windowActivated");
if (parentFrame.getFocusOwner() != null ) {
parentFrame.getFocusOwner().requestFocus();
}
}
public void windowDeactivated(WindowEvent e) {
//System.out.println("windowDeactivated");
}
}
class HandlerKey extends AbstractAction
{//LEFT,RIGHT,PAGEUP,PAGE DOWN will travel the images
int nKeyCode=0;
public HandlerKey(String s)
{
super(s);
}
public HandlerKey(String s,int keycode)
{
super(s);
nKeyCode=keycode;
}
public void actionPerformed(ActionEvent e)
{
System.out.println(nKeyCode);
}
}//end of HandlerKey
|
function focusToApplet ()
{
// This pulls focus away from the applet so the next stuffcan
// put it back. It seems without this every second try doesnot
// restore focus to the applet
hide.xx.focus();
document.TestApplet.jsForceFocus();
document.TestApplet.requestFocus();
}
window.onfocus = focusToApplet;
something here
|
要把IE窗口的大小固定为applet的大小,你用JS开一个新窗口,在里面指定不就行了……
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。