谁能解释一下,我有 3 种方法可以创建 Image 对象,但只有最后一个对象(k)可以使用 getscalledinstance()成功缩放。
public class scalingTest extends JPanel {
public static Image i;
public static Image j;
public static Image k;
scalingTest() throws IOException
{
i = Toolkit.getDefaultToolkit().getImage("C:/Users/Prashant/Desktop/2.png");
j = new ImageIcon("C:/Users/Prashant/Desktop/2.png").getImage();
k = ImageIO.read(new File("C:/Users/Prashant/Desktop/2.png"));
}
public static void main(String s[]) throws IOException
{
JFrame jf = new JFrame("Scaling TEST");
jf.add(new scalingTest());
jf.setSize(500, 500);
jf.setVisible(true);
}
@Override
public void paintComponent(Graphics g)
{
g.drawImage(i.getScaledInstance(100, 100, Image.SCALE_DEFAULT), 0, 0, this);
g.drawImage(j.getScaledInstance(100, 100, Image.SCALE_DEFAULT), 100, 100, this);
g.drawImage(k.getScaledInstance(100, 100, Image.SCALE_DEFAULT), 200, 200, this);
}
}