Here you can find the source of fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height)
static protected void fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height)
//package com.java2s; //License from project: LGPL import java.awt.Graphics2D; import javax.swing.JComponent; public class Main { /**/*w ww . j a v a 2 s .c om*/ * Scale the current graphic so that it fits within the * given width and height. */ static protected void fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height) { double compWidth = (double) component.getWidth(); double compHeight = (double) component.getHeight(); double xscale = (double) width / compWidth; double yscale = (double) height / compHeight; if (xscale < yscale) { yscale = xscale; } else { xscale = yscale; } // Apply the scale. g2.scale(xscale, yscale); } }