Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Sprites, how to?  (Read 501 times)
0 Members and 2 Guests are viewing this topic.
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« on: 2012-01-17 13:58:02 »

Hey guys,

So I've been wanting to figure out how to use sprites in Java. I know how the concept works and all that, since I've used them in html/css for a while now.

But I'm not sure how to implement it in Java? Also, how can i make them so that the sprites are in, say 8 colors, and those colors can be changed on the fly by the game?
Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #1 on: 2012-01-17 17:27:58 »

Hey guys,

So I've been wanting to figure out how to use sprites in Java. I know how the concept works and all that, since I've used them in html/css for a while now.

But I'm not sure how to implement it in Java? Also, how can i make them so that the sprites are in, say 8 colors, and those colors can be changed on the fly by the game?

As far as changing the pixels of a sprite in java 'on the fly', I know no such way.
But for sprites, I could help you out =D.

You're going to want to create a ImageIcon first for the sprite, than list the path for it, than draw it using java Graphics.

1  
public ImageIcon tree = new ImageIcon("src/server/cache/tree.png"); //<-- that is your path, now stored in "tree".

Next you're going to want to create a method to actually draw the tree/picture you're choosing.

1  
2  
3  
public void paint(Graphics g) {
g.drawImage(tree, 0, 0, null); //<-- Simple, "tree" = image, 0=X, 0=Y, null = your observer.
}


Hope i'm not to late, believe everything above should work, but other members here frown upon the way i do things so wait for others opinions if mine doesn't cut it for drawing sprites.

Offline ReBirth

JGO Wizard
****

Posts: 1279
Medals: 19



« Reply #2 on: 2012-01-17 18:54:29 »

You can create a BufferedImage as sprite and redraw it when needed.

Follow me, your mastah, on TWITTAH!
Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3160
Medals: 196


I'm the King!


« Reply #3 on: 2012-01-17 19:26:04 »

You can't directly draw an ImageIcon, you have to get the Image from it first. However, it is much cleaner to get the image using javax.imageio.ImageIO.

A Sprite should basically be a class that stores the X, Y, width, height, image(s), and other variables that might be important.

Offline lordberzerker97

JGO n00b
*

Posts: 34



« Reply #4 on: 2012-01-17 21:03:07 »

There is a video link here.

http://www.youtube.com/watch?v=sxBknKI2BfQ

this guy shows how to use sprites in java.
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #5 on: 2012-01-18 04:36:11 »

Hrm, that looks easy enough. I'll get sprites working first and then I'll figure out how to change their colors on the fly.

Thanks for all the help guys. Smiley
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.07 seconds with 19 queries.