Java Sprite Class

Started by
6 comments, last by jsgcdude 19 years, 5 months ago
I have attempted to write a Java sprite class. Can someone critique it for me? I feel like it has a lot of flaws. Sprite.java
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JApplet;

/*
 * Created on Nov 13, 2004
 *
 */

public class Sprite extends JApplet {
    int xPos;
    int yPos;
    private int xVelocity;
    private int yVelocity;
    int width;
    int height;
    private Image myImage;
    private Box bound;

    public Sprite(String filename, int myWidth, int myHeight) {
        width = myWidth;
        height = myHeight;
        xPos = 0;
        yPos = 0;
        myImage = getImage(getDocumentBase(), filename);
    }
    public void setPosition(int newXpos, int newYpos){
        xPos = newXpos;
        yPos = newYpos;
    }
    public void processVelocity(){
        xPos += xVelocity;
        yPos += yVelocity;
    }
    public boolean collision(Sprite other){
    	int x0 = xPos + bound.x1;
    	int y0 = yPos + bound.y1;
    	int x1 = xPos + bound.x2;
    	int y1 = yPos + bound.y2;
    	
    	int x2 = other.xPos + other.bound.x1;
    	int y2 = other.yPos + other.bound.y1;
    	int x3 = other.xPos + other.bound.x2;
    	int y3 = other.yPos + other.bound.y2;

    	return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
    }
    public void blit(Graphics g){
        g.drawImage(this.myImage, xPos, yPos, width, height, this);
    }
    public void blit(Graphics g, int _xpos, int _ypos){
        g.drawImage(this.myImage, _xpos, _ypos, width, height, this);
    }
                              
}



Box.java
[source lang = "Java"]
public class Box {
    public int x1;
    public int y1;
    public int x2;
    public int y2;
    public int width;
    public int height;

    public Box(Sprite dude) {
        x1 = dude.xPos;
        y1 = dude.yPos;
        x2 = x1 + dude.width;
        y2 = y1 + dude.height;
        width = dude.width;
        height = dude.height;
    }
}




Implementation.java

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

public class Implementation extends Applet
{
   Sprite test;
   public void init()
   {
     test = new Sprite("ganesha.jpg", 234, 343);
   }
  public void paint (Graphics g)
  {
    g.drawString("Hello World!", 50, 25);
  }
}


I run it, and it says "Applet not initialized" and does this:

java.lang.NullPointerException
	at java.applet.Applet.getDocumentBase(Unknown Source)
	at Sprite.<init>(Sprite.java:26)
	at Implementation.init(Implementation.java:9)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
	at java.applet.Applet.getDocumentBase(Unknown Source)
	at Sprite.<init>(Sprite.java:26)
	at Implementation.init(Implementation.java:9)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

What could be going wrong? I have the ganesha.jpg in the right place. [Edited by - CodeTitan on November 13, 2004 5:15:03 PM]
Advertisement
Im not a java programmer so this might be irrelevant but are you linking it properly?
It would help if you commented which lines are 26 and 9 in the source code so we could see where the errors occur.
I believe this is because the Sprite class, although it extends JApplet, is not being run as an applet. Therefore the Sprite class does not have a document base. I would suggest passing a reference to the applet to the Sprite constructor, and then calling getDocumentBase() on the reference. Something like this:

public Sprite(String filename, int myWidth, int myHeight, Applet myApplet) {        width = myWidth;        height = myHeight;        xPos = 0;        yPos = 0;        myImage = getImage(myApplet.getDocumentBase(), filename);    }


This can then be called from the Implementation class like:
import java.applet.*;import java.awt.*;public class Implementation extends Applet{   Sprite test;   public void init()   {     test = new Sprite("ganesha.jpg", 234, 343, this);   }  public void paint (Graphics g)  {    g.drawString("Hello World!", 50, 25);  }}


Stu
Umm, what does a sprite have to do with an Applet?

So what exactly is an applet? Its a gui component and a container for other components.

A sprite is a graphical entity that consists of position and an image.

The two sound pretty disimilar don't they?

Go here; articles-> space invaders 101

http://grexengine.com/sections/externalgames/
I created the sprite class so that I could write games in applets.

Should I instead make this an application instead of an applet?
No, the problem is that your sprite itself isn't an applet, it is something that is going to be drawn onto an applet. Therefore, your Sprite class needs to know about (but not extend) the functionality of JApplet.

Stu
Old but informative....
------------------<a href="http://jsgc.sourceforge.net>jsgc

This topic is closed to new replies.

Advertisement