Problem with event hadling in fullscreen mode
I made a fullscreen application and I'm trying to get it to respond to the mouse or keyboard input. The problem is it only responds a few second after I press the button. Does anyone know how I could speed up the response? All my code is posted below.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
publicclass LostHavenimplements KeyListener, MouseListener
{
privatestatic DisplayMode[] BEST_DISPLAY_MODES =new DisplayMode[]{
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(800, 600, 8, 0)
};
Frame mainFrame;
boolean done;
boolean pressed;
public LostHaven(GraphicsDevice device)
{
try
{
GraphicsConfiguration gc = device.getDefaultConfiguration();
mainFrame =new Frame(gc);
mainFrame.setUndecorated(true);
mainFrame.setIgnoreRepaint(true);
device.setFullScreenWindow(mainFrame);
if (device.isDisplayChangeSupported())
{
chooseBestDisplayMode(device);
}
mainFrame.addMouseListener(this);
mainFrame.addKeyListener(this);
mainFrame.createBufferStrategy(2);
BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
done =false;
pressed =false;
while (!done)
{
Graphics g = bufferStrategy.getDrawGraphics();
render(g);
g.dispose();
bufferStrategy.show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
device.setFullScreenWindow(null);
}
}
privatevoid render(Graphics g)
{
Rectangle bounds = mainFrame.getBounds();
g.setColor(Color.black);
g.fillRect(0,0,bounds.width, bounds.height);
g.setColor(Color.red);
g.fillRect(bounds.width*1/4, bounds.height*1/4, bounds.width*2/4, bounds.height*2/4);
if(pressed)
g.drawString("mouse pressed", 0, 15);
}
privatestatic DisplayMode getBestDisplayMode(GraphicsDevice device)
{
for (int x = 0; x < BEST_DISPLAY_MODES.length; x++)
{
DisplayMode[] modes = device.getDisplayModes();
for (int i = 0; i < modes.length; i++)
{
if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth()
&& modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight()
&& modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth()
)
{
return BEST_DISPLAY_MODES[x];
}
}
}
returnnull;
}
publicstaticvoid chooseBestDisplayMode(GraphicsDevice device)
{
DisplayMode best = getBestDisplayMode(device);
if (best !=null)
{
device.setDisplayMode(best);
}
}
publicvoid mousePressed(MouseEvent e)
{
pressed =true;
}
publicvoid mouseReleased(MouseEvent e)
{
}
publicvoid mouseEntered(MouseEvent e)
{
}
publicvoid mouseExited(MouseEvent e)
{
}
publicvoid mouseClicked(MouseEvent e)
{
}
publicvoid keyTyped(KeyEvent e)
{
}
publicvoid keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
done =true;
}
}
publicvoid keyReleased(KeyEvent e)
{
}
publicstaticvoid main(String[] args)
{
try
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
LostHaven gameWindow =new LostHaven(device);
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
}

