Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Insets;

import java.awt.Rectangle;

import javax.swing.Icon;

import javax.swing.JComponent;
import javax.swing.JLabel;

import javax.swing.SwingUtilities;

public class Main {
    public static String layoutLabel(JLabel label, Icon icon, Rectangle viewRect, Rectangle iconRect,
            Rectangle textRect) {
        setViewBounds(label, viewRect);

        return layoutComponent(label, label.getText(), icon, label.getIconTextGap(), label.getVerticalAlignment(),
                label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(),
                viewRect, iconRect, textRect);
    }

    /**
     * Sets where a component should be painted. It uses the 
     * {@code JComponent}'s width and height and excludes the {@code Insets}.
     * 
     * @param c the component
     * @param viewRect the bounds for painting the component
     */
    public static void setViewBounds(JComponent c, Rectangle viewRect) {
        Insets i = c.getInsets();

        viewRect.x = i.left;
        viewRect.y = i.top;
        viewRect.width = c.getWidth() - (i.right + viewRect.x);
        viewRect.height = c.getHeight() - (i.bottom + viewRect.y);
    }

    public static String layoutComponent(JComponent c, String text, Icon icon, int iconTextGap,
            int verticalAlignment, int horizontalAlignment, int verticalTextAlignment, int horizontalTextAlignment,
            Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
        resetRectangles(iconRect, textRect);

        return SwingUtilities.layoutCompoundLabel(c, c.getFontMetrics(c.getFont()), text, icon, verticalAlignment,
                horizontalAlignment, verticalTextAlignment, horizontalTextAlignment, viewRect, iconRect, textRect,
                iconTextGap);
    }

    /**
     * Resets the specified {@code Rectangle}'s properties to 0.
     * 
     * @param rects the rectangles to reset
     */
    public static void resetRectangles(Rectangle... rects) {
        for (Rectangle r : rects) {
            r.x = r.y = r.width = r.height = 0;
        }
    }
}