0

I have scaled images easily with the .getScaledInstance when importing them like so:

player_sprite = a_1.getImage().getScaledInstance(150, 150, 100);

But the same cannot be said about scaling animated GIFs as when I do the same method of scaling a GIF it causes issues when displaying it. When using g.drawImage it only draws the first frame then disappears.

Here is the full code for importing the GIF that I use and below will be an example of how I display the animated GIF onto the JFrame:

Importing it:

    ImageIcon a_1 = new ImageIcon(FrontHand.class.getResource("/Sprites/character_move_down.gif"));
    move_player_down = a_1.getImage().getScaledInstance(100, 100, 100);

Displaying it:

    g.drawImage(move_player_down, 100, 100, this);

Is there another simple method I could use for increasing the size of the animated GIF or is there a simple fix for this issue.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
matvey-tk
  • 641
  • 7
  • 18
  • GIF's should scale no differently from other file formats, unless you're talking specifically about *animated* GIF's. If so, you might want to put this critical information in your question. – Hovercraft Full Of Eels Oct 04 '16 at 01:37
  • 1. Passing in `100` as the last parameter to `getScaledInstance` does not make sense. 2. Do you *really* need the scaled *instance*? Can't you just draw it larger (with the appropriate `drawImage` call, or maybe a transformed `Graphics2D`? 3. Do you really need to scale it *in your program*? Can't you use an external tool to create the image with the desired size? Obtaining a scaled instance of an animated gif seems to be surprisingly hard. This http://stackoverflow.com/q/9392227/ is basically a duplicate, but the answer looks very fiddly...EDIT @HovercraftFullOfEels *"draws the first frame"* – Marco13 Oct 04 '16 at 01:48
  • didn't know there was a difference will update it :) – matvey-tk Oct 04 '16 at 01:49
  • In my situation I cannot recreate the gif to be larger – matvey-tk Oct 04 '16 at 01:51
  • @Taka Just a hint: When answering to a comment, using `@` will notify the respective user (as [recently discussed on meta](https://meta.stackoverflow.com/questions/335607) ) – Marco13 Oct 04 '16 at 01:54
  • @Marco13 okay, will note for next time – matvey-tk Oct 04 '16 at 02:08

1 Answers1

1

I have found a solution while going into scalr, I ended not with scalr but instead found a comment on a scalr thread which using .getscaledinstance is changing the size and then printing with paintIcon and not drawImage like so:

Change in size:

ImageIcon a_1 = new ImageIcon(FrontHand.class.getResource("/Sprites/character_move_down.gif"));
a_1.setImage(a_1.getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
move_player_down = a_1;

Painting the icon:

move_player_down.paintIcon(this, g, 100, 100);

It works like a charm and does not require me to change all my previous code to work around it :)

matvey-tk
  • 641
  • 7
  • 18