views:

334

answers:

1

I am trying to display an image on a Frame, however the image doesn't fit exactly on the frame. How can I resize the image? I cannot resize the frame. I need it to stay the same size all the time.

  // 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);
      }
  }
+4  A: 

Use one of the drawImage methods, where you can provide the required width and height of the drawing area. E.g:

g.drawImage(img, 0+I.left, 0+I.top, myWidth, myHeight, this);
jarnbjo
ok that was easy. thanks