I'm a newcomer to Java programming. I need to draw some arrows in a
window. Drawing lines is no problem, but I don't see an arrow-drawing
capability in the Graphics2D class. It seems that this would be a
common need - anyone ever see any publicly available classes with this
capability?
TIA,
Steve
John B. Matthews - 29 Jun 2005 16:26 GMT
> I'm a newcomer to Java programming. I need to draw some arrows in a
> window. Drawing lines is no problem, but I don't see an arrow-drawing
[quoted text clipped - 4 lines]
> TIA,
> Steve
If nothing better comes along, you might look at fillArc. This is
especially easy if you already know the angle of the line.

Signature
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
iamfractal@hotmail.com - 30 Jun 2005 08:37 GMT
> I'm a newcomer to Java programming. I need to draw some arrows in a
> window. Drawing lines is no problem, but I don't see an arrow-drawing
[quoted text clipped - 4 lines]
> TIA,
> Steve
I didn't find any either.
.ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
package com.edmundkirwan.frac.view.gui.grid;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Dimension;
import com.edmundkirwan.frac.ir1.Options;
import com.edmundkirwan.frac.view.gui.ir1.Connection;
import com.edmundkirwan.frac.view.gui.ir1.RealEstateManager;
/**
* This class is a connection's arrow head pointing east. The arrow head
* itself will be a filled triangle.
*/
class FullEastArrowHead extends CommonArrowHead {
/**
* Constructor - stores the tip of the arrow head.
*
* @param tipCoords coordinates of the arrow head tip
* @param connection connection that owns this arrow head
*/
FullEastArrowHead(Point tipCoords, Connection connection) {
super(tipCoords, connection);
}
/**
* Draws the arrow head into the given graphical context.
*
* @param graphics2D graphics context
*/
public void draw(Graphics2D graphics2D) {
RealEstateManager realEstateManager =
guiRegistry.getDrawFacade().getRealEstateManager();
Dimension directoryDimension = realEstateManager.
getDirectoryDimension(connection.getSourceNode());
double height = directoryDimension.getHeight();
// Establish the basic size of the arrow head
int arrowHeadLength = (int)
(height / Options.ARROW_HEAD_RATIO_TO_DIRECTORY_HEIGHT);
Polygon poly = new Polygon();
int x = (int)tipCoords.getX();
int y = (int)tipCoords.getY();
x = realEstateManager.getScreenXPosition(x);
y = realEstateManager.getScreenYPosition(y);
int startX = x;
int startY = y;
poly.addPoint(startX, startY);
x = x - (arrowHeadLength / 2);
y = y - (arrowHeadLength / 2);
poly.addPoint(x, y);
y = y + arrowHeadLength;
poly.addPoint(x, y);
poly.addPoint(startX, startY);
draw(graphics2D, poly);
}
}
MacRules - 30 Jun 2005 13:37 GMT
Thanks guys. I need an arrow from (x1,x2) to (y1,y2). I was hoping to
be lazy and not have to mess with trigonometry to get this done, but I
can do it if need be. Maybe one of these days the graphics2D class will
support a general-purpose arrow-drawing capability.
Andrew Thompson - 30 Jun 2005 15:08 GMT
> Maybe one of these days the graphics2D class will
> support a general-purpose arrow-drawing capability.
..and maybe if Sun supported every single simple thing
that anybody 'expects' to be part of the API, the API
would be 10 times the size.

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
MacRules - 30 Jun 2005 15:36 GMT
Uh...typical USENET thread degeneration has begun. Thanks for kicking
it off, Andrew.
Andrey Kuznetsov - 30 Jun 2005 08:55 GMT
> I'm a newcomer to Java programming. I need to draw some arrows in a
> window. Drawing lines is no problem, but I don't see an arrow-drawing
> capability in the Graphics2D class. It seems that this would be a
> common need - anyone ever see any publicly available classes with this
> capability?
you can define shape of appropriate form and then use Graphics2D methods
fill(Shape) and draw(Shape)
in com.imagero.gui.swing.Shapes.java I defined some basic shapes (triangles,
arrows, circles, rectangles, twin and quad arrows).
There are also an applet which uses it:
http://jgui.imagero.com/examples/JShape/
here is an arrow looking at north:
public static Area createArrow(int width, int height, int length, int
direction) {
int arrowStart = Math.abs(length - height);
int lineWidth = width / 3;
return new Area(new Polygon(
new int[]{lineWidth, lineWidth, 0, width / 2, width,
width - lineWidth, width - lineWidth},
new int[]{0, arrowStart, arrowStart, length,
arrowStart, arrowStart, 0},
7));
}
you can use AffineTransforn tomake it looking at any direction.

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities