如何在java中以全屏独占模式处理来自键盘和鼠标的事件?
在被动渲染模式下,可以使用KeyListener
和ActionListener
接口来处理来自用户的事件。
全屏模式下事件处理的正确方法是什么?请扩展此框架,提供鼠标单击和按键事件的实现,请不要膨胀您的示例(该示例启动全屏独占模式,使用 Timer
更新窗口中的图形):
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它看起来像如何使用键绑定 和如何编写鼠标监听器正常工作在 全屏独占模式。
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.
正如此处所建议的,Mac OS X 用户可能对全屏应用程序有不同的期望。 此处显示的另一种方法依赖于
com.apple.eawt
类“提供了一种简单的方法来实现本机功能,以在 Mac OS X 上微调 Java 应用程序”。FullScreenUtilities
方法setWindowCanFullScreen()
启用该功能,Application
方法requestToggleFullScreen()
动态更改设置。请注意版本之间的展开图标有何不同。Mac OS 10.9,小牛队:
Mac OS 10.10,优胜美地:
Mac OS X 10.11、El Capitan:
As suggested here, Mac OS X users may have different user expectations for full screen applications. An alternative approach, shown here, relies on
com.apple.eawt
classes that "provide a simple way to implement native features to fine tune Java applications on Mac OS X." TheFullScreenUtilities
methodsetWindowCanFullScreen()
enables the feature, and theApplication
methodrequestToggleFullScreen()
changes the setting dynamically. Note how the expand icon differs among versions.Mac OS 10.9, Mavericks:
Mac OS 10.10, Yosemite:
Mac OS X 10.11, El Capitan: