Jump to content
  • Advertisement
Sign in to follow this  
Ansou

Slow mouse position

This topic is 2147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone! I have a problem when I try to get the position of my cursor. I have a full screen JFrame and I have active rendering with a continously running for loop. In the loop I try to draw a rectangle at the position of the mouse with getMousePosition, but when I move the mouse the rectangle comes after. The cursor is moving exactly like it should, but the rectangle moves really slow. I have around 60 FPS.

Share this post


Link to post
Share on other sites
Advertisement
Here's my 2 cents...

Without the code I'm mostly guessing but it's likely you have a resource contention where your active main-loop thread is different from the EDT which would be handling all AWT events.

If you're planning on running a full-screen game with active rendering it might be worth while looking into a framework such as lwjgl or Slick2D (slightly higher level) that will give you full control over the drawing as well as the input events.

Share this post


Link to post
Share on other sites
Here is the full code for the "game":

[source lang="java"]import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class GameFrame extends JFrame {

private static final long serialVersionUID = 1L;
private GraphicsEnvironment ge;
private GraphicsDevice gd;
private BufferStrategy buffer;
private boolean running;

//Set the game to full screen and start it
public static void main(String[] args) {
GameFrame game = new GameFrame();
game.setFullScreen();
game.start();
}

//Constructor
public GameFrame() {
super("Game Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
//the game will stop running if escape is pressed
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.VK_ESCAPE) {
running = false;
}
}
});
}

//Make the game full screen and create a buffer strategy
public void setFullScreen() {
setUndecorated(true);
setIgnoreRepaint(true);
setResizable(false);
gd.setFullScreenWindow(this);
createBufferStrategy(2);
buffer = getBufferStrategy();
}

//Main loop is here
public void start() {
Graphics g = null;
running = true;
while(running) {
try {
g = buffer.getDrawGraphics();
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(Color.RED);
g.fillRect(getMousePosition().x, getMousePosition().y, 20, 20);
if(!buffer.contentsLost()) {
buffer.show();
}
} finally {
if(g != null) {
g.dispose();
}
}
}
System.exit(0);
}
}[/source]

lwjgl and Slick2D is programmed in Java, right? So there has to be some way to do it properly.

Share this post


Link to post
Share on other sites
Oh nvm, I just realized that letting the thread sleep for 20 ms helped... Thank you and sorry for bothering!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!