Archived

This topic is now archived and is closed to further replies.

PuterPaul

[java] Understanding Java 2D animation

Recommended Posts

Ok, i''m fairly new to Java2D API, and so I would appreciate some guidance in the form of animation. As I have just been handed an assignment in which I have to code a 2d game. Here''s some bare minimum code below, just to see that I am understanding it correctly. CGameObject.java
import java.awt.*;
import java.awt.image.*;

class CGameObject extends Canvas
{
  private Graphics m_gfx;
  private Image m_image;

  //Constructor

  public CGameObject()
  {
    m_gfx = null;
    m_image = null;
  }

  public void paint()
  {
    Graphics2D g2 = (Graphics2D)m_gfx;
    g2.drawImage(m_image);  //Don''t worry i''m aware this aint quite right.

  }

  public void update()
  {
    BufferedImage im = (BufferedImage)m_image;
    paint();
  }
  
}
Biplanes.java
class Biplanes
{
  private CGameObject m_background; // Background image (will be static)  

  private CGameObject m_foreground; // Represents a bi-plane moving  

  private boolean Running; // Game Running?


  //Constructor

  public Biplanes()
  {
  }

  public void InitGame()
  {
    Running = true;
    m_background.paint();
    m_foreground.paint();

  }

  public void ExitGame()
  {
     System.exit(0);
  }

  public static void main(String[] args)
  {
    InitGame();

    while(Running)
    {
      m_background.update();
      m_foreground.update();
    }
    
    ExitGame();
  }
}
Also I realise this code doesn''t implement any form of sprites (images with multiple frames), how would this be acheived. Oh and when I do eventually implement keyboard input, would it be best to implement it within the GameObject class or the main Biplanes class??

Share this post


Link to post
Share on other sites
Hey, I''m new to java but I can see 1 problem off the bat..
public void paint() should be
public void paint(Graphics g), replace the g with what you want..

As for using multiple frames of animation check out this.

And keyboard input should be in your main class so I''m assuming it''s CGameObject.java.

Share this post


Link to post
Share on other sites
You should use Panel instead of Canvas.

Look up in the API for the Graphics class. It can draw from only a portion of an image if you want. This is how animation is achieved. Try to find The Black Art of Java Game Programming, it has a lot of good tips.

On a style note, everything you make in Java is a class, so there is no need to prepend all your classes with a ''C''. Also to make it easier to identify, you should start all your classes with capital letters and all your methods and variables with lowercase letters.



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

Share this post


Link to post
Share on other sites