forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXGrid.java
More file actions
255 lines (212 loc) · 7.16 KB
/
XGrid.java
File metadata and controls
255 lines (212 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/****************************************************************************************
* Copyright (c) 2014 Michael Goldbach <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
package com.wildplot.android.rendering;
import com.wildplot.android.rendering.graphics.wrapper.ColorWrap;
import com.wildplot.android.rendering.graphics.wrapper.GraphicsWrap;
import com.wildplot.android.rendering.graphics.wrapper.RectangleWrap;
import com.wildplot.android.rendering.interfaces.Drawable;
/**
* This class represents grid lines parallel to the x-axis
*
*/
public class XGrid implements Drawable {
public boolean hasVariableLimits = true;
private boolean isAutoTic = false;
private int pixelDistance = 25;
/**
* the color of the grid lines
*/
private ColorWrap color = ColorWrap.LIGHT_GRAY;
/**
* the Sheet the grid lines will be drawn onto
*/
private PlotSheet plotSheet;
/**
* start point for relative positioning of grid
*/
private double ticStart = 0;
/**
* the space between two grid lines
*/
private double tic = 1;
/**
* maximal distance from x axis the grid will be drawn
*/
private double xLength = 10;
/**
* maximal distance from y axis the grid will be drawn
*/
private double yLength = 2;
/**
* true if the grid should be drawn into the direction left to the axis
*/
private boolean gridkOnLeft = true;
/**
* true if the grid should be drawn into the direction right to the axis
*/
private boolean gridOnRight = true;
/**
* true if the grid should be drawn under the x-axis
*/
private boolean gridOnDownside = true;
/**
* true if the grid should be drawn above the x-axis
*/
private boolean gridOnUpside = true;
/**
* Constructor for an X-Grid object
* @param plotSheet the sheet the grid will be drawn onto
* @param ticStart start point for relative positioning of grid
* @param tic the space between two grid lines
*/
public XGrid(PlotSheet plotSheet, double ticStart, double tic) {
super();
this.plotSheet = plotSheet;
this.ticStart = ticStart;
this.tic = tic;
}
/**
* Constructor for an X-Grid object
* @param plotSheet the sheet the grid will be drawn onto
* @param ticStart start point for relative positioning of grid
* @param tic the space between two grid lines
*/
public XGrid(ColorWrap color, PlotSheet plotSheet, double ticStart, double tic) {
super();
this.color = color;
this.plotSheet = plotSheet;
this.ticStart = ticStart;
this.tic = tic;
}
/**
* Constructor for an X-Grid object
* @param plotSheet the sheet the grid will be drawn onto
* @param ticStart start point for relative positioning of grid
*/
public XGrid(PlotSheet plotSheet, double ticStart, int pixelDistance) {
super();
this.plotSheet = plotSheet;
this.ticStart = ticStart;
this.pixelDistance = pixelDistance;
isAutoTic = true;
}
/**
* Constructor for an X-Grid object
* @param plotSheet the sheet the grid will be drawn onto
* @param ticStart start point for relative positioning of grid
*/
public XGrid(ColorWrap color, PlotSheet plotSheet, double ticStart, int pixelDistance) {
super();
this.color = color;
this.plotSheet = plotSheet;
this.ticStart = ticStart;
this.pixelDistance = pixelDistance;
isAutoTic = true;
}
/* (non-Javadoc)
* @see rendering.Drawable#paint(java.awt.Graphics)
*/
@Override
public void paint(GraphicsWrap g) {
if(this.hasVariableLimits) {
this.xLength = Math.max(Math.abs(plotSheet.getxRange()[0]), Math.abs(plotSheet.getxRange()[1]));
this.yLength = Math.max(Math.abs(plotSheet.getyRange()[0]), Math.abs(plotSheet.getyRange()[1]));
}
ColorWrap oldColor = g.getColor();
RectangleWrap field = g.getClipBounds();
g.setColor(color);
if(this.isAutoTic)
this.tic = plotSheet.ticsCalcY(pixelDistance, field);
int tics = (int)((this.ticStart - (0-this.yLength))/tic);
double downStart = this.ticStart - this.tic*tics;
if(downStart < 0 ) {
if(!this.gridOnDownside) {
while(downStart<0) {
downStart+=this.tic;
}
}
}
double currentY = downStart;
while(currentY <= this.yLength && !(currentY > 0 && !this.gridOnUpside)) {
drawGridLine(currentY, g, field);
currentY+=this.tic;
//System.err.println("another loop");
}
//System.err.println("out of loop");
g.setColor(oldColor);
}
/**
* Draw a grid line in specified graphics object
* @param y x-position the vertical line shall be drawn
* @param g graphic the line shall be drawn onto
* @param field definition of the graphic boundaries
*/
private void drawGridLine(double y, GraphicsWrap g, RectangleWrap field) {
if(this.gridkOnLeft) {
g.drawLine(plotSheet.xToGraphic(0, field), plotSheet.yToGraphic(y, field), plotSheet.xToGraphic(-this.xLength, field), plotSheet.yToGraphic(y, field));
}
if(this.gridOnRight) {
g.drawLine(plotSheet.xToGraphic(0, field), plotSheet.yToGraphic(y, field), plotSheet.xToGraphic(this.xLength, field), plotSheet.yToGraphic(y, field));
}
}
/**
* set if the grid should be drawn onto the left side
* @param gridkOnLeft
*/
public void setGridkOnLeft(boolean gridkOnLeft) {
this.gridkOnLeft = gridkOnLeft;
}
/**
* set if the grid should be drawn onto the right side
*/
public void setGridOnRight(boolean gridOnRight) {
this.gridOnRight = gridOnRight;
}
/**
* set if the grid should be drawn onto the downside side
*/
public void setGridOnDownside(boolean gridOnDownside) {
this.gridOnDownside = gridOnDownside;
}
/**
* set if the grid should be drawn onto the upside
*/
public void setGridOnUpside(boolean gridOnUpside) {
this.gridOnUpside = gridOnUpside;
}
/*
* (non-Javadoc)
* @see rendering.Drawable#isOnFrame()
*/
public boolean isOnFrame() {
return false;
}
@Override
public void abortAndReset() {
// TODO Auto-generated method stub
}
@Override
public boolean isClusterable() {
return true;
}
@Override
public boolean isCritical() {
return true;
}
public void setColor(ColorWrap color) {
this.color = color;
}
}