Reducing flicker (java)

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Post Reply
Nicholai
Posts: 509
Joined: Sun Nov 13, 2005 4:28 pm

Reducing flicker (java)

Post by Nicholai »

This is my Java pong game and oh god does it flicker. Would appreciate some advice, I tried writing it to a buffer, but writing an eraser method made it pretty much equally flickery. Would love some help. :D


Code: Select all

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

public class Pong extends Applet
{
	int x, y, dx, dy,score1, score2, LocP1, LocP2;
	Rectangle pad1,pad2;
	

	
	

	public void init()
	{
		
		enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
		x = 310;
		y = 230;
		dx = 2;
		dy = 0;
		LocP1 = 200;
		LocP2 = 200;
		pad1 = new Rectangle(10,LocP1,20,75);
		pad2 = new Rectangle(610,LocP2,20,75);
		
		(new java.util.Timer(true)).schedule( new java.util.TimerTask() { public void run()	{ computeFrame(); repaint(); }},5,5);
	}

	public void paint( Graphics g )
	{
		g.drawRect(0,0,800,600);
		g.fillOval( x,y,20,20 );
		g.fillRect(10,LocP1,20,75);
		g.fillRect(610,LocP2,20,75);
		g.drawString(Integer.toString(score1), 300, 20);
		g.drawString(Integer.toString(score2), 340, 20);
		
	}

	public void computeFrame()
	{

		x += dx;
		y += dy;

		if ( x <28> 612 )
		{
			
			if(pad1.inside(x,y) || pad2.inside(x+20,y))
			{
				dx = -dx;
				dy = 2;
			}
			else
			{
				if(x>320)
				{
					score1++;
				}
				else
				{
					score2++;
				}
				x = 310;
				y = 230;
				dx = 2;
				dy = 0;
			}
			
		}
			
		if ( y <0> 480 )
			dy = -dy;
			
	}



	public void processKeyEvent(KeyEvent e)
	{
		if ( e.getID() == KeyEvent.KEY_PRESSED )
		{
			if ( e.getKeyCode() == KeyEvent.VK_W )
				LocP1 -= 15;
			if ( e.getKeyCode() == KeyEvent.VK_S )
				LocP1 += 15;
			if ( e.getKeyCode() == KeyEvent.VK_UP )
				LocP2 -= 15;
			if ( e.getKeyCode() == KeyEvent.VK_DOWN )
				LocP2 += 15;
			
			pad1 = new Rectangle(10,LocP1,20,75);
			pad2 = new Rectangle(610,LocP2,20,75);
			repaint();
			
		}
	}
}
gannon
Moderator
Posts: 6974
Joined: Sun Apr 04, 2004 4:48 pm
Location: Near that one big lake
Contact:

Post by gannon »

That how you use timer? I never figured it out so I just use

Code: Select all

while (true) {
	if (System.currentTimeMillis() - draw >= 5) {
		field.repaint();
		draw = System.currentTimeMillis();
	}
}
:P
Nicholai
Posts: 509
Joined: Sun Nov 13, 2005 4:28 pm

Post by Nicholai »

gannon wrote:That how you use timer? I never figured it out so I just use

Code: Select all

while (true) {
	if (System.currentTimeMillis() - draw >= 5) {
		field.repaint();
		draw = System.currentTimeMillis();
	}
}
:P

yeah, it was the most consistent way to animate at a good constant frame rate because every 10 ms, the frame method is called, regardless of what the program's currently doing.
marshallh
Moderator
Posts: 2986
Joined: Sat Sep 10, 2005 2:17 pm
360 GamerTag: marshallh
Location: here and there
Contact:

Post by marshallh »

You need to look into double-buffering (drawing to an identical offscreen buffer and then copying onto the screen every frame.)

Java is a real bear to do anything with graphics-wise, and slow as heck. I started coding a 3d rasterizer engine, eventually I got shaded polygons and models with z-buffering and flat shading, but I only got about 4-5 fps. On the same machine I recoded it into C, added gouraud shading, environment mapping, texturing, etc, and still get about 20-30 fps

Java just sucks. I'm not even sure it's worth teaching anymore.
Image
Nicholai
Posts: 509
Joined: Sun Nov 13, 2005 4:28 pm

Post by Nicholai »

Code: Select all

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Pong extends Applet
{
	int x, y, dx, dy,score1, score2, LocP1, LocP2,pXball,pYball,hits;
	Rectangle pad1,pad2;
	Image virtualMem;
	Graphics gBuffer;
	public void init()
	{
		hits = 0;
		virtualMem = createImage( getWidth() ,getHeight() );
		gBuffer = virtualMem.getGraphics();
		enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
		x = 310;
		y = 230;
		dx = 2;
		dy = 2;
		LocP1 = 200;
		LocP2 = 200;
		pad1 = new Rectangle(10,LocP1,20,75);
		pad2 = new Rectangle(610,LocP2,20,75);
		(new java.util.Timer(true)).schedule( new java.util.TimerTask() { public void run()	{ computeFrame(); repaint(); }},5,5);
	}
	public void paint( Graphics g )
	{	
		gBuffer.setColor(Color.white);
		gBuffer.fillRect(0,0,640,480);
		gBuffer.setColor(Color.black);
		gBuffer.drawRect(0,0,640,480);
		gBuffer.fillOval( x,y,20,20 );
		gBuffer.fillRect(10,LocP1,20,75);
		gBuffer.fillRect(610,LocP2,20,75);
		gBuffer.drawString(Integer.toString(score1), 300, 20);
		gBuffer.drawString(Integer.toString(score2), 340, 20);
		g.drawImage(virtualMem,0,0,this);
	}
	public void computeFrame()
	{
		if(hits == 4)
		{
			dy++;dx++;
			hits = 0;
		}
		x += dx;
		y += dy;
		if ( x <28> 612 )
		{
			if(pad1.inside(x,y) || pad2.inside(x+20,y))
			{
				dx = -dx;hits++;
			}
			else
			{
				if(x>320)
				{
					score1++;
				}
				else
				{
					score2++;
				}
				x = 310;
				y = 230;
				dx = 2;
				dy = 2;
				hits = 0;
			}	
		}		
		if ( y <0> 480 )
			dy = -dy;
	}
	public void processKeyEvent(KeyEvent e)
	{
		if ( e.getID() == KeyEvent.KEY_PRESSED )
		{
			if ( e.getKeyCode() == KeyEvent.VK_W )
				LocP1 -= 15;
			if ( e.getKeyCode() == KeyEvent.VK_S )
				LocP1 += 15;
			if ( e.getKeyCode() == KeyEvent.VK_UP )
				LocP2 -= 15;
			if ( e.getKeyCode() == KeyEvent.VK_DOWN )
				LocP2 += 15;
			
			pad1 = new Rectangle(10,LocP1,20,75);
			pad2 = new Rectangle(610,LocP2,20,75);
			repaint();
		}
	}
	public void update(Graphics g)
	{
		paint(g);
	}
}

Good call. :D
Last edited by Nicholai on Wed Jun 11, 2008 4:54 am, edited 1 time in total.
Post Reply