2
\$\begingroup\$

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);
    }
\$\endgroup\$
  • \$\begingroup\$ You just want to scale them once then you're done? And you have a lot of sprites? \$\endgroup\$ – MichaelHouse Mar 21 '12 at 15:09
  • \$\begingroup\$ Yeah, I have a lot of sprites. I need to enlarge them because they're all approx. 22x37px. Too small to see. \$\endgroup\$ – Tyler Mar 21 '12 at 17:19
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Mar 21 '12 at 17:48
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Mar 21 '12 at 17:43
  • \$\begingroup\$ You can still post your answer, it could very well be better than mine. \$\endgroup\$ – Josh Mar 21 '12 at 17:44
  • \$\begingroup\$ Na, you got it pretty well covered. \$\endgroup\$ – MichaelHouse Mar 21 '12 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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