Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Image clipping  (Read 1436 times)
0 Members and 1 Guest are viewing this topic.
Offline 2DGamer

Senior Newbie




Java games rock!


« Posted 2004-07-03 11:42:11 »

Hi all,

First time poster. Just wanted to ask a quick question about sprite animation and clipping.  Up until now I've been using
1  
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

to draw the correct part of the sprites image, but I've recently made the transition from int to double when storing my sprites position so it doesn't really work anymore.

Now I've started using CropImageFilter, so when I create a sprite that has more than one frame I split the image into an image array and use this bit of code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void draw(Graphics2D g)
      {
            // Draw the sprite if it isn't hidden
           if (m_image != null && !m_bHidden)
            {
                  // Draw the appropriate frame, if necessary
                 // m_at == AffineTransform
                 if (m_iNumFramesX == 1)
                        g.drawImage(m_image, m_at.getTranslateInstance(m_rcPosition.x, m_rcPosition.y), null);
                  else
                  {
                        g.drawImage(m_images[m_iCurRow][m_iCurColumn],
                              m_at.getTranslateInstance(m_rcPosition.x, m_rcPosition.y), null);
                  }
            }
      }


Now this works but in the game I'm working on right now I change a sprites image based on certain game events and when this happens I create a new image array and crop the appropriate image into it. This works just fine locally but when I play the applet over the web I get a half a second freeze when this happens.

I've been trying to use setClip(...) now to draw just the part of the current image that I want but I just can't seem to figure out just how exactly it works :p so any help would be appreciated (regarding setClip or any better way to get the part I want). Oh and if it is of any interest, the Graphics2D object the sprite's draw method receives is from the volatile image I use as a back buffer.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2004-07-03 11:59:56 »

>I've recently made the transition from int to double when
>storing my sprites position so it doesn't really work anymore.

You can still use that method if you like. Y'know you can just cast (or correctly round) those doubles (or floats) to int.

>when this happens I create a new image array

Why? Crop it once and reuse it.

弾幕 ☆ @mahonnaiseblog
Offline 2DGamer

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-07-03 14:32:57 »

Guess I could cast the double values. In fact that's what I've been doing but it seemed to defeat the purpose of using double to store the position only to cast them back to int when drawing the sprite (though I guess any precision lost would be far from noticeable:) ).

I made a new image array because as it turned out the different images did not have an equal amount of frames.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!