Skip to main content
gamedev.net
🔒 Locked

Java: Need Help with Active Rendering. PLEASE HELP

Started by pars 1 reply 200+ views
Original Post
pars
pars
Since the java forum is almost deserted, I posting my question here. I have been programming a pacman game, and It''s been only a short while since I found out about active rendering. Before that I was using "passive" rendering. I have spent the past week trying to convert my code but without success. The thing is different people tell me different ways of doing active rendering, and now I am totally confused. If I could have a "simple" working animation example of active rendering, then I would be able to learn from it. below is a simple animation code in passive rendering (The same one that I used for my game). can you please help me to convert it to active rendering. I hope I''m not asking for too much. Thanx The below code uses 7 GIF images for pacman animation. -----------------------------------
  
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PacmanM extends JPanel implements ActionListener
{
	private ImageIcon images[];
	private static final int TOTAL_IMAGES=7, ANIM_DELAY=50;
	private int currentImage = 0;
	private Timer animationTimer;
	private int delay = 0;

	public PacmanM(int delay)
	{
		this.delay = delay;
		setupAnimation();	
	}
	
	public PacmanM()
	{
		setupAnimation();
	}
	
	public void setupAnimation()
	{
		this.setSize(this.getPreferredSize());
		this.images = new ImageIcon[TOTAL_IMAGES];
		
		for (int i = 0; i < images.length; i++)
		{
			images[i] = new ImageIcon("pac"+i+".gif");
		}
		startAnimation();
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.setColor(Color.blue);
		g.fillRect(0,0,this.getWidth(), this.getHeight());
		if (images[currentImage].getImageLoadStatus() == MediaTracker.COMPLETE)
		{   
			images[currentImage].paintIcon(this, g, 0, 0);
			currentImage = (++currentImage) % TOTAL_IMAGES;
		}	
	}
	
	public void startAnimation()
	{
		if (animationTimer == null)
		{
			currentImage = 0;
			animationTimer = new Timer( ANIM_DELAY + delay, this);
			animationTimer.start();
		} 
		else 
		{
			if (!animationTimer.isRunning())
			animationTimer.restart();
		}
	}
	
	public void stopAnimation (){ animationTimer.stop(); }
	public void actionPerformed(ActionEvent e){ repaint(); }
	public Dimension getMinimumSize() { return getPreferredSize(); }
	public Dimension getPreferredSize (){return new Dimension(400,400);}
	
	public static void main(String[] args )
	{
		PacmanM PacMan = new PacmanM();
		JFrame main = new JFrame("PacMan");
		main.getContentPane().add(PacMan, BorderLayout.CENTER);
		
		main.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
			main.pack();
			main.show();		
	}    
}
  
----Khalije Hamishegiye Faars!!!!
TSwitch
TSwitch
About all you''d have to do is make that nice timer tell your form to be repainted. I think that''s repaint()... haven''t done this in a while.
Member of the Unban nes8bit or the White Rhino in my Basement Gets Sold to the Highest Bidder Association (UNWRBGSHBA - Not accepting new members.)Member of the I'm Glad Mithrandir Finally Found an Association that Accepts People with his Past History Association (IGMFFAAPPHA)
Forestwalker
Forestwalker
You''re code''s a mess... for one thing you are using the repaint method... which is not good, especially for a real-time game because, as I understand it the method for repaint is repaint-->event queque--update(so if you have to many things going at once your coding events may get done before you have painted them on the screen)...
I use AWT... but I am also certain that you need to override the update() method because the update method is this(I''m in a hurry so forgive me...):

public void update(Graphics g){
g.clearRect(0,0,component''s width, component''s height);
g.paint(g);
}

what you need to do is override the update method so it looks like:
This will eliminate the flicker you are propably seeing...
public void update(Graphics g){
g.paint(g);
}

You also need to use Double Buffering, but I don''t see any evidence of it in your code....

-Drugs are bad. Especially if they look like machines. Serial Experiments Lain

-Small girls with a single braid in their hair are all-powerful God''''s. Accept it. (Phar)
-Let chi flow through your body-It's all good :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.