Here you can find the source of drawDirectionalTriangle(Graphics2D bbg, double heading, double x, double y, double sideLength, Color fillColor, Color borderColor)
Parameter | Description |
---|---|
bbg | The graphics2D object |
heading | The direction its heading in radians |
x | The x coordinate |
y | The y coordinate |
sideLength | The length of the longest two sides |
fillColor | The color of the body of the triangle |
borderColor | The border color of the triangle |
public static void drawDirectionalTriangle(Graphics2D bbg, double heading, double x, double y, double sideLength, Color fillColor, Color borderColor)
//package com.java2s; /*//from w w w .j a va 2 s .com * The Bio-inspired Leadership Toolkit is a set of tools used to simulate the * emergence of leaders in multi-agent systems. Copyright (C) 2014 Southern * Nazarene University 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 3 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, see * <http://www.gnu.org/licenses/>. */ import java.awt.Color; import java.awt.Graphics2D; import java.awt.Polygon; public class Main { /** * Draws a directional triangle on a graphics 2D * * @param bbg The graphics2D object * @param heading The direction its heading in radians * @param x The x coordinate * @param y The y coordinate * @param sideLength The length of the longest two sides * @param fillColor The color of the body of the triangle * @param borderColor The border color of the triangle */ public static void drawDirectionalTriangle(Graphics2D bbg, double heading, double x, double y, double sideLength, Color fillColor, Color borderColor) { Polygon triangle = new Polygon(); // add a point straight ahead and slightly longer than side length triangle.addPoint(0, (int) (1.5 * sideLength)); // add the two side points slightly off to the right and left of center triangle.addPoint((int) (sideLength / 2), 0); triangle.addPoint((int) (-sideLength / 2), 0); // move the origin of drawing to the x and y coordinate to be drawn bbg.translate(x, y); // rotate the g2d object by heading bbg.rotate(heading); // set the fill color and draw bbg.setColor(fillColor); bbg.fill(triangle); // set the border color and draw bbg.setColor(borderColor); bbg.draw(triangle); // undo the transformations bbg.rotate(-heading); bbg.translate(-x, -y); } }