Java Code Examples for java.awt.geom.AffineTransform#setToScale()

The following examples show how to use java.awt.geom.AffineTransform#setToScale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FontPanel.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private AffineTransform getAffineTransform( int transform ) {
        /// ABP
        AffineTransform at = new AffineTransform();
        switch ( transform )
        {
        case SCALE:
          at.setToScale( 1.5f, 1.5f ); break;
        case ROTATE:
          at.setToRotation( Math.PI / 6 ); break;
        case SHEAR:
          at.setToShear( 0.4f, 0 ); break;
        case NONE:
          break;
        default:
          //System.err.println( "Illegal G2 Transform Arg: " + transform);
          break;
        }

        return at;
}
 
Example 2
Source File: FontPanel.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private AffineTransform getAffineTransform( int transform ) {
        /// ABP
        AffineTransform at = new AffineTransform();
        switch ( transform )
        {
        case SCALE:
          at.setToScale( 1.5f, 1.5f ); break;
        case ROTATE:
          at.setToRotation( Math.PI / 6 ); break;
        case SHEAR:
          at.setToShear( 0.4f, 0 ); break;
        case NONE:
          break;
        default:
          //System.err.println( "Illegal G2 Transform Arg: " + transform);
          break;
        }

        return at;
}
 
Example 3
Source File: FontPanel.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private AffineTransform getAffineTransform( int transform ) {
        /// ABP
        AffineTransform at = new AffineTransform();
        switch ( transform )
        {
        case SCALE:
          at.setToScale( 1.5f, 1.5f ); break;
        case ROTATE:
          at.setToRotation( Math.PI / 6 ); break;
        case SHEAR:
          at.setToShear( 0.4f, 0 ); break;
        case NONE:
          break;
        default:
          //System.err.println( "Illegal G2 Transform Arg: " + transform);
          break;
        }

        return at;
}
 
Example 4
Source File: FontPanel.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private AffineTransform getAffineTransform( int transform ) {
        /// ABP
        AffineTransform at = new AffineTransform();
        switch ( transform )
        {
        case SCALE:
          at.setToScale( 1.5f, 1.5f ); break;
        case ROTATE:
          at.setToRotation( Math.PI / 6 ); break;
        case SHEAR:
          at.setToShear( 0.4f, 0 ); break;
        case NONE:
          break;
        default:
          //System.err.println( "Illegal G2 Transform Arg: " + transform);
          break;
        }

        return at;
}
 
Example 5
Source File: ButtonShape.java    From pumpernickel with MIT License 6 votes vote down vote up
private static GeneralPath findShapeToFitRectangle(Shape originalShape,
		int w, int h) {
	GeneralPath newShape = new GeneralPath();
	Rectangle2D rect = new Rectangle2D.Float();
	ShapeBounds.getBounds(originalShape, rect);
	if (originalShape.contains(rect.getX() + rect.getWidth() / 2,
			rect.getY() + rect.getHeight() / 2) == false)
		throw new IllegalArgumentException(
				"This custom shape is not allowed.  The center of this shape must be inside the shape.");
	double scale = Math.min((w) / rect.getWidth(), (h) / rect.getHeight());
	AffineTransform transform = new AffineTransform();
	while (true) {
		newShape.reset();
		newShape.append(originalShape, true);
		transform.setToScale(scale, scale);
		newShape.transform(transform);
		ShapeBounds.getBounds(newShape, rect);

		if (newShape.contains(rect.getX() + rect.getWidth() / 2 - w / 2,
				rect.getY() + rect.getHeight() / 2 - h / 2, w, h)) {
			return newShape;
		}

		scale += .01;
	}
}
 
Example 6
Source File: MMDPrint.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Nonnull
private Image makeScaledInstance(@Nonnull final Image image, final float scale) {
  if (Float.compare(scale, 1.0f) == 0) {
    return image;
  }

  final int scaledWidth = Math.round(image.getWidth(null) * scale);
  final int scaledHeight = Math.round(image.getHeight(null) * scale);

  final BufferedImage result = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
  final Graphics2D graphics = (Graphics2D) result.getGraphics();
  try {
    final Map rhints = new HashMap();
    rhints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rhints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    rhints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    rhints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    rhints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    rhints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    final AffineTransform transform = new AffineTransform();
    transform.setToScale(scale, scale);
    graphics.setRenderingHints(rhints);
    graphics.drawImage(image, transform, null);
  } catch (Exception ex) {
    LOGGER.error("Can't scale image", ex);
    return image;
  } finally {
    graphics.dispose();
  }
  return result;
}
 
Example 7
Source File: TexturePaintContext.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 8
Source File: SunGraphics2D.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public SunGraphics2D(SurfaceData sd, Color fg, Color bg, Font f) {
    surfaceData = sd;
    foregroundColor = fg;
    backgroundColor = bg;

    transform = new AffineTransform();
    stroke = defaultStroke;
    composite = defaultComposite;
    paint = foregroundColor;

    imageComp = CompositeType.SrcOverNoEa;

    renderHint = SunHints.INTVAL_RENDER_DEFAULT;
    antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
    textAntialiasHint = SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT;
    fractionalMetricsHint = SunHints.INTVAL_FRACTIONALMETRICS_OFF;
    lcdTextContrast = lcdTextContrastDefaultValue;
    interpolationHint = -1;
    strokeHint = SunHints.INTVAL_STROKE_DEFAULT;
    resolutionVariantHint = SunHints.INTVAL_RESOLUTION_VARIANT_DEFAULT;

    interpolationType = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;

    validateColor();

    devScale = sd.getDefaultScale();
    if (devScale != 1) {
        transform.setToScale(devScale, devScale);
        invalidateTransform();
    }

    font = f;
    if (font == null) {
        font = defaultFont;
    }

    setDevClip(sd.getBounds());
    invalidatePipe();
}
 
