[java] About 1080 fps??

Started by
5 comments, last by Eponick 20 years, 6 months ago
Alright, I know theres probably a better way to do this. I am just learning Java Game programming and I have been programming C++ Games for about 3 years, just learning Java for something to do Anyway, when I run this applet it goes VERY fast, and I put in a frame counter to slow it down, please tell me if there is a better way. Code:

import java.applet.*;
import java.awt.*;

public class ApTest extends Applet
{
	Graphics bufferGraphics; 
	Image offscreen; 
	Dimension dim;
	
	Image Guy;
	int y;
	
	int fcount;
	
	int Accel;
	int Velo;
	
	boolean Jumping;
	public void init()
	{
		Guy = getImage(getCodeBase(), "Guy.GIF");
		
		dim = getSize();
		offscreen = createImage(dim.width,dim.height);
		bufferGraphics = offscreen.getGraphics();
		
		y=150;
		Velo=0;
		Accel=1;
		Jumping=false;
		
		fcount=0;
	}
	public void paint (Graphics g)
	{
		if(fcount>1000)
		{
			bufferGraphics.clearRect(0,0,dim.width,dim.width); 
			bufferGraphics.drawImage(Guy, 80, y, this);
			
			if(Jumping == true)
			{
				Velo+=Accel;
				y+=Velo;
			}
			
			if(y>150 && Jumping==true)
			{
				y=150;
				Jumping=false;
			}
			
			g.drawImage(offscreen,0,0,this);
			fcount=0;
		}
		fcount++;
		repaint();
	}
	
	public boolean keyDown(Event e, int key)
	{
		if(Jumping==false)
		{
			Velo=-15;
			Jumping=true;
		}
		
		return true;
	}
	public void update(Graphics g) 
	{ 
		 paint(g); 
	} 

}
  
Im basicly using Paint as my main game loop, because I dont know a better way [edited by - Eponick on October 22, 2003 11:32:47 PM]
Lead ProgrammerDawn of Daria
Advertisement
use a method called "Update" (i think...haven''t done animation in a while, i''m focusing on AP Comp Sci right now).
and you should define a thread, and tell it to sleep for a certain number of milliseconds in between repetitions of the thread.
Everything looks alright. You should run your main game loop in a separate thread. Your paint method should only contain painting code, not game decisions. If you haven''t learned threads yet, don''t worry. In Java threads are a piece of cake.


First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Alright, ill look into them.
Thanks
I remember threads were a pain at first in C++, but I got used to them :D

Edit:
Alrigt, everything is fine but im getting this error:
The constructor Thread(Pong) is undefined

        public void start()	{		if(gThread==null)		{			gThread = new Thread(this); //<-- Error here			gThread.start();		}	} 


[edited by - Eponick on October 23, 2003 11:57:49 AM]
Lead ProgrammerDawn of Daria
Thread takes a Runnable as its parameter. Runnable is an interface with one method, void run().

change it to be

class ApTest implements Runnable {
...
public void run() {
// code that runs in separate thread in here
}
}

Doing it this way means you can only ever have two of your threads, you may want to create a new separate class and make it Runnable, that way you can add more types of threads later if needed.
You might also think of makeing a class that extends Thread. Thread in it self imlements Runnable..... then the only thing you have to do is create a method in the new class that is called run() and the in the constructor of that class (or in the class creating this "Threaded" class) you call the start() method and your thread should be flying
garazdawi - 'I put the laughter back in slaughter'
That looks right. Post your complete code. The problem may lie somewhere else.


First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement