Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hey StackExchange GameDev community, I'm attempting to resize series of sprites upon instantiation of the class they're located in.

I've attempted to use the following code to resize the images, however my attempts have been unsuccessful. I have been unable to write an implementation that is even compilable, so no error codes yet.

    wLeft.getScaledInstance(wLeft.getWidth()*2, wLeft.getHeight()*2, Image.SCALE_FAST);

I've heard that Graphics2D is the best option.

Any suggestions? I think I'm probably best off loading the images into a Java project, resizing the images then outputting them to a new directory so as not to have to resize each sprite upon class instantiation. What do you think?

Photoshopping each individual sprite is out of the question, unless I used a macro.

Code:

package game;

//Import
import java.awt.Image;
import javax.swing.ImageIcon;

public class Mario extends Human {
    Image wLeft = new ImageIcon("sprites\\mario\\wLeft.PNG").getImage();

 //Constructor
    public Mario(){
        super("Mario", 50);
        wLeft = wLeft.getScaledInstance(wLeft.getWidth()*2, wLeft.getHeight()*2, Image.SCALE_FAST);
    }
share|improve this question
    
You just want to scale them once then you're done? And you have a lot of sprites? –  Byte56 Mar 21 '12 at 15:09
    
Yeah, I have a lot of sprites. I need to enlarge them because they're all approx. 22x37px. Too small to see. –  Delete Mar 21 '12 at 17:19
    
Your code looks OK, just make sure that sprites\\mario\\wLeft.PNG is actually a valid path for your application. Josh has the real answer, use an external program to resize and save. –  Byte56 Mar 21 '12 at 17:48
add comment

1 Answer

up vote 2 down vote accepted

I've attempted to use the following code to resize the images, however my attempts have been unsuccessful. I have been unable to write an implementation that is even compilable, so no error codes yet.

That code looks okay to me -- but I don't know Java very well. You may want to post another more-specific question should you elect to go the route of resizing at runtime if you still can't get it working, however:

I think I'm probably best off loading the images into a Java project, resizing the images then outputting them to a new directory so as not to have to resize each sprite upon class instantiation. What do you think?

That's correct -- it'd be much better to resize the images at build time or author time instead of at runtime, because then you are paying the cost for that operation once, instead of your users paying that cost every time your game loads. It may be a trivial cost, but its simplifying.

You could write your own code to do this, and it might be an excellent learning experience for you. But you could also find a tool to do it for you. Photoshop with a macro might be viable, for example, or you could use Gimp with this batch processor plugin (see here for a guide on batch resizing a lot of images). If you poke around on your favorite search engine for things like "batch image resizer" you'll come up with a few results as well, such as this one.

share|improve this answer
    
Aw, I was just writing my answer. Anyway, a good Google search term is image batch resize there are a few simple free applications that'll allow batch resizing. –  Byte56 Mar 21 '12 at 17:43
    
You can still post your answer, it could very well be better than mine. –  Josh Petrie Mar 21 '12 at 17:44
    
Na, you got it pretty well covered. –  Byte56 Mar 21 '12 at 17:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.