user69514
user69514

Reputation: 27629

Resize Image in Java. How to resize

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);
      }
  }

Upvotes: 1

Views: 1978

Answers (1)

jarnbjo
jarnbjo

Reputation: 34323

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);

Upvotes: 4

Related Questions