Hi everyone.
I'm new to this forum, recommended by a friend.
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();
		
		}
	}
}

I don't think the fact that the machine is quad core should really make any difference -- all of your stuff is executing in one thread (the GUI thread) anyway. What may make more of a difference is how fasd a SINGLE core can run compared to your other machine. Anyway, here's some ideas:
- you're asking the GUI to be updated 100 times a second; that's possibly a bit ambitious, and probably higher than the screen's refresh rate anyway -- maybe try a delay of 50 (=20 times a second) or even 100?
- try loading your image as a BufferedImage rather than using ImageIcon (see the ImageIO.read() method)
- try using a subclass of JComponent rather than JPanel
- unless most of the screen is moving each frame, don't repaint the entire component each frame -- just repaint the section that you know has changed
- when you load the images, look at converting them into one directly compatible with the current screen (see GraphicsConfiguration.createCompatibleImage())

Thank you for your very prompt reply. Most appreciated.
I wondered that. Both my machines are running 2.4 cores (desktop quad, laptop dual). My friend can run it on his new eee no problem.
I replaced the graphic with a plain square using the fillRect method, still get the same lagging problem. I increased the delay to 1000, and it seems to lag at the end of the delay when repainting. The thing is, eventually, most of the screen will be moving each frame, as the tunnel background will be made up of rectangles moving along while being auto generated off the windows edge. I tried using the subclass JComponent as you suggested (not sure if I did this right mind you), however it didn't make any difference.
Any further suggestions?

I'd also recommend timing your game animation on a desired fps instead of a static timer. Here's an article that covers that: http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation.html.
Here is another on sprite animation: http://www.developer.com/java/article.php/893471
And this is a good 2D game tutorial in general (though it uses the GAGE timer instead of a "home-rolled" version): http://www.cokeandcode.com/spaceinvaderstutorial

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.