调整具有gif格式的imageIcon的大小

时间:2015-09-09 10:58:27

标签: java

我试图在这里做两件事 1.我的代码中有一个imageIcon(称为dice),当你按下它时,它会变成一个运动的imageIcon(称为dice2)。但是我无法调整运动imageIcon的大小 2.为dice2设置一个计时器,当它完成时#34;骰子"出现

这是我到目前为止的代码

  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 + " خطوة");
      }
  });

1 个答案:

答案 0 :(得分:0)

支持SCALE_SMOOTH和SCALE_AREA_AVERAGING的区域平均算法似乎不适用于动画GIF。当我使用它时,我得到一个空白图像。

SCALE_DEFAULT非常适合。