简体   繁体   中英

Java - Graphics DrawImage Scaling Issue

I am trying to scale an image (that is from a sprite sheet) using this code below.

Point pos = new Point(100, 100); //this shows where to display the image
Rectangle a = getAnimatedPos(); //just gets the coordinates of a specified image (which works correctly)

g.drawImage(img, pos.x, pos.y, pos.x+getWidth(), pos.y+getHeight(), a.x, a.y, a.x+a.width, a.y+a.height, null);

the function getWidth() returns the width of the frame. The formula is (imageWidth/rows).
the function getHeight() returns the height of the frame. The formula is (imageHeight/cols).

When I scale the image (by changing the width or height), it starts to show the other frames? If I leave the width or height of the image to the default size it works. I thought by defining the sources coords and dest coords it wouldn't do this.

To clarify, the image is being painted/rendered on an internal frame say with the size of 640x480. Also, when I mean frame, I am talking about a sprite sheet and each image in the sheet is a frame. It is for animations of course.

Another thing to clarify, the image is not being drawn on top of components. Just an internal frame.

What should I try to fix this?

Screen Shots

When everything is default this is what it looks like: http://wisemindstudios.com/good.png

When I change something: http://wisemindstudios.com/bad.png

Thanks a bunch!

I don't know if this is viable for you or not, but.

Basically, I extract the frame to be rendered, and then draw it.

This saves my poor little brain from imploding from all the parameters

Rectangle frameBounds = //... calculate the frame bounds to be extracted from the sprite sheet
BufferedImage cell = spriteSheet.getSubimage(frameBounds.x, frameBounds.y, frameBounds.width, frameBounds.height);

Rectangle renderBounds = //... calculate the location and size of the sprite to be rendered

g2d.drawImage(cell, renderBounds.x, renderBounds.y, renderBounds.width, renderBounds.height, this);

In my testing, this resulted in a really crappy result. You really should look at a better scaling algorithm ;)

UPDATED

I used this sprite sheet

雪碧表

That produced this output

最后

Now I've added frame numbers add frame boundaries into the render to help it stand out.

The only reason you might be getting fall through from a previous/next frame using this method is that the style sheet is wrong (ie the frame sizes are not uniform) or the calculation for the current frame is wrong.

I spotted the problem. It was in my getAnimatedPos function. I passed in the new width and height of the frame and not the original width and height of the frame (for source coords). So, it was a very simple fix after all.

I was certain it was something wrong with the drawing and not my code. But, it usually is always the users fault (or programmers in this case). Oh, well. It is what it is.

Thanks for helping me out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM