1
/* $Revision: 7636 $ $Author: egonw $ $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
3
* Copyright (C) 2006-2007 Christoph Steinbeck <steinbeck@users.sf.net>
5
* Contact: cdk-devel@lists.sourceforge.net
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public License
9
* as published by the Free Software Foundation; either version 2.1
10
* of the License, or (at your option) any later version.
11
* All we ask is that proper credit is given for our work, which includes
12
* - but is not limited to - adding the above copyright notice to the beginning
13
* of your source code files, and to any copyright notice that you may distribute
14
* with programs based on this work.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25
package org.openscience.cdk.renderer;
27
import org.openscience.cdk.geometry.GeometryTools;
28
import org.openscience.cdk.interfaces.IAtom;
29
import org.openscience.cdk.interfaces.IAtomContainer;
30
import org.openscience.cdk.tools.LoggingTool;
32
import javax.vecmath.Point2d;
33
import javax.vecmath.Vector2d;
35
import java.awt.geom.Arc2D;
36
import java.awt.geom.Ellipse2D;
39
* A Renderer class which draws 2D representations of curly arrows as used for
40
* representing electron movements in organic chemistry text books.
41
* Adapted from <a href="http://www.wam.umd.edu/~petersd/Interp2_code.java">http://www.wam.umd.edu/~petersd/Interp2_code.java</a>.
45
* @cdk.created 2006-03-28
46
* @cdk.keyword viewer, 2D-viewer
47
* @see org.openscience.cdk.renderer.Renderer2DModel
49
public class ArrowRenderer2D {
53
protected LoggingTool logger;
59
//private Vector points = new Vector(16, 4);
61
//private int precision = 10;
65
* Constructs a ArrowRenderer2D with a default settings model.
67
public ArrowRenderer2D() {
68
this(new Renderer2DModel());
72
* Constructs a ArrowRenderer2D.
75
* The settings model to use for renderingraphics.
77
public ArrowRenderer2D(Renderer2DModel r2dm) {
79
logger = new LoggingTool(this);
85
public void paintArrows(Arrow[] arrows, Graphics2D graphics)
88
for (int f = 0; f < arrows.length; f++)
90
paintArrow(arrows[f], graphics);
94
void paintArrow(Arrow arrow, Graphics2D graphics)
96
Dimension ss = r2dm.getBackgroundDimension();
97
Arc2D.Double arc = contructArc(arrow);
99
Polygon polygon = contructArrowHead(arc);
100
graphics.fill(polygon);
101
graphics.draw(contructArrowHead(arc));
102
graphics.draw(new Ellipse2D.Double(center.x, ss.getHeight() - center.y, 10,10));
105
public Arc2D.Double contructArc(Arrow arrow)
107
Dimension ss = r2dm.getBackgroundDimension();
108
IAtom start = arrow.getStart();
109
IAtom end = arrow.getEnd();
110
Arc2D.Double arc = new Arc2D.Double();
111
IAtomContainer atomContainer = start.getBuilder().newAtomContainer();
112
atomContainer.addAtom(start);
113
atomContainer.addAtom(end);
114
center = GeometryTools.get2DCenter(atomContainer,r2dm.getRenderingCoordinates());
115
Point2d point1 = new Point2d((Point2d)r2dm.getRenderingCoordinate(start));
116
Point2d point2 = new Point2d((Point2d)r2dm.getRenderingCoordinate(end));
117
Vector2d vector1 = new Vector2d(point1);
118
Vector2d vector2 = new Vector2d(point2);
119
vector2.sub(vector1);
122
rotAngle = GeometryTools.getAngle(vector2.x, vector2.y);
123
offsetAngle = rotAngle + (Math.PI/2);
124
vector3 = new Vector2d(Math.cos(offsetAngle), Math.sin(offsetAngle));
128
logger.debug("rotAngle: ", rotAngle * 360 / (Math.PI * 2));
129
logger.debug("offsetAngle: ", offsetAngle * 360 / (Math.PI * 2));
130
arc.setArcByCenter(center.x, ss.height - center.y, point1.distance(point2)/2,(rotAngle* 360 / (Math.PI * 2)),180,Arc2D.OPEN );
135
public Polygon contructArrowHead(Arc2D.Double arc)
138
Polygon polygon = new Polygon();
139
double wingOffset = (Math.PI/18);
140
Vector2d vector2 = new Vector2d(Math.cos(offsetAngle - wingOffset ), Math.sin(offsetAngle - wingOffset));
141
Vector2d vector3 = new Vector2d(Math.cos(offsetAngle + wingOffset), Math.sin(offsetAngle + wingOffset ));
142
Point2d point2 = new Point2d(arc.getStartPoint().getX(), arc.getStartPoint().getY());
143
Point2d point3 = new Point2d(arc.getStartPoint().getX(), arc.getStartPoint().getY());
148
//Dimension ss = r2dm.getBackgroundDimension();
151
polygon.addPoint((int)arc.getStartPoint().getX(), (int)arc.getStartPoint().getY());
152
polygon.addPoint((int)point2.x, (int)point2.y);
153
polygon.addPoint((int)point3.x, (int)point3.y);