Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Slow mouse position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 LeDerpish   Members   -  Reputation: 109

Like
0Likes
Like

Posted 08 July 2012 - 11:33 AM

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.

Sponsor:

#2 loom_weaver   Members   -  Reputation: 325

Like
0Likes
Like

Posted 08 July 2012 - 03:21 PM

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.

#3 LeDerpish   Members   -  Reputation: 109

Like
0Likes
Like

Posted 09 July 2012 - 10:29 AM

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.

#4 LeDerpish   Members   -  Reputation: 109

Like
0Likes
Like

Posted 09 July 2012 - 10:36 AM

Oh nvm, I just realized that letting the thread sleep for 20 ms helped... Thank you and sorry for bothering!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS