Comment gérer les événements depuis le clavier et la souris en mode plein écran exclusif en java?
en mode de rendu passif, on peut utiliser les interfaces KeyListener
et ActionListener
pour gérer les événements depuis l'utilisateur.
Quelle est la bonne façon de gérer un événement en mode plein écran? S'il vous plaît étendre ce squelette fournissant la mise en œuvre pour le clic de souris et les événements de presse clé, s'il vous plaît ne pas gonfler votre exemple (l'exemple commence le mode plein écran exclusif, en utilisant un Timer
pour mettre à jour les graphiques dans la fenêtre):
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();
}
}
2 réponses
il ressemble aux approches habituelles indiquées dans comment utiliser les fixations de clés et " Comment écrire un récepteur de souris travailler correctement dans "mode exclusif plein écran ".
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 /q/how-to-handle-events-from-keyboard-and-mouse-in-full-screen-exclusive-mode-in-java-65879/"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();
}
});
}
}
comme suggéré ici , les utilisateurs de Mac OS X peuvent avoir des attentes différentes de l'utilisateur pour les applications plein écran. Une approche alternative, montrée ici , s'appuie sur com.apple.eawt
classes qui" fournissent un moyen simple d'implémenter des fonctionnalités natives pour affiner les applications Java sur Mac OS X." La FullScreenUtilities
méthode setWindowCanFullScreen()
active la fonctionnalité, et la Application
méthode requestToggleFullScreen()
change le réglage dynamiquement. Notez comment l'icône expand diffère entre versions .
Mac OS 10.9, Mavericks:
Mac OS 10.10, Yosemite:
Mac OS X 10.11, El Capitan:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/**
* @see https://stackoverflow.com/a/30308671/230513
* @see /q/how-to-handle-events-from-keyboard-and-mouse-in-full-screen-exclusive-mode-in-java-65879/"Mac OS X Full Screen Test";
private static final String TOGGLE = "Toggle Full Screen";
private final JFrame f = new JFrame(NAME);
private final Action exit = new AbstractAction(TOGGLE) {
@Override
public void actionPerformed(ActionEvent e) {
toggleOSXFullscreen(f);
}
};
private final JButton b = new JButton(exit);
public FullScreenTest() {
this.add(b);
f.getRootPane().setDefaultButton(b);
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), TOGGLE);
this.getActionMap().put(TOGGLE, exit);
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
FullScreenTest.this.setToolTipText(
"(" + e.getX() + "," + e.getY() + ")");
}
});
}
private void display() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.darkGray);
f.add(this);
f.add(new JLabel(NAME, JLabel.CENTER), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
if (System.getProperty("os.name").startsWith("Mac OS X")) {
enableOSXFullscreen(f);
toggleOSXFullscreen(f);
enableOSXQuitStrategy();
}
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println(e);
}
});
}
//FullScreenUtilities.setWindowCanFullScreen(window, true);
private void enableOSXFullscreen(Window window) {
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[]{Window.class, Boolean.TYPE};
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, window, true);
} catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException exp) {
exp.printStackTrace(System.err);
}
}
//Application.getApplication().requestToggleFullScreen(window);
private void toggleOSXFullscreen(Window window) {
try {
Class application = Class.forName("com.apple.eawt.Application");
Method getApplication = application.getMethod("getApplication");
Object instance = getApplication.invoke(application);
Method method = application.getMethod("requestToggleFullScreen", Window.class);
method.invoke(instance, window);
} catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException exp) {
exp.printStackTrace(System.err);
}
}
//Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
private void enableOSXQuitStrategy() {
try {
Class application = Class.forName("com.apple.eawt.Application");
Method getApplication = application.getMethod("getApplication");
Object instance = getApplication.invoke(application);
Class strategy = Class.forName("com.apple.eawt.QuitStrategy");
Enum closeAllWindows = Enum.valueOf(strategy, "CLOSE_ALL_WINDOWS");
Method method = application.getMethod("setQuitStrategy", strategy);
method.invoke(instance, closeAllWindows);
} catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException exp) {
exp.printStackTrace(System.err);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new FullScreenTest()::display);
}
}