Draw sprite in object class and return to main class

Started by
3 comments, last by Marko T 10 years, 11 months ago

Im trying to make a simple game. I have the basic understanding of spreadsheet and the coding itself, but im havign problems now and then.
Can i place the drawImage function in my object class so that i can return it anywhere i want. example. Boar.java has a line


if(boss.isVisible())
   boss.drawBoss();

adn the Boss.java has this


public Image drawBoss(){
     Graphics g
     Graphics2D g2d = (Graphics2D)g;
     return g2d.drawImage(.....)
}

So how to i make this work???

Advertisement

The Graphics objects comes from Swing. If you have a swing component, such as a JPanel, is will contain a paint method that will be called and passed a graphics object:


public class MyClass extends JPanel {

   public void paint( Graphics g ) {
      // use graphics object here
   }

}

Take a look at this to get started:

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

So, as i understand, all the painting must be done int that one function, but i can send all the attributes to it.

So, as i understand, all the painting must be done int that one function, but i can send all the attributes to it.

Something like that. Try it out and see what happens.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Sending the attributes isnt a problem. i just want to animate the movement and i had an idea with the frame switching, but it had to be built in the object. Now i need to think up a differetn way to change frames.

This topic is closed to new replies.

Advertisement