use of edu.cmu.tetrad.graph.Endpoint in project tetrad by cmu-phil.
the class AbstractWorkbench method toggleEndpoint.
private void toggleEndpoint(IDisplayEdge graphEdge, int endpointNumber) {
Edge edge = graphEdge.getModelEdge();
Edge newEdge;
if (endpointNumber == 1) {
Endpoint endpoint = edge.getEndpoint1();
Endpoint nextEndpoint;
if (endpoint == Endpoint.TAIL) {
nextEndpoint = Endpoint.ARROW;
} else if (endpoint == Endpoint.ARROW) {
nextEndpoint = Endpoint.CIRCLE;
} else {
nextEndpoint = Endpoint.TAIL;
}
newEdge = new Edge(edge.getNode1(), edge.getNode2(), nextEndpoint, edge.getEndpoint2());
} else if (endpointNumber == 2) {
Endpoint endpoint = edge.getEndpoint2();
Endpoint nextEndpoint;
if (endpoint == Endpoint.TAIL) {
nextEndpoint = Endpoint.ARROW;
} else if (endpoint == Endpoint.ARROW) {
nextEndpoint = Endpoint.CIRCLE;
} else {
nextEndpoint = Endpoint.TAIL;
}
newEdge = new Edge(edge.getNode1(), edge.getNode2(), edge.getEndpoint1(), nextEndpoint);
} else {
throw new IllegalArgumentException("Endpoint number should be 1 or 2.");
}
getGraph().removeEdge(edge);
try {
boolean added = getGraph().addEdge(newEdge);
if (!added) {
getGraph().addEdge(edge);
return;
}
} catch (IllegalArgumentException e) {
getGraph().addEdge(edge);
return;
}
repaint();
}
use of edu.cmu.tetrad.graph.Endpoint in project tetrad by cmu-phil.
the class DisplayEdge method drawCircleEndpoint.
/**
* Draws a circle endpoint at the 'to' point angled as if coming from the
* 'from' point.
*/
private void drawCircleEndpoint(Point from, Point to, Graphics g) {
// final int diameter = 13;
int diameter = 12 + (int) getStrokeWidth();
double a = to.x - from.x;
double b = from.y - to.y;
double theta = Math.atan2(b, a);
// int itheta = (int) ((theta * 360.0) / (2.0 * Math.PI) + 180);
int xminus = (int) (Math.cos(theta) * diameter / 2);
int yplus = (int) (Math.sin(theta) * diameter / 2);
g.fillOval(to.x - xminus - diameter / 2, to.y + yplus - diameter / 2, diameter, diameter);
Color c = g.getColor();
g.setColor(Color.white);
g.fillOval(to.x - xminus - diameter / 4 - 1, to.y + yplus - diameter / 4 - 1, (int) (diameter / 1.4), (int) (diameter / 1.4));
g.setColor(c);
}
use of edu.cmu.tetrad.graph.Endpoint in project tetrad by cmu-phil.
the class DisplayEdge method drawEndpoints.
/**
* Draws endpoints appropriate to the type of edge this is.
*
* @param pp the point pair which specifies where and at what angle the
* endpoints are to be drawn. The 'from' endpoint is drawn at the
* 'from' point angled as it it were coming from the 'to'
* endpoint, and vice-versa.
* @param g the graphics context.
*/
protected final void drawEndpoints(PointPair pp, Graphics g) {
if (this.getModelEdge() != null) {
Endpoint endpointA = this.getModelEdge().getEndpoint1();
Endpoint endpointB = this.getModelEdge().getEndpoint2();
if (endpointA == Endpoint.CIRCLE) {
drawCircleEndpoint(pp.getTo(), pp.getFrom(), g);
} else if (endpointA == Endpoint.ARROW) {
drawArrowEndpoint(pp.getTo(), pp.getFrom(), g);
}
if (endpointB == Endpoint.CIRCLE) {
drawCircleEndpoint(pp.getFrom(), pp.getTo(), g);
} else if (endpointB == Endpoint.ARROW) {
drawArrowEndpoint(pp.getFrom(), pp.getTo(), g);
}
} else {
switch(this.type) {
case DIRECTED:
drawArrowEndpoint(pp.getFrom(), pp.getTo(), g);
break;
case NONDIRECTED:
drawCircleEndpoint(pp.getTo(), pp.getFrom(), g);
drawCircleEndpoint(pp.getFrom(), pp.getTo(), g);
break;
case UNDIRECTED:
break;
case PARTIALLY_ORIENTED:
drawCircleEndpoint(pp.getTo(), pp.getFrom(), g);
drawArrowEndpoint(pp.getFrom(), pp.getTo(), g);
break;
case BIDIRECTED:
drawArrowEndpoint(pp.getFrom(), pp.getTo(), g);
drawArrowEndpoint(pp.getTo(), pp.getFrom(), g);
break;
default:
throw new IllegalArgumentException();
}
}
}
use of edu.cmu.tetrad.graph.Endpoint in project tetrad by cmu-phil.
the class DisplayEdge method drawEdge.
/**
* Draws the actual edge.
*
* @param g the graphics context.
*/
private void drawEdge(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
getConnectedPoints().getFrom().translate(-getLocation().x, -getLocation().y);
getConnectedPoints().getTo().translate(-getLocation().x, -getLocation().y);
setClickRegion(null);
int x1 = getConnectedPoints().getFrom().x;
int y1 = getConnectedPoints().getFrom().y;
int x2 = getConnectedPoints().getTo().x;
int y2 = getConnectedPoints().getTo().y;
// Endpoint endpointA = this.getModelEdge().getEndpoint1();
// Endpoint endpointB = this.getModelEdge().getEndpoint2();
//
// if (endpointA == Endpoint.ARROW) {
// x1 += Math.signum(x2 - x1) * getStrokeWidth();
// y1 += Math.signum(x2 - x1) * getStrokeWidth();
// }
//
// if (endpointB == Endpoint.ARROW) {
// x2 += Math.signum(x1 - x2) * getStrokeWidth();
// y2 += Math.signum(x1 - x2) * getStrokeWidth();
// }
// This silly-looking next line is required to get around an annoying
// bug that appears in Windows (but not Linux) in JDK
// 1.4.2_08 and 1.5.0_02 at least. If you display an editor, then
// select the "Save Screenshot" menu item, then do it again (or
// select the "Save Graph Image" menu item), all of the lines drawn
// by Graphics2D.drawLine in this class get atomized and distributed
// across the entire dialog, and this atomization doesn't go away
// even if you close the editor and reopen it. Must be some
// undocumented interaction between the two graphics contexts
// (the one drawing the image on screen and the other drawing it
// in the Graphics2D from BufferedImage). In any case, any stroke
// width <= 1.0 seems to cause the problem, so we pick a stroke
// width slightly greater than 1.0. jdramsey 4/16/2005
// g2d.setStroke(new BasicStroke(1.000001f));
BasicStroke s;
if (bold) {
s = new BasicStroke(3.0f);
} else {
s = new BasicStroke(getStrokeWidth() + 0.000001f);
}
g2d.setStroke(s);
if (!isSelected()) {
g2d.setColor(lineColor);
}
g2d.drawLine(x1, y1, x2, y2);
if (!isShowAdjacenciesOnly()) {
drawEndpoints(getConnectedPoints(), g);
}
firePropertyChange("newPointPair", null, getConnectedPoints());
}
use of edu.cmu.tetrad.graph.Endpoint in project tetrad by cmu-phil.
the class KnowledgeDisplayEdge method drawCircleEndpoint.
/**
* Draws a circle endpoint at the 'to' point angled as if coming from the
* 'from' point.
*/
private static void drawCircleEndpoint(Point from, Point to, Graphics g) {
final int diameter = 13;
double a = to.x - from.x;
double b = from.y - to.y;
double theta = Math.atan2(b, a);
// int itheta = (int) ((theta * 360.0) / (2.0 * Math.PI) + 180);
int xminus = (int) (Math.cos(theta) * diameter / 2);
int yplus = (int) (Math.sin(theta) * diameter / 2);
g.fillOval(to.x - xminus - diameter / 2, to.y + yplus - diameter / 2, diameter, diameter);
Color c = g.getColor();
g.setColor(Color.white);
g.fillOval(to.x - xminus - diameter / 4 - 1, to.y + yplus - diameter / 4 - 1, (int) (diameter / 1.4), (int) (diameter / 1.4));
g.setColor(c);
}