Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

draw a gameobject on playfield with OOP

 
Ferdy Dunki Jacobs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm new on this forum so i don't know if my post is at the right place...

I'm just practicing with OOP and inheritance for a game i want to make, i have a playfield with squares (gridlike playfield)
but... and here comes the question...
How can i draw an image on the playfield if i have a superclass called GameObject with 4 subclasses with their own image

my code for the playfield...
Class Game

Class Playfield

SuperClass GameObject

SubClass Player

SubClass Happer

SubClass Stone

SubClass Block

I tried everything i could think of to make an image pop up in my grid like playfield but nothing works
Please can someone help me with this?

If i'm not totally clear just ask me for more info that you need
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ferdy. Welcome to CodeRanch!

First of all, when you post your classes, please post the class name/declaration as well. It's important to know what types they extend or implement.

You also speak of a class GameObject, and four subclasses. Please post them, because it's hard to tell what you want to achieve.

I also notice you make a call to super.paintComponent() twice, one should be sufficient. And you override imageUpdate(). Why?
 
Ferdy Dunki Jacobs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the incomplete info, i edited my post and i hope that it is clear to you now,
and netbeans asked me for an override by imageUpdate(), i think because it is an superclass from netbeans that is hidden from our view.
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, you won't be able to do what you want from making objects being rendered from their constructor. Technically you can, but this would cause a huge amount of overhead (new objects would be created every time the playing field is redrawn).

You also don't need the subclasses. What you can do is have one final GameObject class that is responsible for drawing all the images. You map a type of object to an image, and each instance will keep track of their x-y position and draw their associated image at the correct location. Here is a skeleton:
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the classes GameModel and Entity make up your game data. Entity is anything in your game, a happer, a block, a player, etc. Entity.Type is an enum that describes the type of the entity. GameModel is the model of your game. It keeps track of where all the entities are. It should extend Observable, so it can notify your JPanel of changes to the game.
 
Ferdy Dunki Jacobs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnx, i will look into it and try to fix it in the weekend
 
Ferdy Dunki Jacobs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for all your help, it works now.
you are really a genius with java you can see this topic as solved
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it helped
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic