2010-02-23 319 views
1

我试图在一个帧上显示图像,但图像并不完全适合在框架上。我怎样才能调整图像大小?我无法调整框架大小。我需要它始终保持相同的大小。用Java调整图像大小。如何调整大小

// Override paint(): 
    public void paint (Graphics g) 
    { 
     Dimension dimension = this.getSize(); 
     Insets I = this.getInsets(); 

     Toolkit tk = Toolkit.getDefaultToolkit(); 
     try{ 
      URL u = new URL ("http://www.gstatic.com/hostedimg/c195c33263b5205c_large"); 
      Image img = tk.getImage(u); 
      g.drawImage(img, 0+I.left, 0+I.top, this); 
     } 
     catch(MalformedURLException e){ 
      System.out.println(e); 
     } 
    } 
+2

您不应该在paint()方法中读取图像。 – camickr 2010-02-24 04:37:03

回答

4

使用其中一种drawImage方法,您可以在其中提供所需的绘图区宽度和高度。例如:

g.drawImage(img, 0+I.left, 0+I.top, myWidth, myHeight, this); 
+1

好吧,那很容易。谢谢 – user69514 2010-02-23 23:18:50