Slow mouse position
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.
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.
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.
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.
[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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement