0

I'm trying to do two things here 1. I have an imageIcon in my code ( called dice ) that when you press on it, it changes to a moving imageIcon ( called dice2 ). however i failed resizing the moving imageIcon 2. setting a timer for dice2 and when its done "dice" shows up

here is my code so far

  ImageIcon dice = new ImageIcon("dice.png") ;
  Image image = dice.getImage(); // transform it 
  Image newimg = image.getScaledInstance(150, 120,java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
  dice = new ImageIcon(newimg) ;
  dice_1 = new JButton(dice);           
  dice_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
          dicePanel.remove(dice_1);
          dicePanel.revalidate();
          dicePanel.repaint();                
          ImageIcon dice2 = new ImageIcon("dice.gif");
          dice_1 = new JButton(dice2);
          dice_1.setOpaque(false);
          dice_1.setContentAreaFilled(false);
          dice_1.setBorderPainted(false);
          dicePanel.add(dice_1);  
          // random number 
          Random r = new Random();
          R = r.nextInt(10) + 2;
          randomNumbe.setText("تقدم " + R + " خطوة");
      }
  });
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
Neyon
  • 43
  • 8
  • I'm not sure if you can scale a gif within Java (or at least not without a large amount of work), it would be better to scale it externally, that our have size images, one for each side, and using a Swing `Timer`, randomly switch the images yourself – MadProgrammer Sep 09 '15 at 11:24
  • is there an example that i can follow of Timer? – Neyon Sep 09 '15 at 11:27
  • For [example](http://stackoverflow.com/questions/28342538/java-swing-timer-and-animation-how-to-put-it-together/28342986#28342986), [example](http://stackoverflow.com/questions/21034679/jframe-shows-up-grey-blank/21034770#21034770) – MadProgrammer Sep 09 '15 at 11:33

1 Answers1

0

The area-averaging algorithm that backs both SCALE_SMOOTH and SCALE_AREA_AVERAGING does not appear to work with animated GIFs. When I use it, I get a blank image as a result.

SCALE_DEFAULT works great, though.

JakeRobb
  • 1,711
  • 1
  • 17
  • 32