use of java.awt.RadialGradientPaint in project jdk8u_jdk by JetBrains.
the class ConcurrentReadingTest method createTestFile.
private static void createTestFile() {
int w = 1280;
int h = 1024;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = { 0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p = new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
try {
System.out.println("Create test image " + file.getAbsolutePath());
boolean b = ImageIO.write(img, "JPEG", file);
if (!b) {
throw new RuntimeException("Failed to create test image.");
}
} catch (IOException e) {
throw new RuntimeException("Test failed", e);
}
}
use of java.awt.RadialGradientPaint in project litiengine by gurkenlabs.
the class LightSource method renderShadows.
/**
* Renders the shadows using simple vector math. The steps are as follows:
*
* <pre>
* for each entity
* if entity is not moving:
* ignore entity
* if entity is too far from mouse:
* ignore entity
*
* determine unit vector from mouse to entity center
* get perpendicular of unit vector
*
* Create Points A + B:
* extrude perpendicular in either direction, by the half-size of the entity
* Create Points C + D:
* extrude A + B away from mouse position
*
* construct polygon with points A, B, C, D
*
* render with RadialGradientPaint to give it a "fade-out" appearance
* </pre>
*
* @param g
* the graphics to use for rendering
* @param center
* the center
*/
private void renderShadows(final Graphics2D g) {
if (!Game.getEnvironment().getCombatEntities().stream().anyMatch(isInRange(this.getCenter(), SHADOW_GRADIENT_SIZE))) {
return;
}
// we'll use a radial gradient
final Paint gradientPaint = new RadialGradientPaint(Game.getCamera().getViewPortDimensionCenter(this), SHADOW_GRADIENT_SIZE, SHADOW_GRADIENT_FRACTIONS, SHADOW_GRADIENT_COLORS);
// old Paint object for resetting it later
final Paint oldPaint = g.getPaint();
g.setPaint(gradientPaint);
// for each entity
for (final ICombatEntity mob : Game.getEnvironment().getCombatEntities()) {
if (mob.isDead() || !isInRange(this.getCenter(), SHADOW_GRADIENT_SIZE).test(mob)) {
continue;
}
final Shape obstructedVision = this.getObstructedVisionArea(mob, Game.getCamera().getViewPortDimensionCenter(this));
// fill the polygon with the gradient paint
g.fill(obstructedVision);
}
// reset to old Paint object
g.setPaint(oldPaint);
}
use of java.awt.RadialGradientPaint in project jdk8u_jdk by JetBrains.
the class BufferedPaints method setRadialGradientPaint.
/********************** RadialGradientPaint support *************************/
/**
* This method calculates six m** values and a focusX value that
* are used by the native fragment shader. These techniques are
* based on a whitepaper by Daniel Rice on radial gradient performance
* (attached to the bug report for 6521533). One can refer to that
* document for the complete set of formulas and calculations, but
* the basic goal is to compose a transform that will convert an
* (x,y) position in device space into a "u" value that represents
* the relative distance to the gradient focus point. The resulting
* value can be used to look up the appropriate color by linearly
* interpolating between the two nearest colors in the gradient.
*/
private static void setRadialGradientPaint(RenderQueue rq, SunGraphics2D sg2d, RadialGradientPaint paint, boolean useMask) {
boolean linear = (paint.getColorSpace() == ColorSpaceType.LINEAR_RGB);
int cycleMethod = paint.getCycleMethod().ordinal();
float[] fractions = paint.getFractions();
Color[] colors = paint.getColors();
int numStops = colors.length;
int[] pixels = convertToIntArgbPrePixels(colors, linear);
Point2D center = paint.getCenterPoint();
Point2D focus = paint.getFocusPoint();
float radius = paint.getRadius();
// save original (untransformed) center and focus points
double cx = center.getX();
double cy = center.getY();
double fx = focus.getX();
double fy = focus.getY();
// transform from gradient coords to device coords
AffineTransform at = paint.getTransform();
at.preConcatenate(sg2d.transform);
focus = at.transform(focus, focus);
// transform unit circle to gradient coords; we start with the
// unit circle (center=(0,0), focus on positive x-axis, radius=1)
// and then transform into gradient space
at.translate(cx, cy);
at.rotate(fx - cx, fy - cy);
at.scale(radius, radius);
// invert to get mapping from device coords to unit circle
try {
at.invert();
} catch (Exception e) {
at.setToScale(0.0, 0.0);
}
focus = at.transform(focus, focus);
// clamp the focus point so that it does not rest on, or outside
// of, the circumference of the gradient circle
fx = Math.min(focus.getX(), 0.99);
// assert rq.lock.isHeldByCurrentThread();
rq.ensureCapacity(20 + 28 + (numStops * 4 * 2));
RenderBuffer buf = rq.getBuffer();
buf.putInt(SET_RADIAL_GRADIENT_PAINT);
buf.putInt(useMask ? 1 : 0);
buf.putInt(linear ? 1 : 0);
buf.putInt(numStops);
buf.putInt(cycleMethod);
buf.putFloat((float) at.getScaleX());
buf.putFloat((float) at.getShearX());
buf.putFloat((float) at.getTranslateX());
buf.putFloat((float) at.getShearY());
buf.putFloat((float) at.getScaleY());
buf.putFloat((float) at.getTranslateY());
buf.putFloat((float) fx);
buf.put(fractions);
buf.put(pixels);
}
use of java.awt.RadialGradientPaint in project j6dof-flight-sim by chris-ali.
the class DirectionalGyro method create_AIRPLANE_Image.
private BufferedImage create_AIRPLANE_Image(final int WIDTH) {
if (WIDTH <= 0) {
return UTIL.createImage(1, 1, Transparency.TRANSLUCENT);
}
final BufferedImage IMAGE = UTIL.createImage(WIDTH, WIDTH, Transparency.TRANSLUCENT);
final Graphics2D G2 = IMAGE.createGraphics();
G2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
G2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
final int IMAGE_WIDTH = IMAGE.getWidth();
final int IMAGE_HEIGHT = IMAGE.getHeight();
// Effect
final Ellipse2D OVERLAY_EFFECT = new Ellipse2D.Double(IMAGE_WIDTH * 0.08411215245723724, IMAGE_HEIGHT * 0.08411215245723724, IMAGE_WIDTH * 0.8317756652832031, IMAGE_HEIGHT * 0.8317756652832031);
final Point2D OVERLAY_EFFECT_CENTER = new Point2D.Double((0.5 * IMAGE_WIDTH), (0.5 * IMAGE_HEIGHT));
final float[] OVERLAY_EFFECT_FRACTIONS = { 0.0f, 0.41f, 0.705f, 1.0f };
final Color[] OVERLAY_EFFECT_COLORS = { UTIL.setAlpha(getBackgroundColor().LABEL_COLOR, 0), UTIL.setAlpha(getBackgroundColor().LABEL_COLOR, 0), UTIL.setAlpha(getBackgroundColor().LABEL_COLOR, 30), UTIL.setAlpha(getBackgroundColor().LABEL_COLOR, 0) };
final RadialGradientPaint OVERLAY_EFFECT_GRADIENT = new RadialGradientPaint(OVERLAY_EFFECT_CENTER, (float) (0.4158878326 * IMAGE_WIDTH), OVERLAY_EFFECT_FRACTIONS, OVERLAY_EFFECT_COLORS);
G2.setPaint(OVERLAY_EFFECT_GRADIENT);
G2.fill(OVERLAY_EFFECT);
// Plane holder
final Ellipse2D PLANEHOLDER_FRAME = new Ellipse2D.Double(IMAGE_WIDTH * 0.44392523169517517, IMAGE_HEIGHT * 0.44392523169517517, IMAGE_WIDTH * 0.11214950680732727, IMAGE_HEIGHT * 0.11214950680732727);
final Point2D PLANEHOLDER_FRAME_START = new Point2D.Double(0, PLANEHOLDER_FRAME.getBounds2D().getMinY());
final Point2D PLANEHOLDER_FRAME_STOP = new Point2D.Double(0, PLANEHOLDER_FRAME.getBounds2D().getMaxY());
final float[] PLANEHOLDER_FRAME_FRACTIONS = { 0.0f, 0.46f, 1.0f };
final Color[] PLANEHOLDER_FRAME_COLORS = { new Color(180, 180, 180, 255), new Color(63, 63, 63, 255), new Color(40, 40, 40, 255) };
final LinearGradientPaint PLANEHOLDER_FRAME_GRADIENT = new LinearGradientPaint(PLANEHOLDER_FRAME_START, PLANEHOLDER_FRAME_STOP, PLANEHOLDER_FRAME_FRACTIONS, PLANEHOLDER_FRAME_COLORS);
G2.setPaint(PLANEHOLDER_FRAME_GRADIENT);
G2.fill(PLANEHOLDER_FRAME);
final Ellipse2D GAUGE_BACKGROUND = new Ellipse2D.Double(IMAGE_WIDTH * 0.08411215245723724, IMAGE_HEIGHT * 0.08411215245723724, IMAGE_WIDTH * 0.8317756652832031, IMAGE_HEIGHT * 0.8317756652832031);
final Ellipse2D PLANEHOLDER_MAIN = new Ellipse2D.Double(IMAGE_WIDTH * 0.44859811663627625, IMAGE_HEIGHT * 0.44859811663627625, IMAGE_WIDTH * 0.10280373692512512, IMAGE_HEIGHT * 0.10280373692512512);
final Point2D PLANEHOLDER_MAIN_START = new Point2D.Double(0, GAUGE_BACKGROUND.getBounds2D().getMinY());
final Point2D PLANEHOLDER_MAIN_STOP = new Point2D.Double(0, GAUGE_BACKGROUND.getBounds2D().getMaxY());
final float[] PLANEHOLDER_MAIN_FRACTIONS = { 0.0f, 0.35f, 1.0f };
final Color[] PLANEHOLDER_MAIN_COLORS = { getBackgroundColor().GRADIENT_START_COLOR, getBackgroundColor().GRADIENT_FRACTION_COLOR, getBackgroundColor().GRADIENT_STOP_COLOR };
final LinearGradientPaint PLANEHOLDER_MAIN_GRADIENT = new LinearGradientPaint(PLANEHOLDER_MAIN_START, PLANEHOLDER_MAIN_STOP, PLANEHOLDER_MAIN_FRACTIONS, PLANEHOLDER_MAIN_COLORS);
G2.setPaint(PLANEHOLDER_MAIN_GRADIENT);
G2.fill(PLANEHOLDER_MAIN);
// Airplane
final GeneralPath PLANE = new GeneralPath();
PLANE.setWindingRule(Path2D.WIND_EVEN_ODD);
PLANE.moveTo(IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.2523364485981308);
PLANE.curveTo(IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.2523364485981308, IMAGE_WIDTH * 0.4766355140186916, IMAGE_HEIGHT * 0.2850467289719626, IMAGE_WIDTH * 0.4719626168224299, IMAGE_HEIGHT * 0.3130841121495327);
PLANE.curveTo(IMAGE_WIDTH * 0.4672897196261682, IMAGE_HEIGHT * 0.32710280373831774, IMAGE_WIDTH * 0.4672897196261682, IMAGE_HEIGHT * 0.38317757009345793, IMAGE_WIDTH * 0.4672897196261682, IMAGE_HEIGHT * 0.38317757009345793);
PLANE.lineTo(IMAGE_WIDTH * 0.32710280373831774, IMAGE_HEIGHT * 0.5186915887850467);
PLANE.lineTo(IMAGE_WIDTH * 0.32710280373831774, IMAGE_HEIGHT * 0.5700934579439252);
PLANE.lineTo(IMAGE_WIDTH * 0.4719626168224299, IMAGE_HEIGHT * 0.48598130841121495);
PLANE.lineTo(IMAGE_WIDTH * 0.4719626168224299, IMAGE_HEIGHT * 0.6121495327102804);
PLANE.lineTo(IMAGE_WIDTH * 0.4252336448598131, IMAGE_HEIGHT * 0.6635514018691588);
PLANE.lineTo(IMAGE_WIDTH * 0.4252336448598131, IMAGE_HEIGHT * 0.7149532710280374);
PLANE.lineTo(IMAGE_WIDTH * 0.48130841121495327, IMAGE_HEIGHT * 0.6822429906542056);
PLANE.lineTo(IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.6962616822429907);
PLANE.lineTo(IMAGE_WIDTH * 0.5, IMAGE_HEIGHT * 0.6962616822429907);
PLANE.lineTo(IMAGE_WIDTH * 0.5186915887850467, IMAGE_HEIGHT * 0.6822429906542056);
PLANE.lineTo(IMAGE_WIDTH * 0.5747663551401869, IMAGE_HEIGHT * 0.7149532710280374);
PLANE.lineTo(IMAGE_WIDTH * 0.5747663551401869, IMAGE_HEIGHT * 0.6635514018691588);
PLANE.lineTo(IMAGE_WIDTH * 0.5280373831775701, IMAGE_HEIGHT * 0.6121495327102804);
PLANE.lineTo(IMAGE_WIDTH * 0.5280373831775701, IMAGE_HEIGHT * 0.48598130841121495);
PLANE.lineTo(IMAGE_WIDTH * 0.6728971962616822, IMAGE_HEIGHT * 0.5700934579439252);
PLANE.lineTo(IMAGE_WIDTH * 0.6728971962616822, IMAGE_HEIGHT * 0.5186915887850467);
PLANE.lineTo(IMAGE_WIDTH * 0.5327102803738317, IMAGE_HEIGHT * 0.38317757009345793);
PLANE.curveTo(IMAGE_WIDTH * 0.5327102803738317, IMAGE_HEIGHT * 0.38317757009345793, IMAGE_WIDTH * 0.5327102803738317, IMAGE_HEIGHT * 0.32710280373831774, IMAGE_WIDTH * 0.5280373831775701, IMAGE_HEIGHT * 0.3130841121495327);
PLANE.curveTo(IMAGE_WIDTH * 0.5233644859813084, IMAGE_HEIGHT * 0.2897196261682243, IMAGE_WIDTH * 0.5046728971962616, IMAGE_HEIGHT * 0.2570093457943925, IMAGE_WIDTH * 0.5046728971962616, IMAGE_HEIGHT * 0.2523364485981308);
PLANE.curveTo(IMAGE_WIDTH * 0.5046728971962616, IMAGE_HEIGHT * 0.2523364485981308, IMAGE_WIDTH * 0.5046728971962616, IMAGE_HEIGHT * 0.2336448598130841, IMAGE_WIDTH * 0.5046728971962616, IMAGE_HEIGHT * 0.2336448598130841);
PLANE.lineTo(IMAGE_WIDTH * 0.5, IMAGE_HEIGHT * 0.16822429906542055);
PLANE.lineTo(IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.2336448598130841);
PLANE.curveTo(IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.2336448598130841, IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.2523364485981308, IMAGE_WIDTH * 0.4953271028037383, IMAGE_HEIGHT * 0.2523364485981308);
PLANE.closePath();
G2.setColor(getPointerColor().MEDIUM);
G2.setStroke(new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
G2.draw(PLANE);
G2.translate(1, 2);
G2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
G2.setColor(new Color(0.0f, 0.0f, 0.0f, 0.25f));
G2.draw(PLANE);
G2.dispose();
return IMAGE;
}
use of java.awt.RadialGradientPaint in project pivot by apache.
the class GraphicsUtilities method decodePaint.
/**
* Interpret a dictionary as a {@link Paint} value
*
* @param dictionary A dictionary containing a key {@value #PAINT_TYPE_KEY}
* and further elements according to its value: <ul> <li><b>solid_color</b>
* - key {@value #COLOR_KEY} with value being any of the
* {@linkplain GraphicsUtilities#decodeColor color values recognized by
* Pivot}</li> <li><b>gradient</b> - keys {@value #START_X_KEY},
* {@value #START_Y_KEY}, {@value #END_X_KEY}, {@value #END_Y_KEY} (values
* are coordinates), {@value #START_COLOR_KEY}, {@value #END_COLOR_KEY}
* (values are {@linkplain GraphicsUtilities#decodeColor colors})</li>
* <li><b>linear_gradient</b> - keys {@value #START_X_KEY},
* {@value #START_Y_KEY}, {@value #END_X_KEY}, {@value #END_Y_KEY}
* (coordinates), {@value #STOPS_KEY} (a list of dictionaries with keys
* {@value #OFFSET_KEY} (a number in [0,1]) and {@value #COLOR_KEY})</li>
* <li><b>radial_gradient</b> - keys {@value #CENTER_X_KEY},
* {@value #CENTER_Y_KEY} (coordinates), {@value #RADIUS_KEY} (a number),
* {@value #STOPS_KEY} (a list of dictionaries with keys
* {@value #OFFSET_KEY} and {@value #COLOR_KEY})</li> </ul>
* @return The fully decoded paint value.
* @throws IllegalArgumentException if there is no paint type key found.
*/
public static Paint decodePaint(Dictionary<String, ?> dictionary) {
Utils.checkNull(dictionary, "paint dictionary");
String paintType = JSON.get(dictionary, PAINT_TYPE_KEY);
if (paintType == null) {
throw new IllegalArgumentException(PAINT_TYPE_KEY + " is required.");
}
Paint paint;
switch(PaintType.valueOf(paintType.toUpperCase(Locale.ENGLISH))) {
case SOLID_COLOR:
{
String color = JSON.get(dictionary, COLOR_KEY);
paint = decodeColor(color);
break;
}
case GRADIENT:
{
float startX = JSON.getFloat(dictionary, START_X_KEY);
float startY = JSON.getFloat(dictionary, START_Y_KEY);
float endX = JSON.getFloat(dictionary, END_X_KEY);
float endY = JSON.getFloat(dictionary, END_Y_KEY);
Color startColor = decodeColor((String) JSON.get(dictionary, START_COLOR_KEY));
Color endColor = decodeColor((String) JSON.get(dictionary, END_COLOR_KEY));
paint = new GradientPaint(startX, startY, startColor, endX, endY, endColor);
break;
}
case LINEAR_GRADIENT:
{
float startX = JSON.getFloat(dictionary, START_X_KEY);
float startY = JSON.getFloat(dictionary, START_Y_KEY);
float endX = JSON.getFloat(dictionary, END_X_KEY);
float endY = JSON.getFloat(dictionary, END_Y_KEY);
@SuppressWarnings("unchecked") List<Dictionary<String, ?>> stops = (List<Dictionary<String, ?>>) JSON.get(dictionary, STOPS_KEY);
int n = stops.getLength();
float[] fractions = new float[n];
Color[] colors = new Color[n];
for (int i = 0; i < n; i++) {
Dictionary<String, ?> stop = stops.get(i);
float offset = JSON.getFloat(stop, OFFSET_KEY);
fractions[i] = offset;
Color color = decodeColor((String) JSON.get(stop, COLOR_KEY));
colors[i] = color;
}
paint = new LinearGradientPaint(startX, startY, endX, endY, fractions, colors);
break;
}
case RADIAL_GRADIENT:
{
float centerX = JSON.getFloat(dictionary, CENTER_X_KEY);
float centerY = JSON.getFloat(dictionary, CENTER_Y_KEY);
float radius = JSON.getFloat(dictionary, RADIUS_KEY);
@SuppressWarnings("unchecked") List<Dictionary<String, ?>> stops = (List<Dictionary<String, ?>>) JSON.get(dictionary, STOPS_KEY);
int n = stops.getLength();
float[] fractions = new float[n];
Color[] colors = new Color[n];
for (int i = 0; i < n; i++) {
Dictionary<String, ?> stop = stops.get(i);
float offset = JSON.getFloat(stop, OFFSET_KEY);
fractions[i] = offset;
Color color = decodeColor((String) JSON.get(stop, COLOR_KEY));
colors[i] = color;
}
paint = new RadialGradientPaint(centerX, centerY, radius, fractions, colors);
break;
}
default:
{
throw new UnsupportedOperationException();
}
}
return paint;
}
Aggregations