Re: drawing arrows...



"MacRules" <dodeca001@xxxxxxx> wrote in message news:<1120049738.356987.83100@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>...
> 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

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);
}
}
.