1   package fi.jyu.mit.graphics;
2   
3   import java.awt.*;
4   
5   /**
6    * Luokka akseleille
7    * @author Markus Kivioja
8    *
9    */
10  public class Axis extends BasicShape {
11      
12      /** X koordinaattia kuvaava vakio */
13      public final static int X = 0;
14      
15      /** Y koordinaattia kuvaava vakio */
16      public final static int Y = 1;
17      
18      /** Z koordinaattia kuvaava vakio */
19      public final static int Z = 2;
20      
21      private Line axisX, axisY, axisZ;
22      private boolean showXscale, showYscale;
23      private double xLength, yLength, zLength;
24      private int xScalePoints, yScalePoints;
25      private FillPolygon xArrowHead, yArrowHead;
26      private double arrowHeadSize = 0.01;
27      
28      public Axis() {
29          super();
30          this.xArrowHead = new FillPolygon();
31          this.yArrowHead = new FillPolygon();
32          this.xScalePoints = 0;
33          this.yScalePoints = 0;
34          this.xLength = 0;
35          this.yLength = 0;
36          this.zLength = 0;
37          this.axisX = new Line();
38          this.axisY = new Line();
39          this.axisZ = new Line();
40          this.showXscale = true;
41          this.showYscale = true;
42      }
43      
44      /**
45       * Luo uudet akselit
46       * @param xLength x-akselin pituus
47       * @param yLength y-akselin pituus
48       * @param zLength z-akselin pituus
49       * @param x akselien leikkauspisteen x-koordinaatti
50       * @param y akselien leikkauspisteen y-koordinaatti
51       * @param z akselien leikkauspisteen z-koordinaatti
52       */
53      public Axis(double xLength, double yLength, double zLength, double x, double y, double z) { 
54          super();
55          double[][] points = {{xLength/2, -xLength*arrowHeadSize}, {xLength/2, xLength*arrowHeadSize}, {xLength/2+(xLength*2*arrowHeadSize), 0}};
56          this.xArrowHead = new FillPolygon(points);
57          double[][] points2 = {{-yLength*arrowHeadSize, yLength/2}, {yLength*arrowHeadSize, yLength/2}, {0, yLength/2+(yLength*2*arrowHeadSize)}};
58          this.yArrowHead = new FillPolygon(points2);
59          this.xScalePoints = 10;
60          this.yScalePoints = 10;
61          this.xLength = xLength;
62          this.yLength = yLength;
63          this.zLength = zLength;
64          this.axisX = new Line(x-xLength/2, y,z, x+xLength/2, y,z);
65          this.axisY = new Line(x, y-yLength/2,z, x, y+yLength/2,z);
66          this.axisZ = new Line(x, y, z-this.zLength/2, x, y, z+this.zLength/2);
67          this.showXscale = true;
68          this.showYscale = true;
69      }
70      
71      /**
72       * Luo uudet akselit
73       * @param xLength x-akselin pituus
74       * @param yLength y-akselin pituus
75       * @param zLength z-akselin pituus
76       */
77      public Axis(double xLength, double yLength, double zLength) {
78          this(xLength,yLength,zLength,0,0,0);
79      }
80      
81      /**
82       * Asettaa x-akselin asteikon näkyväksi
83       * @param showXscale asteikko näkyvä/piilotettu
84       */
85      public void showXscale(boolean showXscale) {
86          this.showXscale = showXscale;
87          this.redraw();
88      }
89      
90      /**
91       * Asettaa y-akselin asteikon näkyväksi
92       * @param showYscale asteikko näkyvä/piilotettu
93       */
94      public void showYscale(boolean showYscale) {
95          this.showYscale = showYscale;
96          this.redraw();
97      }
98      
99      /**
100      * Asettaa akselien pituudet
101      * @param xLength x-akselin pituus
102      * @param yLength y-akselin pituus
103      * @param zLength z-akselin pituus
104      */
105     public void setLengths(double xLength, double yLength, double zLength) {
106         this.xLength = xLength;
107         this.yLength = yLength;
108         this.zLength = zLength;
109         this.axisX.setPoints(-this.xLength/2, 0, 0, this.xLength/2, 0, 0);
110         this.axisY.setPoints(0, -this.yLength/2, 0, 0, this.yLength/2, 0);
111         this.axisZ.setPoints(0, 0, -this.zLength/2, 0, 0, this.zLength/2);
112         this.redraw();
113     }
114     
115     /**
116      * Asettaa x-akselin asteikon lukujen määrän
117      * @param xScalePoints asteikon lukujen määrä
118      */
119     public void setXscalePoints(int xScalePoints) {
120         this.xScalePoints = xScalePoints;
121         this.redraw();
122     }
123     
124     /**
125      * Asettaa y-akselin asteikon lukujen määrän
126      * @param yScalePoints asteikon lukujen määrä
127      */
128     public void setYscalePoints(int yScalePoints) {
129         this.yScalePoints = yScalePoints;
130         this.redraw();
131     }
132     
133     /**
134      * Asettaa akselien nuolenpäiden koon
135      * @param arrowHeadSize nuolenpäiden koko
136      */
137     public void setArrowHeadSize(double arrowHeadSize) {
138         this.arrowHeadSize = arrowHeadSize;
139         this.redraw();
140     }
141     
142     /**
143      * Pyoristaa luvun halutulla desimaalimaaralla.
144      * Vain sisaiseen kayttoon
145      * @param number pyoristettava luku
146      * @param digits desimaalien maara
147      * @return pyoristetty luku
148      */
149     private double round(double number, int digits) {
150         return ((int)(number*Math.pow(10, digits))/Math.pow(10, digits));
151     }
152     
153     @Override
154     protected void drawShape(Graphics g, Matrix a) {
155         Text value = null;
156         Line l = new Line(0,0,0,0);
157         double dx = xLength/xScalePoints/50; 
158         if (this.showXscale) {
159             for ( double i = -xLength/xScalePoints; i >= -xLength/2 ; i -= xLength/xScalePoints) {
160                 value = new Text(round(i,2)+"", i, 0);
161                 value.setTranslate(0, -1.2);
162                 value.draw(g, a);
163                 l.setPoints(i, -dx, 0, i, dx, 0);
164                 l.draw(g, a);
165             }
166             for ( double i = xLength/xScalePoints; i <= xLength/2; i += xLength/xScalePoints) {
167                 value = new Text(round(i,2)+"", i, 0);
168                 value.setTranslate(0, -1.2);
169                 value.draw(g, a);
170                 l.setPoints(i, -dx, 0, i, dx, 0);
171                 l.draw(g, a);
172             }
173         }
174         if (this.showYscale) {
175             for ( double i = -yLength/yScalePoints; i >= -yLength/2;  i -= yLength/yScalePoints) {
176                 value = new Text(" " + round(i,2)+"", 0, i);
177                 value.setTranslate(-1.2, 0);
178                 value.draw(g, a);
179                 l.setPoints(-dx, i, 0, dx,i, 0);
180                 l.draw(g, a);
181             }
182             for ( double i = yLength/yScalePoints; i <= yLength/2; i += yLength/yScalePoints) {
183                 value = new Text(" " + round(i,2)+"", 0, i);
184                 value.setTranslate(-1.2, 0);
185                 value.draw(g, a);
186                 l.setPoints(-dx, i, 0, dx,i, 0);
187                 l.draw(g, a);
188             }
189         }
190         this.axisX.setColor(getColor());
191         this.axisY.setColor(getColor());
192         this.axisZ.setColor(getColor());
193         this.axisX.draw(g, a);
194         this.axisY.draw(g, a);
195         this.axisZ.draw(g, a);
196         this.xArrowHead.draw(g, a);
197         this.yArrowHead.draw(g, a);
198     }
199     
200 }
201