Example 9
Source File: TexturePaintContext.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 10
Source File: TexturePaintContext.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 11
Source File: PhotonEditPanel.java    From PhotonFileValidator with MIT License 5 votes vote down vote up
private Graphics2D createGraphics() {
    Graphics2D g = image.createGraphics();
    if (mirrored) {
        AffineTransform transform = new AffineTransform();
        transform.setToScale(1, -1);
        transform.translate(0, -image.getHeight());
        g.setTransform(transform);
    }
    return g;
}
 
Example 12
Source File: TexturePaintContext.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 13
Source File: TexturePaintContext.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 14
Source File: TexturePaintContext.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 15
Source File: SunGraphics2D.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public SunGraphics2D(SurfaceData sd, Color fg, Color bg, Font f) {
    surfaceData = sd;
    foregroundColor = fg;
    backgroundColor = bg;

    transform = new AffineTransform();
    stroke = defaultStroke;
    composite = defaultComposite;
    paint = foregroundColor;

    imageComp = CompositeType.SrcOverNoEa;

    renderHint = SunHints.INTVAL_RENDER_DEFAULT;
    antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
    textAntialiasHint = SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT;
    fractionalMetricsHint = SunHints.INTVAL_FRACTIONALMETRICS_OFF;
    lcdTextContrast = lcdTextContrastDefaultValue;
    interpolationHint = -1;
    strokeHint = SunHints.INTVAL_STROKE_DEFAULT;
    resolutionVariantHint = SunHints.INTVAL_RESOLUTION_VARIANT_DEFAULT;

    interpolationType = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;

    validateColor();

    devScale = sd.getDefaultScale();
    if (devScale != 1) {
        transform.setToScale(devScale, devScale);
        invalidateTransform();
    }

    font = f;
    if (font == null) {
        font = defaultFont;
    }

    setDevClip(sd.getBounds());
    invalidatePipe();
}
 
Example 16
Source File: PlaceView.java    From PIPE with MIT License 4 votes vote down vote up
/**
 * Paints the Place component taking into account the n q12[umber of tokens from
 * the storeCurrentMarking
 *
 * @param g The PositionGraphics object onto which the Place is drawn.
 */
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();

    Rectangle bounds = shape.getBounds();
    g2.translate(bounds.getWidth()/2, bounds.getHeight()/2);

    if (hasCapacity()) {
        g2.setStroke(new BasicStroke(2.0f));
        setToolTipText("k = " + model.getCapacity());
    } else {
        g2.setStroke(new BasicStroke(1.0f));
        setToolTipText("k = \u221E");
    }
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    if (isSelected() && !ignoreSelection) {
        g2.setColor(GUIConstants.SELECTION_FILL_COLOUR);
    } else {
        g2.setColor(GUIConstants.ELEMENT_FILL_COLOUR);
    }
    g2.fill(shape);

    if (isSelected() && !ignoreSelection) {
        g2.setPaint(GUIConstants.SELECTION_LINE_COLOUR);
    } else {
        g2.setPaint(GUIConstants.ELEMENT_LINE_COLOUR);
    }
    g2.draw(shape);

    g2.setStroke(new BasicStroke(1.0f));

    // Paints border round a tagged place - paint component is called after any action on the place, so this bit
    // of code doesn't have to be called specially

    if (this.isTagged()) {
        AffineTransform oldTransform = g2.getTransform();

        AffineTransform scaleTransform = new AffineTransform();
        scaleTransform.setToScale(1.2, 1.2);

        g2.transform(scaleTransform);

        g2.translate(-2, -2);

        g2.fill(shape);

        g2.translate(2, 2);

        g2.setTransform(oldTransform);
    }

    g2 = (Graphics2D) g.create();
    paintTokens(g2);

    g2.dispose();
}
 
Example 17
Source File: BufferedPaints.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: SVGElement.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
static public AffineTransform parseSingleTransform(final String val) throws SVGException {
	final Matcher matchWord = Pattern.compile("[-.\\w]+").matcher("");

	final AffineTransform retXform = new AffineTransform();

	matchWord.reset(val);
	if (!matchWord.find()) {
		// Return identity transformation if no data present (eg, empty string)
		return retXform;
	}

	final String function = matchWord.group().toLowerCase();

	final LinkedList<String> termList = new LinkedList<>();
	while (matchWord.find()) {
		termList.add(matchWord.group());
	}

	final double[] terms = new double[termList.size()];
	final Iterator<String> it = termList.iterator();
	int count = 0;
	while (it.hasNext()) {
		terms[count++] = XMLParseUtil.parseDouble(it.next());
	}

	// Calculate transformation
	if (function.equals("matrix")) {
		retXform.setTransform(terms[0], terms[1], terms[2], terms[3], terms[4], terms[5]);
	} else if (function.equals("translate")) {
		retXform.setToTranslation(terms[0], terms[1]);
	} else if (function.equals("scale")) {
		if (terms.length > 1) {
			retXform.setToScale(terms[0], terms[1]);
		} else {
			retXform.setToScale(terms[0], terms[0]);
		}
	} else if (function.equals("rotate")) {
		if (terms.length > 2) {
			retXform.setToRotation(Math.toRadians(terms[0]), terms[1], terms[2]);
		} else {
			retXform.setToRotation(Math.toRadians(terms[0]));
		}
	} else if (function.equals("skewx")) {
		retXform.setToShear(Math.toRadians(terms[0]), 0.0);
	} else if (function.equals("skewy")) {
		retXform.setToShear(0.0, Math.toRadians(terms[0]));
	} else {
		throw new SVGException("Unknown transform type");
	}

	return retXform;
}
 
Example 19
Source File: BufferedPaints.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: BufferedPaints.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}