triangle.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 163 行

JAVA
163
字号
package org.jnode.wt.components;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;

/**
 * This class to draw a triangle in a given Rectangle Area.
 * This can also be called as Arrow Class.
 *
 *
 * This Class allows to specify a Rectangle Area,
 * and returns two int arrays which can be
 * passed to graphics.fillPolygon(int x[], int y[], length);
 *
 * [1] This Object allows to specify Direction of Arrow,
 * [2] And insets Area which it should ignore.
 *
 * @author Kishore
 */
public class Triangle {

    // Direction of Triangle or Arrow FACING.
    public final static int LEFT = 0;
    public final static int RIGHT = 1;
    public final static int TOP = 2;
    public final static int BOTTOM = 3;

    protected int arrow_direction = TOP;

    protected boolean filled = true;

    protected Rectangle triangleArea = null;

    protected Insets insets = null;

    /**
     * Triangle constructor comment.
     */
    public Triangle(int w, int h) {
        this(new Rectangle(0, 0, w, h));
    }

    /**
     * Triangle constructor comment.
     */
    public Triangle(int w, int h, int direction) {
        this(new Rectangle(0, 0, w, h));

        setDirection(direction);
    }

    /**
     * Triangle constructor comment.
     */
    public Triangle(Rectangle r) {
        super();

        this.triangleArea = r;
    }
/*
 * This is "triagnleArea - insets"
 */
    public Rectangle getResulting_RectangleArea() {
        if (insets != null) {
            return Util.substract(this.triangleArea, this.insets);
        }

        return triangleArea;
    }
/*
 * This is "triagnleArea - insets"
 */
    public Dimension getResultingSize() {
        int x = triangleArea.x + insets.left;
        int y = triangleArea.y + insets.top;

        int w = triangleArea.width - insets.right;
        int h = triangleArea.height - insets.bottom;

        return new Dimension(w - x, h - y);
    }

    /**
     * This method returns x,y arrays which can directly
     * passed to graphics.fillPolygon(int x[], int y[], length);
     */

    public Object[] getTriangleXYArrays() {
        int[] xarr = new int[3];
        int[] yarr = new int[3];

        Rectangle r = getResulting_RectangleArea();

        int top_left_X = r.x;
        int top_left_Y = r.y;

        int top_right_X = r.x + r.width;
        int top_right_Y = r.y;

        int bottom_left_X = r.x;
        int bottom_left_Y = r.y + r.height;

        int bottom_right_X = r.x + r.width;
        int bottom_right_Y = r.y + r.height;


        if (BOTTOM == arrow_direction) {
            xarr[1] = top_left_X;
            yarr[1] = top_left_Y;
            xarr[2] = top_right_X;
            yarr[2] = top_right_Y;

            xarr[0] = r.x + (r.width) / 2;
            yarr[0] = r.y + r.height;
        } else if (TOP == arrow_direction) {
            xarr[0] = r.x + (r.width) / 2;
            yarr[0] = r.y;

            xarr[1] = bottom_right_X;
            yarr[1] = bottom_right_Y;
            xarr[2] = bottom_left_X;
            yarr[2] = bottom_left_Y;
        } else if (LEFT == arrow_direction) {
            xarr[0] = r.x;
            yarr[0] = r.y + (r.height) / 2;

            xarr[1] = top_right_X;
            yarr[1] = top_right_Y;
            xarr[2] = bottom_right_X;
            yarr[2] = bottom_right_Y;
        } else if (RIGHT == arrow_direction) {
            xarr[0] = top_left_X;
            yarr[0] = top_left_Y;

            xarr[2] = r.x + r.width;
            yarr[2] = r.y + (r.height) / 2;

            xarr[2] = bottom_left_X;
            yarr[2] = bottom_left_Y;
        }

        //
        Object[] xy = new Object[2];
        xy[0] = xarr;
        xy[1] = yarr;

        return xy;
    }

    public void setDirection(int direction) {
        this.arrow_direction = direction;
    }

    public void setInsets(int t, int l, int r, int b) {
        setInsets(new Insets(t, l, r, b));
    }

    public void setInsets(Insets ins) {
        this.insets = ins;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?