In this tutorial I will show you how easy to draw (character) sprite if you wanna make game in java programing language. First we gonna use JPanel as our game canvas, the idea is override and call logic method as you need inside of it, example :

protected void paintComponent(Graphics g)

Its you who decide the logic inside of each method, but here is how to draw selected sprite basic on your character state :

//spriteLocationX is position x of where the sprite have to draw in canvas (JPanel)
//spriteLocationY is position y of where the sprite have to draw in canvas (JPanel)
//x is x of starting draw coordinate of picture
//y is y of starting draw coordinate of picture
g.drawImage(sprite.getImg(),spriteLocationX , spriteLocationY, (spriteLocationX+width), (spriteLocationY+height),x, y, (x+width), y+height,this);

Spliting Sprite

There rest is decide the logic how to update or change location (spriteLocationX, spriteLocationY) of character in canvas and coordinate (x,y) split of picture based on character state change. Don’t forget to repaint() root parent component each time character state changed event (variable  x, y, spriteLocationX,  sprite locationY, etc changed) to refresh the draw inside of your canvas (JPanel) , if you not then you wouldn’t see any change. Short post but I hope useful  for anyone who want to make java game. Thanks for reading, don’t hesitate to comment if you want to ask something or give any feedback.😉

For newest post about Java :