Source Code Cross Referenced for Grid.java in  » Science » jcm1-source » edu » hws » jcm » draw » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » jcm1 source » edu.hws.jcm.draw 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*************************************************************************
002:         *                                                                        *
003:         *   1) This source code file, in unmodified form, and compiled classes   *
004:         *      derived from it can be used and distributed without restriction,  *
005:         *      including for commercial use.  (Attribution is not required       *
006:         *      but is appreciated.)                                              *
007:         *                                                                        *
008:         *    2) Modified versions of this file can be made and distributed       *
009:         *       provided:  the modified versions are put into a Java package     *
010:         *       different from the original package, edu.hws;  modified          *
011:         *       versions are distributed under the same terms as the original;   *
012:         *       and the modifications are documented in comments.  (Modification *
013:         *       here does not include simply making subclasses that belong to    *
014:         *       a package other than edu.hws, which can be done without any      *
015:         *       restriction.)                                                    *
016:         *                                                                        *
017:         *   David J. Eck                                                         *
018:         *   Department of Mathematics and Computer Science                       *
019:         *   Hobart and William Smith Colleges                                    *
020:         *   Geneva, New York 14456,   USA                                        *
021:         *   Email: eck@hws.edu          WWW: http://math.hws.edu/eck/            *
022:         *                                                                        *
023:         *************************************************************************/package edu.hws.jcm.draw;
024:
025:        import java.awt.*;
026:
027:        /**
028:         * A Grid object draws a graph paper-like grid on a Canvas.  The pixel width
029:         * and height between adjacent grid lines is specified as a parameter to the
030:         * constructer, or through the access methods "setXSP(double)" and
031:         * "setYSP(double)".  Note that the spacing will be scaled to between PIX_MIN
032:         * and PIX_MAX (20 and 80, respectively, by default).  The color of the grid
033:         * lines can be set, and defaults to (220, 220, 220).
034:         *
035:         * <p>This class was written by Gabriel Weinstock (with some modifications by David Eck).
036:         */
037:        public class Grid extends Drawable {
038:
039:            private Color gcol = new Color(220, 220, 220);
040:            private double xsp, ysp;
041:            private final int PIX_MAX = 50, PIX_MIN = 20;
042:
043:            /**
044:             * Create a Grid object with x and y spacing 1.0.  This does not mean that
045:             * the actual spacing between grid lines will be 1.  It will be some reasonable
046:             * fraction or multiply of 1, with the value chosen to give a reasonable
047:             * spacing between the grid lines.
048:             */
049:            public Grid() {
050:                this (1.0, 1.0);
051:            }
052:
053:            /**
054:             * Create a Grid object with spacing specified.
055:             */
056:            public Grid(double xspace, double yspace) {
057:                xsp = xspace;
058:                ysp = yspace;
059:            }
060:
061:            /**
062:             * Access method which returns the Color of the grid lines.
063:             */
064:            public Color getColor() {
065:                return gcol;
066:            }
067:
068:            /**
069:             * Method to set the Color used to draw grid lines.
070:             */
071:
072:            public void setColor(Color c) {
073:                if (c != null && !c.equals(gcol)) {
074:                    gcol = c;
075:                    needsRedraw();
076:                }
077:            }
078:
079:            /**
080:             * Access method to return the x spacing used between grid lines.
081:             */
082:            public double getXSP() {
083:                return xsp;
084:            }
085:
086:            /**
087:             * Access method to return the y spacing used between grid lines
088:             */
089:            public double getYSP() {
090:                return ysp;
091:            }
092:
093:            /**
094:             * Method to set the x spacing between grid lines.  This does not mean that
095:             * the actual spacing between grid lines will be x.  It will be some reasonable
096:             * fraction or multiply of s, with the value chosen to give a reasonable
097:             * spacing between the grid lines.
098:             */
099:            public void setXSP(double x) {
100:                xsp = x;
101:                needsRedraw();
102:            }
103:
104:            /**
105:             * Method to set the y spacing between grid lines.  This does not mean that
106:             * the actual spacing between grid lines will be y.  It will be some reasonable
107:             * fraction or multiply of s, with the value chosen to give a reasonable
108:             * spacing between the grid lines.
109:             */
110:            public void setYSP(double y) {
111:                ysp = y;
112:                needsRedraw();
113:            }
114:
115:            /**
116:             * Draws the grid if an update is required.  This is not usually called directly.
117:             *
118:             * @param g the Graphics context
119:             * @param coordsch boolean describing whether coordinates have changed
120:             */
121:            public void draw(Graphics g, boolean coordsch) {
122:                if (coords == null)
123:                    return;
124:                double pixwidth = coords.getPixelWidth();
125:                double pixheight = coords.getPixelHeight();
126:                if (Double.isNaN(pixwidth) || Double.isNaN(pixheight)
127:                        || Double.isInfinite(pixheight)
128:                        || Double.isInfinite(pixwidth) || pixwidth == 0
129:                        || pixheight == 0)
130:                    return;
131:                g.setColor(gcol);
132:                // start by drawing vertical grid lines (starting with center):
133:                if (xsp > 0) {
134:                    double x = xsp;
135:                    while (x > (pixwidth * PIX_MAX))
136:                        x /= 10;
137:                    if (x < (pixwidth * PIX_MIN))
138:                        x *= 5;
139:                    if (x > (pixwidth * PIX_MAX))
140:                        x /= 2;
141:                    int j = (int) (Math.ceil(coords.getXmin() / x));
142:                    double i = j * x;
143:                    while (coords.xToPixel(i) < (coords.getWidth() + coords
144:                            .getLeft())) {
145:                        g.drawLine(coords.xToPixel(i), coords.getTop(), coords
146:                                .xToPixel(i), coords.getTop()
147:                                + coords.getHeight());
148:                        i += x;
149:                    }
150:                }
151:                // next draw horizontal grid lines
152:                if (ysp > 0) {
153:                    double y = ysp;
154:                    while (y > (pixheight * PIX_MAX))
155:                        y /= 10;
156:                    if (y < (pixheight * PIX_MIN))
157:                        y *= 5;
158:                    if (y > (pixheight * PIX_MAX))
159:                        y /= 2;
160:                    int j = (int) (Math.ceil(coords.getYmin() / y));
161:                    double i = j * y;
162:                    while (coords.yToPixel(i) > coords.getTop()) {
163:                        g.drawLine(coords.getLeft(), coords.yToPixel(i), coords
164:                                .getLeft()
165:                                + coords.getWidth(), coords.yToPixel(i));
166:                        i += y;
167:                    }
168:                }
169:            }
170:
171:        } // end class Grid
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.