[JAVA] Problem med focus, tror jag
Hej, jag vill att mitt program ska stängas ner när jag trycker ner någon tangent. Men det verkar inte funka, och efter lite koll så har jag sett att det är min frame som har focus, men mousemovement funkar endå tydligen och mousepressed verkar funka, det kan dock bero på att den ger focus först när jag trycker på musknappen.
Iallafall finns det något sätt att direkt säga att jag vill att myWindow ska få focus bestämt? Den verkar inte få det fast jag kör myWindow.requestFocus();
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Main_Window {
private Model themodel;
private long fpstimer;
Graphics g;
Window myWindow;
GraphicsDevice gd;
GraphicsConfiguration gc;
Frame frame;
BufferStrategy strategy;
boolean quit;
int screenwidth;
int offsetx;
int offsety;
int screenheight;
int test;
/**starting the program, creates a new paintwindow
* @param args
*/
public static void main(String[] args) {
Main_Window window = new Main_Window();
window.theGame();
}
/**creates a new paintwindow, and add containers to it
*
*/
Main_Window() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
gc = gd.getDefaultConfiguration();
frame = new Frame(gd.getDefaultConfiguration());
test = 0;
myWindow = new Window(frame);
this.themodel = new Model();
quit = false;
gd.setFullScreenWindow(myWindow);
myWindow.requestFocus();
myWindow.requestFocusInWindow();
int numBuffers = 2; // Includes front buffer
this.myWindow.createBufferStrategy(numBuffers);
strategy = this.myWindow.getBufferStrategy();
// g = strategy.getDrawGraphics();
// this.myWindow.
// this.myWindow.addKeyListener(this);
myWindow.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
quit = true;
}
}
);
myWindow.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent evt){
themodel.handleMouseMotion(evt);
}
}
);
frame.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent evt){
quit = true;
// themodel.handleKeyBoardpressed(evt);
}
public void keyTyped(KeyEvent e){
// themodel.handleKeyBoardtyped(e);
quit = true;
//themodel.handleKeyBoardpressed(e);
}
}
);
screenwidth = myWindow.getWidth();
screenheight = myWindow.getHeight();
offsetx = (screenwidth - 1200)/2;
offsety = (screenheight - 900)/2;
}
/**
* this is the main loop
*/
void theGame(){
myWindow.setFocusableWindowState(true);
myWindow.setEnabled(true);
myWindow.setFocusable(true);
// KeyboardFocusManager.setCurrentKeyboardFocusManager(null);
while(quit == false){
//frame.requestFocus();
myWindow.requestFocusInWindow();
myWindow.requestFocus();
if(frame.isFocused()){
System.out.println(test);
test++;
}
fpstimer = java.lang.System.currentTimeMillis();
Graphics g = strategy.getDrawGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, screenwidth, screenheight);
this.themodel.drawGame(g);
g.setColor(Color.black);
g.fillRect(0,0,offsetx,screenheight);
g.fillRect(screenwidth-offsetx,0,offsetx,screenheight);
g.fillRect(offsetx, 0, 1200, offsety);
g.fillRect(offsetx, screenheight-offsety, 1200, offsety);
strategy.show();
g.dispose();
while(fpstimer + 20.0 > java.lang.System.currentTimeMillis()){}
}
gd.setFullScreenWindow(null);
this.frame.dispose();
this.myWindow.dispose();
}
}