Calculate the desired X offset given the specified image, bounding rectangle, alignment. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Calculate the desired X offset given the specified image, bounding rectangle, alignment.

Demo Code

/*//from w  ww  .ja  v a2  s  .c  om
 * @(#) GUIUtils
 *
 * Copyright (C) 2007 Pingtel Corp., certain elements licensed under a Contributor Agreement.  
 * Contributors retain copyright to elements licensed under a Contributor Agreement.
 * Licensed to the User under the LGPL license.
 *
 */
//package com.java2s;
import java.awt.*;

public class Main {
    /** Align the component to the east along the horizonal axis. */
    public static final int ALIGN_EAST = 0x0010;
    /** Align the component to the west along the horizonal axis. */
    public static final int ALIGN_WEST = 0x0001;

    /**
     * Calculate the desired X offset given the specified image, bounding
     * rectangle, alignment.
     */
    public static int calcXImageOffset(Image image, Rectangle rectBounds,
            int iAlignment) {
        int xOffset = 0;
        int iWidth;

        if (image != null) {
            iWidth = image.getWidth(null);

            // Align Flush Right
            if ((iAlignment & ALIGN_EAST) == ALIGN_EAST) {
                xOffset = rectBounds.x
                        + Math.max(rectBounds.width - iWidth, 0);
            }
            // Align Flush Left
            else if ((iAlignment & ALIGN_WEST) == ALIGN_WEST) {
                xOffset = rectBounds.x;
            }
            // Center
            else {
                xOffset = rectBounds.x
                        + Math.max((rectBounds.width - iWidth) / 2, 0);
            }
        }

        return xOffset;
    }
}

Related Tutorials