Here you can find the source of drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2)
Parameter | Description |
---|---|
g | Graphics2D |
x1 | position x where arrow starts |
y1 | position y where arrow starts |
x2 | position x where arrow ends |
y2 | position y where arrow ends |
public static void drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2)
//package com.java2s; /*//from w w w . j a v a2 s. c o m * USE - UML based specification environment * Copyright (C) 1999-2010 Mark Richters, University of Bremen * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.awt.Graphics2D; import java.awt.geom.AffineTransform; public class Main { /** * Draw arrow of a message in communication diagram * * @param g * Graphics2D * @param x1 * position x where arrow starts * @param y1 * position y where arrow starts * @param x2 * position x where arrow ends * @param y2 * position y where arrow ends */ public static void drawArrow(final Graphics2D g, final int x1, final int y1, final int x2, final int y2) { final int ARR_SIZE = 5; // Get the current transform AffineTransform saveAT = g.getTransform(); double dx = x2 - x1, dy = y2 - y1; double angle = Math.atan2(dy, dx); int len = (int) Math.sqrt(dx * dx + dy * dy); AffineTransform at = AffineTransform.getTranslateInstance(x1, y1); at.concatenate(AffineTransform.getRotateInstance(angle)); // Perform transformation g.transform(at); // Draw horizontal arrow starting in (0, 0) g.drawLine(0, 0, (int) len, 0); g.fillPolygon(new int[] { len, len - ARR_SIZE, len - ARR_SIZE, len }, new int[] { 0, -ARR_SIZE, ARR_SIZE, 0 }, 4); // Restore original transform g.setTransform(saveAT); } }