Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Dimension;

import java.awt.Insets;

import java.awt.Rectangle;

import javax.swing.Icon;

import javax.swing.JComponent;

import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class Main {
    public static Dimension getPreferredSize(JComponent c, String text, Icon icon, int iconTextGap,
            int verticalAlignment, int horizontalAlignment, int verticalTextAlignment, int horizontalTextAlignment,
            Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
        viewRect.x = viewRect.y = 0;
        viewRect.width = viewRect.height = Short.MAX_VALUE;

        layoutComponent(c, text, icon, iconTextGap, verticalAlignment, horizontalAlignment, verticalTextAlignment,
                horizontalTextAlignment, viewRect, iconRect, textRect);

        Rectangle size = iconRect.union(textRect);
        if (c.getInsets() != null) {
            addInsets(size, c.getInsets());
        }

        return new Dimension(size.width, size.height);
    }

    public static Dimension getPreferredSize(JComponent c, String text, Icon icon, int iconTextGap,
            Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
        int center = SwingConstants.CENTER;
        return getPreferredSize(c, text, icon, iconTextGap, center, center, center, center, viewRect, iconRect,
                textRect);
    }

    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);
    }

    public static void addInsets(Rectangle rect, Insets insets) {
        rect.x += insets.left;
        rect.y += insets.bottom;
        rect.width += insets.left + insets.right;
        rect.height += insets.top + insets.bottom;
    }

    public static void addInsets(Dimension dim, Insets insets) {
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom;
    }

    /**
     * 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;
        }
    }
}