~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/jchempaint/applet/AppletCanvas.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  $RCSfile$
 
3
 *  $Author: egonw $
 
4
 *  $Date: 2007-05-01 21:15:34 +0200 (Tue, 01 May 2007) $
 
5
 *  $Revision: 8292 $
 
6
 *
 
7
 *  Copyright (C) 2002-2007  The Jmol Development Team
 
8
 *
 
9
 *  Contact: jmol-developers@lists.sf.net
 
10
 *
 
11
 *  This program is free software; you can redistribute it and/or
 
12
 *  modify it under the terms of the GNU Lesser General Public License
 
13
 *  as published by the Free Software Foundation; either version 2.1
 
14
 *  of the License, or (at your option) any later version.
 
15
 *  All we ask is that proper credit is given for our work, which includes
 
16
 *  - but is not limited to - adding the above copyright notice to the beginning
 
17
 *  of your source code files, and to any copyright notice that you may distribute
 
18
 *  with programs based on this work.
 
19
 *
 
20
 *  This program is distributed in the hope that it will be useful,
 
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 *  GNU Lesser General Public License for more details.
 
24
 *
 
25
 *  You should have received a copy of the GNU Lesser General Public License
 
26
 *  along with this program; if not, write to the Free Software
 
27
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
28
 */
 
29
package org.openscience.cdk.applications.jchempaint.applet;
 
30
 
 
31
import java.awt.Canvas;
 
32
import java.awt.Color;
 
33
import java.awt.Graphics;
 
34
import java.awt.Graphics2D;
 
35
import java.util.Iterator;
 
36
 
 
37
import javax.vecmath.Point2d;
 
38
 
 
39
import org.openscience.cdk.Atom;
 
40
import org.openscience.cdk.interfaces.IAtomContainer;
 
41
import org.openscience.cdk.applications.jchempaint.JChemPaintModel;
 
42
import org.openscience.cdk.geometry.GeometryTools;
 
43
import org.openscience.cdk.renderer.Renderer2D;
 
44
import org.openscience.cdk.tools.manipulator.ChemModelManipulator;
 
45
 
 
46
/**
 
47
 * The canvas for a JChempaint applet
 
48
 *
 
49
 * @cdk.module jchempaint.applet
 
50
 * @author     steinbeck
 
51
 */
 
52
public class AppletCanvas extends Canvas
 
53
{
 
54
 
 
55
    private static final long serialVersionUID = 5776370310193515006L;
 
56
    
 
57
    private Renderer2D renderer;
 
58
        private JChemPaintModel model;
 
59
 
 
60
 
 
61
        /**
 
62
         *  Sets the renderer attribute of the AppletCanvas object
 
63
         *
 
64
         *@param  renderer  The new renderer value
 
65
         */
 
66
        public void setRenderer(Renderer2D renderer)
 
67
        {
 
68
                this.renderer = renderer;
 
69
        }
 
70
 
 
71
        public void update(Graphics g)
 
72
        {
 
73
                paint(g);
 
74
        }
 
75
 
 
76
 
 
77
        /**
 
78
         *  Sets the jChemPaintModel attribute of the AppletCanvas object
 
79
         *
 
80
         *@param  model  The new jChemPaintModel value
 
81
         */
 
82
        public void setJChemPaintModel(JChemPaintModel model)
 
83
        {
 
84
                this.model = model;
 
85
        }
 
86
 
 
87
        public void paint(Graphics g)
 
88
        {
 
89
                renderer.getRenderer2DModel().setBackgroundDimension(getSize());
 
90
                IAtomContainer container = null;
 
91
                if (model != null) {
 
92
                        container = model.getChemModel().getBuilder().newAtomContainer();
 
93
                Iterator containers = ChemModelManipulator.getAllAtomContainers(model.getChemModel()).iterator();
 
94
                while (containers.hasNext()) {
 
95
                        container.add((IAtomContainer)containers.next());
 
96
                }
 
97
                } else
 
98
                {
 
99
                        Atom carbon = new Atom("C");
 
100
                        carbon.setHydrogenCount(4);
 
101
                        carbon.setPoint2d(new Point2d(1.0, 1.0));
 
102
                        container.addAtom(carbon);
 
103
                }
 
104
                /*
 
105
                 *  this code ensures that the molecule ends up somewhere in the model
 
106
                 *  of the view screen
 
107
                 */
 
108
                GeometryTools.translateAllPositive(container,renderer.getRenderer2DModel().getRenderingCoordinates());
 
109
                double scaleFactor = GeometryTools.getScaleFactor(container, 40.0,renderer.getRenderer2DModel().getRenderingCoordinates());
 
110
                GeometryTools.scaleMolecule(container, scaleFactor,renderer.getRenderer2DModel().getRenderingCoordinates());
 
111
                GeometryTools.center(container, getSize(),renderer.getRenderer2DModel().getRenderingCoordinates());
 
112
 
 
113
                setBackground(Color.white);
 
114
                if (model != null)
 
115
                {
 
116
                        renderer.paintChemModel(model.getChemModel(), (Graphics2D) g);
 
117
                } else
 
118
                {
 
119
                        renderer.paintMolecule(container, (Graphics2D) g,false,true);
 
120
                }
 
121
        }
 
122
 
 
123
}
 
124