* ThisTime 2.0 (2008-03-30)
* Copyright 2007 Zach Scrivena
* Simple clock and timer program.
* TERMS AND CONDITIONS:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* Perform miscellaneous graphics-related operations.
* Scale an image to fit a canvas, while keeping the aspect ratio of the image.
* This method takes the widths and heights of the image and canvas, and returns
* the width and height of the scaled image in a Dimension object.
* @param imageWidth
* image width
* @param imageHeight
* image height
* @param canvasWidth
* canvas width
* @param canvasHeight
* canvas height
* Dimension object representing the width and height of the scaled image
static Dimension scaleToFitKeepRatio(
final int imageWidth,
final int imageHeight,
final int canvasWidth,
final int canvasHeight)
final int width;
final int height;
final int unconstrainedWidth = imageWidth * canvasHeight / imageHeight;
final int unconstrainedHeight = imageHeight * canvasWidth / imageWidth;
if (unconstrainedWidth >= canvasWidth)
/* canvas width is the limiting constraint */
width = canvasWidth;
height = unconstrainedHeight;
/* canvas height is the limiting constraint */
width = unconstrainedWidth;
height = canvasHeight;
return new Dimension(width, height);