Java Graphics Draw fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height)

Here you can find the source of fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height)

Description

Scale the current graphic so that it fits within the given width and height.

License

LGPL

Declaration

static protected void fitToWidthAndHeight(Graphics2D g2, JComponent component, int width, int height) 

Method Source Code

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

Related

  1. drawUp(Graphics g, int x, int y, int w, int h)
  2. drawWeightBox(Graphics2D g2d, int ycentre, int xcentre, int width)
  3. drawWinUpperLeft(Graphics g, int which, Color fill, int x, int y, int fmWidth, int fmHeight)
  4. drawXMajorScaleTick(Graphics g, int x, int y)
  5. fillVisible(final Graphics g, final JComponent c)
  6. getGraphicsDevice(Component comp)
  7. getTranslatedGraphics(Graphics g, PageFormat pf, int pageIndex, Component component)
  8. getUsableScreenBounds(GraphicsConfiguration gconf)
  9. paint(Graphics2D g)