Jump to content
  • Advertisement

Archived

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

Jonbca

[java] Polymorphism

This topic is 5847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m working on a game for school using java and it will be 3d. Basically what my problem is, is that I will have several classes(Player, Monster etc) that will all contain a Draw(String param) method. I want this to be used by my graphics class to draw all all the objects. I want to be able to do this something like this:
  
//public class monster

...

public int Draw(String param)
{
    return verticies[];  // or other stuff

}
..
  

  
//public class graphics

...

public void Draw(Type t)
{
    int [] vertices = new int [t.Draw("numOfVerts");
    verticies=t.Draw("getVerts");
    ...
}
...
  

  
// public class PrimEval

static Player player;
static Monster mon;

...

player = new Player;
mon = new Monster;

...

graphics.Draw(player);
graphics.Draw(mon);
...
  
I want to have all players, monsters, whatevers drawn by the same method. I''m kinda looking for templates but the java equivilant. ANy help would be great.

Share this post


Link to post
Share on other sites
Advertisement
Create an interface that contains the draw method and make all your classes implement the interface. Like this:

        
public interface Drawable {
public int draw(String param);
}

public class Monster implements Drawable {
public int draw(String param) {
// Code here

}
}

public class Player implements Drawable {
public int draw(String param) {
// Code here

}
}

// The method in the graphics class

public void draw(Drawable d) {
int vertices = d.draw("numOfVertices");
...
}

// The code that uses the graphics class

static Player player;
static Monster mon;

...

player = new Player();
mon = new Monster();

...

graphics.draw(player);
graphics.draw(mon);



[edited by - HenryAPe on September 13, 2002 3:20:29 PM]

Share this post


Link to post
Share on other sites
Hello


Maybe, this mail is not directly related with what are you asking for, but I hope that it can help you.


In this URL, you will find a java3d game, a first person shooter very simple. The controls are key arrows to move, and
the mouse to shoot.

http://www.mycgiserver.com/~nabetse/Multijuegos/paquetes/Multijuegos_Tomato3D.htm


And the source code of this game:


http://campus.uab.es/~2054119/Tomato3D.zip

Bye.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!