Reputation: 71
I am currently developing a game in java.When I start my game I get to chose between Full Screen and Window Mode, by clicking somewhere in the frame.The Window Mode works perfectly, but I have some issues at the full screen mode.So if I have in my main() just a simple initialization of the game, like new FullscreenMode()
, the game works perfect, the listeners are working.In the code bellow if I launch the full screen Mode frame from the constructor it works perfect, but if I launch it from a function that belongs to my class, all the listeners won't work...And when I say it's not working properly, I mean the listeners are not responding, everywhere I click I get no response from the game, but if the full screen Mode Frame is launched from the constructor everything works perfectly.Thank you in advance!
public class ResolutionChoser extends JFrame implements MouseMotionListener,
MouseListener {
private static final long serialVersionUID = 1L;
private BufferedImageLoader loader;
private BufferedImage rezImg = null;
public ResolutionChoser() {
super("Welcome!");
requestFocus();
loader = new BufferedImageLoader();
rezImg = loader.loadImage("/RezImg.png");
this.setPreferredSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
Game.HEIGHT * Game.SCALE + 2));
this.setMaximumSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
Game.HEIGHT * Game.SCALE + 2));
this.setMinimumSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
Game.HEIGHT * Game.SCALE + 2));
this.pack();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
addMouseMotionListener(this);
addMouseListener(this);
JLabel jl = new JLabel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(Graphics g) {
g.drawImage(rezImg, 0, 0, null);
}
};
setContentPane(jl);
//Works Perfectly if I do it like so
// launchFullScreen();
}
//Method that launches my fullscreen game
public void launchFullScreen() {
removeMouseMotionListener(this);
removeMouseListener(this);
MultiBufferTest.main(null);
}
public static void main(String[] args) {
ResolutionChoser rc = new ResolutionChoser();
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("CLICKED AT: " + arg0.getX() + "--" + arg0.getY());
launchFullScreen();
//not working properly, the listeners for my game aren't responding
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
Upvotes: 0
Views: 59
Reputation: 347204
Don't attach Mouseistener
s directly to a frame, there is a JRootPane
and content pane (and possibly a glass pane) between the frame the user, any one of theses could be stealing mouse events, which will prevent your frame from receiving mouse events.
Instead, attach the MouseListener
to the top level component directly
Also...
JLabel jl = new JLabel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(Graphics g) {
g.drawImage(rezImg, 0, 0, null);
}
};
this is not only a bad idea (you've broken the paint chain), it's also not required, as JLabel
is capable of displaying images.
See Painting in AWT and Swing, Performing Custom Painting and How to Use Labels for more details
Upvotes: 1
Reputation: 3557
Well, you are removing the listeners in your launchFullScreen()
method:
public void launchFullScreen() {
removeMouseMotionListener(this);
removeMouseListener(this);
MultiBufferTest.main(null);
}
How do you expect the listeners to respond?
Upvotes: 1