Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/svg/LinearGradientFill.java
1463 views
1
package org.newdawn.slick.svg;
2
3
import org.newdawn.slick.geom.Line;
4
import org.newdawn.slick.geom.Shape;
5
import org.newdawn.slick.geom.TexCoordGenerator;
6
import org.newdawn.slick.geom.Transform;
7
import org.newdawn.slick.geom.Vector2f;
8
9
/**
10
* A filler for shapes that applys SVG linear gradients
11
*
12
* @author kevin
13
*/
14
public class LinearGradientFill implements TexCoordGenerator {
15
/** The start position of the gradient line */
16
private Vector2f start;
17
/** The ends position of the gradient line */
18
private Vector2f end;
19
/** The gradient being applied */
20
private Gradient gradient;
21
/** The line of the gradient */
22
private Line line;
23
/** The shape being filled with gradient */
24
private Shape shape;
25
26
/**
27
* Create a new fill for gradients
28
*
29
* @param shape The shape being filled
30
* @param trans The transform given for the shape
31
* @param gradient The gradient to apply
32
*/
33
public LinearGradientFill(Shape shape, Transform trans, Gradient gradient) {
34
this.gradient = gradient;
35
36
float x = gradient.getX1();
37
float y = gradient.getY1();
38
float mx = gradient.getX2();
39
float my = gradient.getY2();
40
41
float h = my - y;
42
float w = mx - x;
43
44
float[] s = new float[] {x,y+(h/2)};
45
gradient.getTransform().transform(s, 0, s, 0, 1);
46
trans.transform(s, 0, s, 0, 1);
47
float[] e = new float[] {x+w,y+(h/2)};
48
gradient.getTransform().transform(e, 0, e, 0, 1);
49
trans.transform(e, 0, e, 0, 1);
50
51
start = new Vector2f(s[0],s[1]);
52
end = new Vector2f(e[0],e[1]);
53
54
line = new Line(start, end);
55
}
56
57
/**
58
* @see org.newdawn.slick.geom.TexCoordGenerator#getCoordFor(float, float)
59
*/
60
public Vector2f getCoordFor(float x, float y) {
61
Vector2f result = new Vector2f();
62
line.getClosestPoint(new Vector2f(x,y), result);
63
float u = result.distance(start);
64
u /= line.length();
65
66
return new Vector2f(u,0);
67
}
68
69
}
70
71