在被動渲染模式下,可以使用接口KeyListener
和ActionListener
來處理來自用戶的事件。如何在Java中以全屏獨佔模式處理來自鍵盤和鼠標的事件?
在全屏模式下事件處理的正確方法是什麼?請擴展此框架提供實現鼠標點擊按鍵事件,請不要臃腫的例子(例子開始全屏獨佔模式,使用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();
}
}
謝謝你的回答!你能不能也請提供相同的例子,但作爲Java小程序?我想要成爲一個Java小程序的例子,但似乎我忘了在第一篇文章中包含它...我認爲你只需要對你提供的實際代碼進行小的增加/更改,非常感謝你,如果你這樣做,因爲你的例子很好學習! – Vadim
我建議'JApplet',但爲了安全起見,它可能必須[signed](http://download.oracle.com/javase/tutorial/deployment/applet/security.html)。您可以使用[此處]顯示的混合方法進行試用(https://sites.google.com/site/drjohnbmatthews/subway); [tag:java-web-start]會是一個更好的部署策略。 – trashgod
謝謝,會檢查它! – Vadim