Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / simple sprite animation loop on: 2007-06-01 05:55:40
This has probably been asked before, but I searched the forums and couldn't find a specific answer...Anyway, I have three images that I want to loop when the up arrow key is pressed, three to loop when down is pressed, same for left and right (making it look like the sprite is walking)...The question is, how would I go about making this loop? Is the while loop best for this sort of thing? I've tried a few different things on my own but so far I haven't been able to make it loop like it should.
2  Game Development / Newbie & Debugging Questions / Re: loading images from a sprite sheet? on: 2007-05-31 05:08:02
lhkbob, I believe you are right!  Using the
g.drawImage(sheet, px,py, px+sw,py+sh, i*sw, j*sh, (i+1)*sw, (j+1)*sh, null);
implementation, when I set i = 1 and j = 0, it displays the first sprite properly!  One problem with this method though is that the sprites aren't lined up evenly with the 2d array...for example when i = 8 and j = 0, it shows half of one sprite and half of another.  This could probably be solved if I knew exactly where the array lined up and redid the sheet accordingly...
oNyx, thank you for the elaborate example!  I think I understand what kind of method you used.  When I get more time I'll be able to look at it more thoroughly.  I think the technique of loading the images separately prior to the painting process is the most practical method, so I will probably end up using this one.  Thanks everyone for the help!  Cool
3  Game Development / Newbie & Debugging Questions / Re: loading images from a sprite sheet? on: 2007-05-30 06:32:56
How you copy it over doesn't really matter since it's only done once.

Hmm.  I understand what you are saying.  I don't know of any way to copy the sprites location and store it as its own image though...can you give an example of how you would do it?  Or the kind of process it would take?
4  Game Development / Newbie & Debugging Questions / Re: loading images from a sprite sheet? on: 2007-05-29 23:59:53
Thank you for your input.  My program now reads images using ImageIO.  I also realized that my x and y coordinates were switched (i should equal 125 and j should equal 0).  Anyway, I'm using photoshop and found the exact pixels needed for the sprite, so the new coordinates are more accurate.  However, the drawImage line of code rdcarvallo gave me still didn't work as expected.  When I made i = 0 and j = 0 with the height = 100 and width =  100, it would display what one would expect: the first 100 x 100 pixels of the image.  But when I tried it with the sprite's exact pixels it wouldn't display anything.  After searching around the internet I came across the setClip method which seems pretty useful:

setClip(int x, int y, int width, int height)
      Sets the current clip to the rectangle specified by the given coordinates.

Using this method I was able to display the specific sprite that I wanted.  Here is the code so far:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;

class Bomberman extends JPanel {
    // Load image
   Image sheet = ImageIO.read(new File("bomberman.gif"));
    // Width and height of sprite
   int sw = 15;
    int sh = 23;
    // Position of sprite on screen
   int px = 100;
    int py = 100;
    // Coordinates of desired sprite image  
   int i = 124;
    int j = 2;
   
    public Bomberman() throws Exception {
    }
   
    // Draw image
   public void paint(Graphics g) {
       super.paintComponent(g);
       g.setClip(i, j, sw, sh);
   g.drawImage(sheet, i - 124,j - 2, null);
    }
}


It cuts out everything around the clipped part of the image, leaving just the desired sprite to be displayed.  Now the part that I'm not sure with now is how to set the location of the image.  The sprite is positioned where it would be as if the rest of the image was there.  So, it's located 124 pixels to the right and 2 pixels down.  The problem is, how would I set it to a different position on the screen?  I think once this problem is solved I'll be able to figure out the rest for my program.  Any suggestions?
5  Game Development / Newbie & Debugging Questions / Re: loading images from a sprite sheet? on: 2007-05-29 07:59:00
Hmm...I'm having trouble getting it to work.  I was testing it on the sprite sheet that I posted above, the coordinates being where the sprite is facing down.  Here's some of the code (pretty much what you gave me) that I used to test it:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
import java.awt.*;
import javax.swing.*;

public class Bomberman extends JPanel {
    // Load image
   Image sheet = Toolkit.getDefaultToolkit().getImage("bomberman.gif");
    // Width and height of sprite
   int sw = 15;
    int sh = 30;
    // Position of sprite on screen
   int px = 100;
    int py = 100;
    // Coordinates of desired sprite image  
   int i = 0;
    int j = 125;
   
    public Bomberman() {
    }
   
    // Draw image
   public void paint(Graphics g) {
       super.paintComponent(g);
      g.drawImage(sheet, px,py, px+sw,py+sh, i*sw, j*sh, (i+1)*sw, (j+1)*sh, null);
    }
}


I have another class which has a main method to display the image on a panel.  Don't worry, I know the class works because I tested it with just a single image and it displayed it correctly (just making sure I rule out any possible errors).  Any suggestions as to what I'm missing?  I appreciate your help!
6  Game Development / Newbie & Debugging Questions / Re: loading images from a sprite sheet? on: 2007-05-28 23:28:11
Thank you very much!  As soon as I determine the pixel coordinates of the images I need I'll try your technique.
7  Game Development / Newbie & Debugging Questions / loading images from a sprite sheet? on: 2007-05-28 22:07:58
Hello.  I've only been learning java a little less than a year now.  I'm working on a 2d rpg game in which I have a sprite sheet that contains all of the images for my main characters sprite.  (the sprite sheet looks something like this:  http://sdb.drshnaps.com/sheets/Misc/Misc/Other/SBM4-Bomberman-.gif ).  However, I'm having trouble figuring out how to extract each image singularly.  I know it would involve finding the exact box of pixels that surround the desired image, cutting it out, and loading it into the program.  Is there a class in the api that can do this?  If so, can it also make the color surrounding the extracted image transparent?  On the other hand, am I taking a wrong approach to making a 2d game?  Is there a better way to load a main character with animation?  I appreciate any help on this matter.
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!