如何在java中以全屏独占模式处理键盘和鼠标中的事件? [英] How to handle events from keyboard and mouse in full screen exclusive mode in java?
问题描述
在被动渲染模式下,可以使用 KeyListener
和 ActionListener
接口来处理来自用户的事件。
In passive rendering mode one can use KeyListener
and ActionListener
interfaces to handle events from user.
全屏模式下事件处理的正确方法是什么?请扩展此骨架,提供鼠标点击和按键事件的实现,请不要膨胀您的示例(示例启动全屏独占模式,使用计时器
更新图形窗口):
What is the correct way of event handling in full screen mode? Please extend this skeleton providing implementation for mouse click and key press events, please don't bloat your example (the example starts full screen exclusive mode, using a Timer
to update graphics in window):
import java.applet.Applet;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.Timer;
public class applet extends Applet
{
Timer timer;
JFrame frame;
DisplayMode[] displayModes = new DisplayMode[] {
new DisplayMode(1280, 800, 32, 60)
};
BufferStrategy bufferStrategy;
Rectangle bounds;
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public void init()
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); //displays, fonts, color shemes...
GraphicsDevice device = env.getDefaultScreenDevice(); //for one-display systems
setIgnoreRepaint(true);
GraphicsConfiguration gc = device.getDefaultConfiguration();
frame = new JFrame(gc);
device.setFullScreenWindow(frame);
if (device.isDisplayChangeSupported())
device.setDisplayMode(displayModes[0]);
frame.createBufferStrategy(2);
bufferStrategy = frame.getBufferStrategy();
timer = new Timer(1000 / 50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Graphics2D g = null;
try {
g = (Graphics2D) bufferStrategy.getDrawGraphics();
render(g);
} finally {
g.dispose();
}
bufferStrategy.show();
}
});
}
private void render(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, bounds.width, bounds.height);
}
public void start()
{
timer.start();
}
public void stop()
{
timer.stop();
}
}
推荐答案
它看起来像 如何使用键绑定 和 如何编写鼠标侦听器 在 全屏独占模式 中正常工作。
It looks like the usual approaches shown in How to Use Key Bindings and How to Write a Mouse Listener work correctly in Full-Screen Exclusive Mode.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/** @see http://stackoverflow.com/questions/7456227 */
public class FullScreenTest extends JPanel {
private static final String EXIT = "Exit";
private JFrame f = new JFrame("FullScreenTest");
private Action exit = new AbstractAction(EXIT) {
@Override
public void actionPerformed(ActionEvent e) {
f.dispatchEvent(new WindowEvent(
f, WindowEvent.WINDOW_CLOSING));
}
};
private JButton b = new JButton(exit);
public FullScreenTest() {
this.add(b);
f.getRootPane().setDefaultButton(b);
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT);
this.getActionMap().put(EXIT, exit);
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
FullScreenTest.this.setToolTipText(
"("+ e.getX() + "," + e.getY() + ")");
}
});
}
private void display() {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice dev = env.getDefaultScreenDevice();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.darkGray);
f.setResizable(false);
f.setUndecorated(true);
f.add(this);
f.pack();
dev.setFullScreenWindow(f);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new FullScreenTest().display();
}
});
}
}
这篇关于如何在java中以全屏独占模式处理键盘和鼠标中的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!