截取点代码片断,你自己看着改
BufferedImage bimg = nullImage img = null
int width, height
try {
bimg = ImageIO.read(u)
if(bimg.getWidth() > bimg.getHeight())
{
width = 40
height = bimg.getHeight() * width / bimg.getWidth()
}
else
{
height = 40
width = bimg.getWidth() * height / bimg.getHeight()
}
img = bimg.getScaledInstance(width, height, Image.SCALE_DEFAULT)
pre_image = bimg.getScaledInstance(width * 10, height * 10, Image.SCALE_DEFAULT)
} catch (IOException e) {
e.printStackTrace()
}
用Image中的getScaledInstance方法得到一个按照指定宽度和高度缩放以后的Image实例,然后再用setImage方法设置ImageIcon所显示的图像一下示例:
import javax.swing.*
import java.awt.*
public class test extends JFrame
{
private ImageIcon img
private JLabel showImg
private final static int WIDTH=147
private final static int HEIGHT=136
public test()
{
img=new ImageIcon("1.png")
img.setImage(img.getImage().getScaledInstance(test.WIDTH,test.HEIGHT,Image.SCALE_DEFAULT))
showImg=new JLabel()
showImg.setIcon(img)
this.add(showImg,BorderLayout.CENTER)
this.setBounds(300,200,400,300)
this.pack()
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setVisible(true)
}
public static void main(String args[])
{
new test()
}
}