Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters : Geometry « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » 2D Graphics GUI » GeometryScreenshots 
Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters
        
//package com.myapp.util.image;

import java.awt.Dimension;

public class ImageUtil {
    
    /**
     * returns a dimension where width and height are inside the bounds of the
     * maxWidth and maxHeight parameters<br>
     * and the aspect ratio is the same as sourceWidth and sourceHeight.
     
     @param maxWidth
     *            the maximum width of the target dimension
     @param maxHeight
     *            the maximum height of the target dimension
     @param sourceWidth
     *            the widht of the source image
     @param sourceHeight
     *            the height of the source image
     @return the target dimension that will be the greatest dimension that
     *         <ul>
     *         <li>will keep the ratio of the source images dimension</li>
     *         <li>fits in the bounds of the maximum dimension given</li>
     *         </ul>
     */
    public static Dimension scaleDimensions(int maxWidth,
                                     int maxHeight,
                                     int sourceWidth,
                                     int sourceHeight) {
        float vidH, vidW, maxH, maxW;
        vidW = Integer.valueOf(sourceWidth).floatValue();
        vidH = Integer.valueOf(sourceHeight).floatValue();
        maxW = Integer.valueOf(maxWidth).floatValue();
        maxH = Integer.valueOf(maxHeight).floatValue();
        
//        System.out.println("initial height / width ratio: " + vidH / vidW);
//        System.out.println();
//        System.out.println("vidW "+vidW+", vidH "+vidH);
//        System.out.println("maxW "+maxW+", maxH "+maxH);
//        System.out.println();

        float finalW, finalH;
        finalH = vidH / vidW * maxW;
        finalW = maxW;
        
        if (finalH > maxH) {
            float factor = maxH / finalH;
            finalH = maxH;
            finalW = finalW * factor;
        }

        int w = Float.valueOf(finalW).intValue();
        int h = Float.valueOf(finalH).intValue();
        
        Dimension d = new Dimension(w,h);

//        System.out.println();
//        System.out.println("finalW "+finalW+", finalH "+finalH);
//        System.out.println("final height / width ratio: " + finalH / finalW);
//        System.out.println();
//        System.out.println(d);
        
        return d;
    }

    
    
    // test scaleDimensions
    public static void main(String[] args) {
        int maxWidth = 250;
        int maxHeight = 200;
        int videoWidth = 320;
        int videoHeight = 240;
        Dimension d = scaleDimensions(maxWidth,
                                      maxHeight,
                                      videoWidth,
                                      videoHeight);
        System.out.println(d);
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.Collection of geometry utility methods
2.Unions Rectangle2D
3.Interpolates points given in the 2D plane
4.Returns distance between two sets of coords
5.Returns distance between 3D set of coords
6.Returns closest point on segment to point
7.Calculate Angle From
8.Returns distance to segment
9.Hexagon demo
10.Implements an Vector in 3D space.
11.Implementation of the 4 dimensional vector.
12.Quaternion
13.Circle shape
14.Geometry Utilities
15.This is a Polygon that allows the user to flip and swap the points along it's axis.
16.Fast trigonometric operationsFast trigonometric operations
17.A class to represent a latitude and longitude
18.An undirected graph that keeps track of connected components (groups).
19.Generates n logarithmically-spaced points between d1 and d2 using the provided base.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.