Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * OpenBench LogicSniffer / SUMP project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * 
 * Copyright (C) 2010-2011 - J.W. Janssen, http://www.lxtreme.nl
 */

import java.awt.*;

public class Main {
    public static final int LEFT_FACING = 1;
    public static final int RIGHT_FACING = -1;

    /**
     * Draws a double headed arrow of 8x8.
     * 
     * @param aG
     *          the canvas to draw on;
     * @param aX1
     *          the starting X position of the arrow;
     * @param aY
     *          the starting Y position of the arrow;
     * @param aX2
     *          the ending X position of the arrow.
     */
    public static final void drawDoubleHeadedArrow(final Graphics aG, final int aX1, final int aY, final int aX2) {
        drawDoubleHeadedArrow(aG, aX1, aY, aX2, aY);
    }

    /**
     * Draws a double headed arrow of 8x8.
     * 
     * @param aG
     *          the canvas to draw on;
     * @param aX1
     *          the starting X position of the arrow;
     * @param aY1
     *          the starting Y position of the arrow;
     * @param aX2
     *          the ending X position of the arrow;
     * @param aY2
     *          the ending Y position of the arrow.
     */
    public static final void drawDoubleHeadedArrow(final Graphics aG, final int aX1, final int aY1, final int aX2,
            final int aY2) {
        drawDoubleHeadedArrow(aG, aX1, aY1, aX2, aY2, 8, 8);
    }

    /**
     * Draws a double headed arrow with arrow heads of a given width and height.
     * 
     * @param aG
     *          the canvas to draw on;
     * @param aX1
     *          the starting X position of the arrow;
     * @param aY1
     *          the starting Y position of the arrow;
     * @param aX2
     *          the ending X position of the arrow;
     * @param aY2
     *          the ending Y position of the arrow;
     * @param aArrowWidth
     *          the total width of the arrow head;
     * @param aArrowHeight
     *          the total height of the arrow head.
     */
    public static final void drawDoubleHeadedArrow(final Graphics aG, final int aX1, final int aY1, final int aX2,
            final int aY2, final int aArrowWidth, final int aArrowHeight) {
        final Graphics2D g2d = (Graphics2D) aG.create();

        final int lineWidth = Math.abs(aX2 - aX1);
        final int threshold = (2 * aArrowWidth) + 2;
        try {
            int x1 = aX1;
            int x2 = aX2;

            if (lineWidth > threshold) {
                drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight);
                // why x2 needs to be shifted by one pixel is beyond me...
                drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight);

                x1 += aArrowWidth - 1;
                x2 -= aArrowWidth + 1;
            }

            g2d.drawLine(x1, aY1, x2, aY2);
        } finally {
            g2d.dispose();
        }
    }

    /**
     * Draws a single arrow head
     * 
     * @param aG
     *          the canvas to draw on;
     * @param aXpos
     *          the X position of the arrow head;
     * @param aYpos
     *          the (center) Y position of the arrow head;
     * @param aFactor
     *          +1 to have a left-facing arrow head, -1 to have a right-facing
     *          arrow head;
     * @param aArrowWidth
     *          the total width of the arrow head;
     * @param aArrowHeight
     *          the total height of the arrow head.
     */
    public static final void drawArrowHead(final Graphics2D aG, final int aXpos, final int aYpos, final int aFactor,
            final int aArrowWidth, final int aArrowHeight) {
        final double halfHeight = aArrowHeight / 2.0;
        final int x1 = aXpos + (aFactor * aArrowWidth);
        final int y1 = (int) Math.ceil(aYpos - halfHeight);
        final int y2 = (int) Math.floor(aYpos + halfHeight);

        final Polygon arrowHead = new Polygon();
        arrowHead.addPoint(aXpos, aYpos);
        arrowHead.addPoint(x1, y1);
        arrowHead.addPoint(x1, y2);

        aG.fill(arrowHead);
    }

    /**
     * Closes and disposes a given {@link Window}.
     * 
     * @param aWindow
     *          the window to close, if <code>null</code>, this method doesn't do
     *          anything.
     */
    public static void dispose(final Window aWindow) {
        if (aWindow == null) {
            return;
        }
        if (aWindow.isVisible()) {
            aWindow.setVisible(false);
        }
        aWindow.dispose();
    }
}