如何在Java中以全屏独占模式处理键盘和鼠标的事件?
2022-09-04 06:25:33
在被动渲染模式下,可以使用 和 接口来处理来自用户的事件。KeyListener
ActionListener
在全屏模式下处理事件的正确方法是什么?请扩展此框架,为鼠标单击和按键事件提供实现,请不要膨胀您的示例(该示例启动全屏独占模式,使用 a 在窗口中更新图形):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();
}
}