Reputation: 8580
Well i use this code to paint my Graph's Edges, If the user wants to see the graph in its Flow mode then i draw arrows instead of lines, all works perfectly, UNTILL I CHANGE WINDOW SIZE, then the arrows starting point (x1,y1) get moved unsynchronizely with my nodes leaving them unconnected, while drawLine() works perfect.
is this something to do with the AffineTransform?
(the arrows painting methodology is adopted from here) )
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
Graphics2D g = (Graphics2D) g1.create();
int arrowSize = 5;
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));
g.setTransform(at);
g.drawLine(0, 0, len, 0);
g.setStroke(new BasicStroke(1));
g.drawLine(len/3, arrowSize , len/3-3*arrowSize, 0);
g.drawLine(len/3, -arrowSize , len/3-3*arrowSize, 0);
}
to whoever wandered how the paint Edge method look like:
public void paintEdge(Graphics g) {
Point uCenter = u.getCenter();
Point vCenter = v.getCenter();
g.setColor(color);
Graphics2D g2=(Graphics2D) g;
g2.setStroke(new BasicStroke(3));
if (mode==Mode.FLOW) {
int minx=Math.min(uCenter.x, vCenter.x),miny=Math.min(uCenter.y, vCenter.y);
int maxx=Math.max(uCenter.x, vCenter.x),maxy=Math.max(uCenter.y, vCenter.y);
g2.drawString(""+ f + " / " + c,10+minx + (maxx-minx)/2,10+miny+ (maxy- miny)/2);
drawArrow(g2,uCenter.x, uCenter.y, vCenter.x, vCenter.y);
} else {
g2.drawLine(uCenter.x, uCenter.y, vCenter.x, vCenter.y);
}
g2.setStroke(new BasicStroke(1));
}
Upvotes: 1
Views: 636
Reputation: 8580
void drawArrow(Graphics2D g, int x1, int y1, int x2, int y2) {
AffineTransform prev = g.getTransform();
int arrowSize = 5;
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.rotate(angle);
g.transform(at);
g.drawLine(0, 0, len, 0);
g.setStroke(new BasicStroke(1));
g.drawLine(len/3, arrowSize , len/3-3*arrowSize, 0);
g.drawLine(len/3, -arrowSize , len/3-3*arrowSize, 0);
g.setTransform(prev);
}
this is how its done, to whoever wondered.
the problem was, that i used g.setTransform(at); instead of g.transform(at); this was a hard one.
Upvotes: 2