Sign in to follow this  
relequestual

[java] Simple Java timer lag issue (caused by quad core?)

Recommended Posts

relequestual    122
Hi everyone. I'm new to this forum. A friend of mine has been using it for some time, so I thought I'd give it a go. I've just finished my first term at uni, and learnt some java. I'm creating a simple helicopter game in java, and have some issues using timers. The funny thing was, i started the project on my laptop, and it was going fine. Went back to my desktop, and it lagged. Weird. So I tried it on my laptop again, and it had no lag. By lag, I mean graphically and mouse movement when the application runs. Its not consuming resources, so that's not the problem. I read something on a forum about it being to do with the fact my desktop has a quad core. If this is the case, how can I work around this? Obviously I will have to use my laptop for now. Laptop is dual core running vista, desktop is a quad core running xp sp3. code listing below. 2 separate files but together for this forum import javax.swing.JFrame; import java.awt.*; public class HeliApp extends Object { public static void main(String[] argStrings) throws Exception { JFrame frame = new JFrame ("Heli"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new HeliPanel()); frame.pack(); frame.setVisible(true); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.* public class HeliPanel extends JPanel { int heliX = 50; int heliY = 200; int yMove = 5; private ImageIcon image; int delay = 20; private Timer timer; public HeliPanel() { image = new ImageIcon ("../black-helicopter.gif"); setBackground (Color.white); setPreferredSize (new Dimension(1000,400)); setFocusable (true); Timer timer = new Timer(10, new GameListener()); timer.start(); } public void paintComponent (Graphics page) { super.paintComponent (page); image.paintIcon (this,page,heliX, heliY); } private class GameListener implements ActionListener { public void actionPerformed (ActionEvent event) { heliY = heliY + yMove; repaint(); } } } *edit* It randomly started working again. Possible conflict with some application as I had a clear out and then went back to the code. [Edited by - relequestual on February 3, 2009 3:14:37 AM]

Share this post


Link to post
Share on other sites
Mattox    122
Why are you using a Timer anyway? For a game that simplistic I would assume it'd be more efficient and far easier to just use an infinite while loop in a Thread like most other Java-based games? Maybe a Thread's not even necessary. Something like

public void gameLoop()
{
while(true)
{
if(moveHeli)
{
heliY += yMove;
repaint();
moveHeli = false;
}
}
}


and then you have the basic keyListener/mouseListener triggers that set moveHeli = true etc if you get what I mean.

Personally I have no idea how the code you've put there even works, let alone how to fix it.

http://www.cokeandcode.com/info/tut2d.html - maybe read over that, that's the reference I used to write my first basic Java game and the principles taught there can apply to any game of any complexity, really. Goodluck though, maybe someone else here can provide you with a more educated opinion as I'm not expertly experienced in Java games programming myself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this