- import java.awt.Color;
- import java.awt.LinearGradientPaint;
- import java.awt.MultipleGradientPaint;
- import java.awt.RadialGradientPaint;
- import java.awt.Rectangle;
- import java.awt.geom.Point2D;
- import java.util.ArrayList;
- import java.util.Collections;
-
- public class CGradient extends ArrayList<CGradientColor> {
-
- private static final long serialVersionUID = 1L;
-
- private int _angle = 0;
- private boolean _radial = false;
-
- public CGradient() {
- super();
- }
-
- public CGradient(int angle, boolean radial) {
- this();
- _angle = angle;
- _radial = radial;
- }
-
- public int getAngle() {
- return _angle;
- }
-
- public void setAngle(int angle) {
- _angle = angle;
- }
-
- public boolean isRadial() {
- return _radial;
- }
-
- public void setRadial(boolean radial) {
- _radial = radial;
- }
-
- @SuppressWarnings("unchecked")
- public MultipleGradientPaint getPaint(Rectangle position) {
- Collections.sort(this);
-
- float[] fraction = new float[size()];
- Color[] colors = new Color[size()];
- float temp = -1f;
- for (int i = 0; i < size(); ++i) {
- if (get(i).getPosition() / 100.0f > temp) {
- fraction[i] = get(i).getPosition() / 100.0f;
- } else {
- if (temp < 1) {
- fraction[i] = temp + 0.001f;
- }
- }
- temp = fraction[i];
- colors[i] = get(i).getColor();
- }
-
- MultipleGradientPaint gradient;
- if (_radial) {
- float rayon = 0;
- if (position.width > position.height) {
- rayon = position.height;
- } else {
- rayon = position.width;
- }
- gradient = new RadialGradientPaint(new Point2D.Double(position.x + position.width / 2, position.y + position.height / 2), rayon, fraction, colors);
- } else {
- float angle = (2.0f * new Float(Math.PI) / 360.0f) * _angle;
- Point2D start = new Point2D.Double((position.x + position.width / 2) + Math.cos(angle) * position.width / 2, (position.y + position.height / 2) + Math.sin(angle) * position.height / 2);
- Point2D end = new Point2D.Double((position.x + position.width / 2) + Math.cos(Math.PI + angle) * position.width / 2, (position.y + position.height / 2) + Math.sin(Math.PI + angle) * position.height / 2);
- gradient = new LinearGradientPaint(start, end, fraction, colors);
- }
- return gradient;
- }
- }
import java.awt.Color;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint;
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
public class CGradient extends ArrayList<CGradientColor> {
private static final long serialVersionUID = 1L;
private int _angle = 0;
private boolean _radial = false;
public CGradient() {
super();
}
public CGradient(int angle, boolean radial) {
this();
_angle = angle;
_radial = radial;
}
public int getAngle() {
return _angle;
}
public void setAngle(int angle) {
_angle = angle;
}
public boolean isRadial() {
return _radial;
}
public void setRadial(boolean radial) {
_radial = radial;
}
@SuppressWarnings("unchecked")
public MultipleGradientPaint getPaint(Rectangle position) {
Collections.sort(this);
float[] fraction = new float[size()];
Color[] colors = new Color[size()];
float temp = -1f;
for (int i = 0; i < size(); ++i) {
if (get(i).getPosition() / 100.0f > temp) {
fraction[i] = get(i).getPosition() / 100.0f;
} else {
if (temp < 1) {
fraction[i] = temp + 0.001f;
}
}
temp = fraction[i];
colors[i] = get(i).getColor();
}
MultipleGradientPaint gradient;
if (_radial) {
float rayon = 0;
if (position.width > position.height) {
rayon = position.height;
} else {
rayon = position.width;
}
gradient = new RadialGradientPaint(new Point2D.Double(position.x + position.width / 2, position.y + position.height / 2), rayon, fraction, colors);
} else {
float angle = (2.0f * new Float(Math.PI) / 360.0f) * _angle;
Point2D start = new Point2D.Double((position.x + position.width / 2) + Math.cos(angle) * position.width / 2, (position.y + position.height / 2) + Math.sin(angle) * position.height / 2);
Point2D end = new Point2D.Double((position.x + position.width / 2) + Math.cos(Math.PI + angle) * position.width / 2, (position.y + position.height / 2) + Math.sin(Math.PI + angle) * position.height / 2);
gradient = new LinearGradientPaint(start, end, fraction, colors);
}
return gradient;
}
}