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.AbstractButton;

import javax.swing.Icon;

import javax.swing.JComponent;

import javax.swing.SwingUtilities;

public class Main {
    /**
     * Layouts a button and determines where it should be painted as well as 
     * where the icon and text should be painted.
     * 
     * @param b the button to layout
     * @param viewRect the rectangle for the entire view
     * @param iconRect the rectangle for the icon
     * @param textRect the rectangle for the text
     * @return the clipped text that the button should display
     * @see SwingUtilities#layoutCompoundLabel(JComponent, 
     * java.awt.FontMetrics, String, Icon, int, int, int, int, Rectangle, 
     * Rectangle, Rectangle, int)
     */
    public static String layoutButton(AbstractButton b, Icon icon, Rectangle viewRect, Rectangle iconRect,
            Rectangle textRect) {
        setViewBounds(b, viewRect);

        return layoutCustomButton(b, icon, 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 layoutCustomButton(AbstractButton b, Icon icon, Rectangle viewRect, Rectangle iconRect,
            Rectangle textRect) {
        return layoutComponent(b, b.getText(), icon, b.getIconTextGap(), b.getVerticalAlignment(),
                b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 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);
    }

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