Java 全屏独家模式键输入,只需一个窗口
我正在使用全屏独家制作一个小游戏,我需要能够接收玩家的键盘输入 在我的程序中,我有一个设置为全屏独占的窗口和一个渲染循环 窗口创建:Java 全屏独家模式键输入,只需一个窗口,java,window,awt,fullscreen,Java,Window,Awt,Fullscreen,我正在使用全屏独家制作一个小游戏,我需要能够接收玩家的键盘输入 在我的程序中,我有一个设置为全屏独占的窗口和一个渲染循环 窗口创建: private void initialize() { //This is used for my game loop... running = true; //Create the instance variable 'window' here. window = new Window(null); //Ignoring
private void initialize() {
//This is used for my game loop...
running = true;
//Create the instance variable 'window' here.
window = new Window(null);
//Ignoring OS paint requests...
window.setIgnoreRepaint(true);
//Set the window to full screen exclusive.
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);
...
游戏循环:
private void loop() {
Graphics graphics = window.getGraphics();
graphics.setColor(Color.CYAN);
graphics.fillRect(0, 0, 1920, 1080);
graphics.dispose();
}
我的进口:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
现在,这个很好用。正如它应该的那样。它在我的屏幕上呈现一个青色的矩形,但是,我需要能够检测到用户何时点击按键,例如点击Escape
关闭程序等等。我不知道如何操作:(
我已尝试在我的窗口中添加键侦听器
(不起作用)。
我曾尝试在我的窗口中添加JPanel
,并在其中添加一个侦听器(也不起作用)。
我试着要求专注于我的JPanel
,并做了上面同样的事情。
我试着用一个KeyListener创建一个JFrame
,然后将它传递到我的窗口的构造函数中。
我已经尝试过用键绑定将相同的JFrame
传递到我的窗口
,而不是一个键侦听器
显然,上面的这些都不起作用。(没有抛出错误,当我按下一个键或使用System.exit(int);
退出程序时,我根本无法让程序在我的sysout
s中输出文本。)我已经从上面的代码中删除了所有不适用于我的内容;我目前有一个窗口和一个游戏循环。如果有任何其他方法可以让我在全屏独家模式下获得按键输入,请通知我。(我觉得有一种专门用于全屏独家模式的常规方法,但我还没有找到。)或者,如果你认为有一种方法可以得到我尝试过的方法之一,(也许你认为我做错了什么),请告诉我。(我现在有点绝望了).我认为击键的默认事件处理方式是只能从可聚焦组件(即文本字段)获取击键。但是,引用此项,可以尝试添加自定义的KeyEventDispatcher
(我认为这是AWT的基本事件处理方式)对于KeyboardFocusManager
,我认为击键的默认事件处理是只能从可聚焦组件(即文本字段)获取它们。但是,参照此,您可以尝试使用添加自定义KeyEventDispatcher
(我认为这是AWT的底层事件处理)使用键绑定的KeyboardFocusManager
示例。非常简单,还演示了DisplayMode
的使用,如果您愿意,但一旦它运行,只需按住空格,它就会更新,松开,就会更新。双击关闭;)
导入java.awt.DisplayMode;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.GraphicsDevice;
导入java.awt.GraphicsEnvironment;
导入java.awt.event.ActionEvent;
导入java.awt.event.KeyEvent;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.AbstractAction;
导入javax.swing.ActionMap;
导入javax.swing.InputMap;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.KeyStroke;
导入javax.swing.SwingUtilities;
公开课考试{
公共静态void main(字符串[]args){
JFrame f=新JFrame(“测试”);
f、 未装饰的设置(真实);
f、 添加(新的TestPane());
f、 可设置大小(假);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice设备=GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if(device.isFullScreenSupported()){
设备。设置完整屏幕窗口(f);
if(device.isDisplayChangeSupported()){
试一试{
列表匹配模式=新阵列列表(25);
DisplayMode[]modes=device.getDisplayModes();
用于(显示模式:模式){
if(mode.getWidth()==1280&&mode.getHeight()==720){
匹配模式。添加(模式);
}
}
如果(!matchingModes.isEmpty()){
用于(显示模式:匹配模式){
试一试{
设置显示模式(模式);
System.out.println(mode.getWidth()+“x”+mode.getHeight()+“”+mode.getBitDepth()+“@”+mode.getRefreshRate());
打破
}捕获(例外e){
e、 printStackTrace();
}
}
}否则{
System.err.println(!!没有可用的匹配模式);
}
}捕获(例外e){
e、 printStackTrace();
}
}否则{
System.err.println(“不支持更改显示模式”);
}
}否则{
System.err.println(“不支持全屏”);
}
}
公共静态类TestPane扩展了JPanel{
私有布尔间隔=假;
公共测试窗格(){
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
requestFocusInWindow(真);
如果(如getClickCount()==2){
SwingUtilities.windowForComponent(TestPane.this.dispose();
}
}
});
InputMap im=getInputMap();
ActionMap am=getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_空格,0,false),“按空格键”);
im.put(KeyStroke.getK
import java.awt.DisplayMode;
import java.awt.FontMetrics;
import java.awt.Graphics;
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.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setUndecorated(true);
f.add(new TestPane());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (device.isFullScreenSupported()) {
device.setFullScreenWindow(f);
if (device.isDisplayChangeSupported()) {
try {
List<DisplayMode> matchingModes = new ArrayList<>(25);
DisplayMode[] modes = device.getDisplayModes();
for (DisplayMode mode : modes) {
if (mode.getWidth() == 1280 && mode.getHeight() == 720) {
matchingModes.add(mode);
}
}
if (!matchingModes.isEmpty()) {
for (DisplayMode mode : matchingModes) {
try {
device.setDisplayMode(mode);
System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate());
break;
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
System.err.println("!! No matching modes available");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("Change display mode not supported");
}
} else {
System.err.println("Full screen not supported");
}
}
public static class TestPane extends JPanel {
private boolean spaced = false;
public TestPane() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocusInWindow(true);
if (e.getClickCount() == 2) {
SwingUtilities.windowForComponent(TestPane.this).dispose();
}
}
});
InputMap im = getInputMap();
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "spaced-pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), "spaced-released");
am.put("spaced-pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
spaced = true;
repaint();
}
});
am.put("spaced-released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
spaced = false;
repaint();
}
});
requestFocusInWindow(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String text = getWidth() + "x" + getHeight();
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) / 2;
g.drawString(text, x, y + fm.getAscent());
GraphicsDevice device = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode mode = device.getDisplayMode();
text = mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate();
x = (getWidth() - fm.stringWidth(text)) / 2;
y += fm.getHeight();
g.drawString(text, x, y + fm.getAscent());
text = "Spaced [" + spaced + "]";
x = (getWidth() - fm.stringWidth(text)) / 2;
y += fm.getHeight();
g.drawString(text, x, y + fm.getAscent());
}
}
}