Java Integer Align align(int size, int space, int weight)

Here you can find the source of align(int size, int space, int weight)

Description

Return the coordinate where something of size 'size' starts in a space of size 'space', with 'weight' as the alignment.

License

Open Source License

Parameter

Parameter Description
size size to align
space space to align in
weight -1 for align left/top, 0 for center, 1 for right/bottom

Declaration

private static int align(int size, int space, int weight) 

Method Source Code

//package com.java2s;
/*// ww w. j  a v a  2s. c o m
  ImageUtils.java
    
  (c) 2009-2012 Edward Swartz
    
  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v1.0
  which accompanies this distribution, and is available at
  http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    public static final int ALIGN_LEFT = -1;
    public static final int ALIGN_CENTER = 0;
    /** Center if image fits, else top. */
    public static final int ALIGN_CENTER_OR_TOP = 1;

    /**
     * Return the coordinate where something of size 'size' starts
     * in a space of size 'space', with 'weight' as the alignment.  
     * @param size size to align 
     * @param space space to align in
     * @param weight -1 for align left/top, 0 for center, 1 for right/bottom
     * @return
     */
    private static int align(int size, int space, int weight) {
        if (weight == ALIGN_LEFT) {
            return 0;
        } else if (weight == ALIGN_CENTER) {
            return (space - size) / 2;
        } else if (weight == ALIGN_CENTER_OR_TOP) {
            return size <= space ? (space - size) / 2 : 0;
        } else /* ALIGN_RIGHT */ {
            return space - size;
        }
    }
}

Related

  1. align(int min, int number, int max)
  2. align(int pos, int align)
  3. align(int size)
  4. align(int size, int align)
  5. align(int size, int alignment)
  6. align(int value, int alignment)