如何在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(); } }); }
相关分类