DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Simple Java timer lag issue (caused by quad core?)  (Read 6,175 times)

relequestual

  • Honorary Member
  • Joined in 2007
  • **
  • Posts: 93
Simple Java timer lag issue (caused by quad core?)
« on: December 14, 2008, 10:59 PM »
Hi everyone.
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();

}
}
}
<a href="http://feeds.feedburner.com/~r/Appaholic/~6/3"><img src="http://feeds.feedburner.com/Appaholic.3.gif" alt="Are You An AppAholic?" style="border:0"></a>

Original address: https://www.donationcoder.com/forum/index.php?topic=16176.0