Permalink
Browse files

add

  • Loading branch information...
chemhack committed Oct 12, 2009
1 parent 1afe000 commit dd58256c151022be1375a653a034ad94a38417c4
View
Binary file not shown.
@@ -1,3 +1,24 @@
/*
* ==================================================
* jsMolEditor: JavaScript based molecule strucutre editor
* ==================================================
* Project Info: http://chemhack.com/jsmoleditor
*
* Copyright (C) 2009. Duan Lian
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.chemhack.jsMolEditor.client.renderer;
import com.chemhack.jsMolEditor.client.model.Molecule;
@@ -7,22 +28,26 @@
import com.chemhack.jsMolEditor.client.jre.emulation.java.awt.geom.Point2D;
import com.chemhack.jsMolEditor.client.widget.ExtendedCanvas;
import com.chemhack.jsMolEditor.client.controller.EditorController;
import com.chemhack.jsMolEditor.client.renderer.color.BasicColorScheme;
public class CanvasRenderer {
EditorController controller;
RendererModel rendererModel;
ExtendedCanvas canvas;
CordTransformer transformer;
BasicColorScheme colorScheme;
public CanvasRenderer(ExtendedCanvas canvas) {
this(null,canvas);
this(null, canvas);
}
public CanvasRenderer(EditorController controller, ExtendedCanvas canvas) {
this.controller = controller;
this.canvas = canvas;
rendererModel = new RendererModel();
transformer=new CordTransformer(54,0,0,-54,-12660,8196,3); //Hardcoded default transform. `
colorScheme = new BasicColorScheme();
transformer = new CordTransformer(54, 0, 0, -54, -12660, 8196, 3); //Hardcoded default transform. `
}
@@ -46,10 +71,10 @@ private void paintBonds(Molecule molecule) {
canvas.setLineWidth(rendererModel.getBondWidth());
for (int i = 0; i < molecule.countBonds(); i++) {
Bond currentBond = molecule.getBond(i);
if(rendererModel.getHighlightedBonds().contains(currentBond)){
canvas.setStrokeStyle("red");
}else{
canvas.setStrokeStyle(rendererModel.getForeColor());
if (rendererModel.getHighlightedBonds().contains(currentBond)) {
canvas.setStrokeStyle("red");
} else {
canvas.setStrokeStyle(rendererModel.getForeColor());
}
Point2D pointSource = transformer.transform(new Point2D(currentBond.getSource().getX(), currentBond.getSource().getY()), null);
@@ -120,52 +145,47 @@ private void paintAtom(Atom currentAtom) {
double x = point.getX();
double y = point.getY();
boolean drawAtomLabel =false;
boolean drawAtomLabel = false;
String symbolText = currentAtom.getSymbol();
if(!symbolText.equals("C")){
drawAtomLabel=true;
}else if(currentAtom.countNeighbors()==0){
drawAtomLabel=true;
}else if(currentAtom.getCharge()!=0){
drawAtomLabel=true;
if (!symbolText.equals("C")) {
drawAtomLabel = true;
} else if (currentAtom.countNeighbors() == 0) {
drawAtomLabel = true;
} else if (currentAtom.getCharge() != 0) {
drawAtomLabel = true;
}
String fontColor = rendererModel.getForeColor();
if (symbolText.equals("N")) {
fontColor = "blue";
} else if (symbolText.equals("O")) {
fontColor = "red";
}
String fontColor = colorScheme.getColor(symbolText);
boolean highlighted = rendererModel.getHighlightedAtoms().contains(currentAtom);
if(drawAtomLabel|| highlighted){
if (drawAtomLabel || highlighted) {
canvas.save();
if (highlighted) {
canvas.setFillStyle(rendererModel.getSelectedAtomBackColor());
} else {
canvas.setFillStyle(rendererModel.getBackColor());
}
double symbolFontSize=12;
double chargeFontSize=symbolFontSize*0.6;
double symbolFontSize = 12;
double chargeFontSize = symbolFontSize * 0.6;
double radius=symbolFontSize/2*1.414;
double radius = symbolFontSize / 2 * 1.414;
// currentAtom.setCharge(1);
String chargeText="";
if(currentAtom.getCharge()!=0){
if(currentAtom.getCharge()==1)chargeText="+";
else if(currentAtom.getCharge()==-1)chargeText="-";
else if(currentAtom.getCharge()>0)chargeText=currentAtom.getCharge()+"+";
else if(currentAtom.getCharge()<0)chargeText=Math.abs(currentAtom.getCharge())+"-";
radius+=5;
String chargeText = "";
if (currentAtom.getCharge() != 0) {
if (currentAtom.getCharge() == 1) chargeText = "+";
else if (currentAtom.getCharge() == -1) chargeText = "-";
else if (currentAtom.getCharge() > 0) chargeText = currentAtom.getCharge() + "+";
else if (currentAtom.getCharge() < 0) chargeText = Math.abs(currentAtom.getCharge()) + "-";
radius += 5;
}
double textWidth = canvas.measure(symbolText, symbolFontSize);
canvas.beginPath();
canvas.rect(x - textWidth / 2,y - symbolFontSize / 2,textWidth,symbolFontSize);
canvas.rect(x - textWidth / 2, y - symbolFontSize / 2, textWidth, symbolFontSize);
// canvas.arc(x, y, radius, 0, Math.PI * 2, true);
canvas.fill();
@@ -176,12 +196,12 @@ private void paintAtom(Atom currentAtom) {
canvas.drawText(symbolText, x - textWidth / 2, y - symbolFontSize / 2, symbolFontSize);
if(!chargeText.equals("")){
if (!chargeText.equals("")) {
canvas.drawText(chargeText, x + textWidth / 2, y - symbolFontSize, chargeFontSize);
}
// canvas.drawTextCenter(currentAtom.getSymbol(), x, y, fontSize);
canvas.restore();
}
@@ -53,6 +53,8 @@ public void testItShouldAddAValidAtom() {
assertEquals(atom, molecule.getAtom(0));
}
public void testItShouldLeavAnAtomWithANullMoleculeWhenDeletingIt() {
Molecule molecule = new DefaultMolecule();
Atom atom = molecule.addAtom("C");

0 comments on commit dd58256

Please sign in to comment.