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

The following examples show how to use java.awt.geom.AffineTransform#rotate() . 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: GraphicsTests.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void init(Graphics2D g2d, Context ctx, Dimension dim) {
    int w = dim.width;
    int h = dim.height;
    double theta = Math.toRadians(15);
    double cos = Math.cos(theta);
    double sin = Math.sin(theta);
    double xsize = sin * h + cos * w;
    double ysize = sin * w + cos * h;
    double scale = Math.min(w / xsize, h / ysize);
    xsize *= scale;
    ysize *= scale;
    AffineTransform at = new AffineTransform();
    at.translate((w - xsize) / 2.0, (h - ysize) / 2.0);
    at.translate(sin * h * scale, 0.0);
    at.rotate(theta);
    g2d.transform(at);
    dim.setSize(scaleForTransform(at, dim));
}
 
Example 2
Source File: PanelCanvas.java    From nanoleaf-desktop with MIT License 6 votes vote down vote up
private void drawTransformedPanel(Polygon panel, int deviceIndex, Graphics2D g2d)
{
	AffineTransform original = g2d.getTransform();
	try
	{
		AffineTransform scaled = new AffineTransform();
		Point centroid = getCentroid(deviceIndex);
		scaled.translate(centroid.getX(), centroid.getY());
		scaled.scale(scaleFactor, scaleFactor);
		scaled.translate(-centroid.getX(), -centroid.getY());
		Point tempCentroid = getCentroidFromPanels(deviceIndex,
				pdl.getTempPanels()[deviceIndex]);
		scaled.rotate(Math.toRadians(tempRotation[deviceIndex]),
				tempCentroid.x, tempCentroid.y);
		g2d.setTransform(scaled);
		g2d.drawPolygon(panel);
	}
	finally
	{
		g2d.setTransform(original);
	}
}
 
Example 3
Source File: CardBuilder.java    From magarena with GNU General Public License v3.0 6 votes vote down vote up
private static BufferedImage makeSplitCard(IRenderableCard cardDef) {
    BufferedImage firstHalf;
    BufferedImage secondHalf;
    if (cardDef.isSecondHalf()) {
        firstHalf = makeBasicCard(cardDef.getSplitDefinition());
        secondHalf = makeBasicCard(cardDef);
    } else {
        firstHalf = makeBasicCard(cardDef);
        secondHalf = makeBasicCard(cardDef.getSplitDefinition());
    }
    BufferedImage base = new BufferedImage(firstHalf.getHeight(), firstHalf.getWidth() << 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = base.createGraphics();
    //Transforms occur in reverse order
    AffineTransform affineTransform1 = new AffineTransform();
    affineTransform1.translate(0, firstHalf.getWidth() << 1);
    affineTransform1.rotate(-Math.PI / 2, 0, 0);
    graphics2D.drawImage(firstHalf, affineTransform1, null);

    AffineTransform affineTransform2 = new AffineTransform();
    affineTransform2.translate(0, secondHalf.getWidth());
    affineTransform2.rotate(-Math.PI / 2, 0, 0);
    graphics2D.drawImage(secondHalf, affineTransform2, null);

    graphics2D.dispose();
    return base;
}
 
Example 4
Source File: PageImageWriter.java    From sejda with GNU Affero General Public License v3.0 6 votes vote down vote up
private void write(PDPage page, PDXObject image, Point2D position, float width, float height,
        PDPageContentStream.AppendMode mode, PDExtendedGraphicsState gs, boolean resetContext, int rotation)
        throws TaskIOException {
    try (PDPageContentStream contentStream = new PDPageContentStream(document, page, mode, true, resetContext)) {
        AffineTransform at = new AffineTransform(width, 0, 0, height, (float) position.getX(), (float) position.getY());
        if(rotation != 0) {
            at.rotate(Math.toRadians(rotation));
        }

        if (image instanceof PDFormXObject) {
            contentStream.drawImage((PDFormXObject) image, new Matrix(at), gs);
        } else {
            contentStream.drawImage((PDImageXObject) image, new Matrix(at), gs);
        }
    } catch (IOException e) {
        throw new TaskIOException("An error occurred writing image to the page.", e);
    }
}
 
Example 5
Source File: SxBracket.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public void paintComponent(Graphics g){
   Graphics2D g2d = (Graphics2D) g;
   Stroke pen = new BasicStroke(stroke);
   g2d.setStroke(pen);
   g2d.setColor(colorFront);
   GeneralPath polyline = new GeneralPath();
   polyline.moveTo(0,0);
   polyline.lineTo(5,5);
   polyline.lineTo(5,length/2-6);
   polyline.lineTo(8,length/2);
   polyline.lineTo(5,length/2+6);
   polyline.lineTo(5,length-5);
   polyline.lineTo(0,length);
   AffineTransform rat = new AffineTransform();
   if (direction == DIRECTION_EAST){
      rat.translate(thickness,length);
      rat.rotate(Math.PI);
   } else if (direction == DIRECTION_SOUTH){
      rat.translate(0,thickness);
      rat.rotate(-Math.PI/2);
   } else if (direction == DIRECTION_NORTH){
      rat.translate(length,0);
      rat.rotate(Math.PI/2);
   }
   g2d.transform(rat);
   g2d.draw(polyline);
   super.paintComponent(g);
}
 
Example 6
Source File: RotatedBrush.java    From WorldPainter with GNU General Public License v3.0 5 votes vote down vote up
private void cacheAllStrengths() {
    final int d = radius * 2 + 1;
    
    // Create image from original brush
    final byte[] rasterData = new byte[d * d];
    for (int dx = -radius; dx <= radius; dx++) {
        for (int dy = -radius; dy <= radius; dy++) {
            rasterData[dx + radius + (dy + radius) * d] = (byte) (brush.getFullStrength(dx, dy) * 255);
        }
    }
    final DataBuffer imageDataBuffer = new DataBufferByte(rasterData, d * d);
    final Raster raster = Raster.createRaster(new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, d, d, new int[] {255}), imageDataBuffer, new Point(0, 0));
    
    // Rotate image
    final double a = degrees / 180d * Math.PI;
    AffineTransformOp transformOp = new AffineTransformOp(AffineTransform.getRotateInstance(a), AffineTransformOp.TYPE_BICUBIC);
    Rectangle2D transformedBounds = transformOp.getBounds2D(raster);
    AffineTransform transform = AffineTransform.getTranslateInstance(-transformedBounds.getX(), -transformedBounds.getY());
    transform.rotate(a);
    transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    fullStrengthCache = transformOp.filter(raster, null);

    // Calculate effective dimensions
    effectiveWidth = fullStrengthCache.getWidth();
    effectiveHeight = fullStrengthCache.getHeight();
    effectiveRadius = effectiveWidth / 2;
}
 
Example 7
Source File: GraphicsContext.java    From freerouting with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the interior of an array  of ellipses.
 * Ellipses contained in an other ellipse are treated as holes.
 */
public void fill_ellipse_arr(Ellipse []  p_ellipse_arr, Graphics p_g, Color p_color, double p_translucency_factor)
{
    if (p_color == null || p_ellipse_arr.length <= 0)
    {
        return;
    }
    GeneralPath draw_path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    for (Ellipse curr_ellipse : p_ellipse_arr)
    {
        Point2D center = coordinate_transform.board_to_screen(curr_ellipse.center);
        double bigger_radius = coordinate_transform.board_to_screen(curr_ellipse.bigger_radius);
        if (!point_near_rectangle(center.getX(), center.getY(), p_g.getClip().getBounds(), bigger_radius))
        {
            continue;
        }
        double smaller_radius =  coordinate_transform.board_to_screen(curr_ellipse.smaller_radius);
        Ellipse2D draw_ellipse =
                new Ellipse2D.Double(center.getX() - bigger_radius,center.getY() - smaller_radius,
                2 * bigger_radius, 2 * smaller_radius);
        double rotation = coordinate_transform.board_to_screen_angle(curr_ellipse.rotation);
        AffineTransform affine_transform = new  AffineTransform();
        affine_transform.rotate(rotation, center.getX(), center.getY());
        java.awt.Shape rotated_ellipse = affine_transform.createTransformedShape(draw_ellipse);
        draw_path.append(rotated_ellipse, false);
    }
    Graphics2D g2 = (Graphics2D)p_g;
    g2.setColor(p_color);
    set_translucency(g2, p_translucency_factor);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.fill(draw_path);
}
 
Example 8
Source File: PointStrUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
public static String affineTransPoints(String ptsStr, double tx, double ty, double sx, double sy, double rot) throws PointParseException {
	AffineTransform at = new AffineTransform();
	at.scale(sx, sy);
	at.rotate(rot);
	at.translate(tx, ty);

	return affineTransPoints(ptsStr, at);
}
 
Example 9
Source File: Plot3D.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void outString_bak(Graphics2D g, int x, int y, String s, XAlign x_align, YAlign y_align, float angle) {
        switch (y_align) {
            case TOP:
                y += g.getFontMetrics(g.getFont()).getAscent();
                break;
            case CENTER:
                y += g.getFontMetrics(g.getFont()).getAscent() / 2;
                break;
        }
        Dimension labSize = Draw.getStringDimension(s, g);
        switch (x_align) {
            case RIGHT:
                x = x - labSize.width;
                break;
            case CENTER:
                x = x - labSize.width / 2;
                break;
        }

        AffineTransform tempTrans = g.getTransform();
        //AffineTransform myTrans = new AffineTransform();
        AffineTransform myTrans = (AffineTransform)tempTrans.clone();
        myTrans.translate(x, y);
        myTrans.rotate(-angle * Math.PI / 180);
        g.setTransform(myTrans);
        x = 0; y = 0;
//        if (angle == 90) {
//            x = -(int) (labSize.getWidth() - 10);
//            y = (int) (labSize.getHeight() / 3);
//        } else {
//            x = -(int) (labSize.getWidth() - 5);
//            y = 0;
//        }
        Draw.drawString(g, s, x, y);
        g.setTransform(tempTrans);
    }
 
Example 10
Source File: VGDLSprite.java    From GVGAI_GYM with Apache License 2.0 5 votes vote down vote up
/**
 * In case this sprite is oriented and has an arrow to draw, it draws it.
 * @param g graphics device to draw in.
 */
public void _drawOriented(Graphics2D g, Rectangle r)
{
    Color arrowColor = new Color(color.getRed(), 255-color.getGreen(), color.getBlue());
    Polygon p = Utils.triPoints(r, orientation);

    // Rotation information

    if(shrinkfactor != 1)
    {
        r.width *= shrinkfactor;
        r.height *= shrinkfactor;
        r.x += (rect.width-r.width)/2;
        r.y += (rect.height-r.height)/2;
    }

    int w = image.getWidth(null);
    int h = image.getHeight(null);
    float scale = (float)r.width/w; //assume all sprites are quadratic.

    AffineTransform trans = new AffineTransform();
    trans.translate(r.x, r.y);
    trans.scale(scale,scale);
    trans.rotate(rotation,w/2.0,h/2.0);
    // Uncomment this line to have only one sprite
    //g.drawImage(image, trans, null);

    /* Code added by Carlos*/
    g.drawImage(image, trans, null);
    /* End of code added by carlos*/

    // We only draw the arrow if the directional sprites are null
    if (draw_arrow) {
        g.setColor(arrowColor);
        g.drawPolygon(p);
        g.fillPolygon(p);
    }

}
 
Example 11
Source File: MeshShuffleTransition2D.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
		Dimension size) {
	progress = (float) Math.pow(progress, .45);
	float stripHeight = size.height * 10 / 200;

	List<Rectangle2D> v = new ArrayList<Rectangle2D>();
	for (int y = size.height; y > -stripHeight; y -= stripHeight) {
		v.add(new Rectangle2D.Float(0, y, size.width, stripHeight));
	}
	Transition2DInstruction[] instr = new Transition2DInstruction[v.size()];
	instr[0] = new ImageInstruction(true);
	for (int a = 1; a < v.size(); a++) {
		Rectangle2D r = v.get(a);
		AffineTransform transform = new AffineTransform();
		float k = (1 - progress) * (a) / (v.size());
		float theta = (float) (Math.PI * k / 2 + (1 - progress) * Math.PI
				/ 2);
		if (theta > Math.PI / 2)
			theta = (float) (Math.PI / 2);
		if (a % 2 == 0) {
			transform.rotate(-theta, -size.width * (1 - progress) / 2,
					size.height * progress);
		} else {
			transform.rotate(theta, size.width + (1 - progress)
					* size.width / 2, size.height * progress);
		}
		instr[a] = new ImageInstruction(false, transform,
				transform.createTransformedShape(r));
	}
	return instr;
}
 
Example 12
Source File: BoundedImage.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the image.
 *
 * @param panel  the world in which the arrow is viewed
 * @param g  the graphics context upon which to draw
 */
public void draw(DrawingPanel panel, Graphics g) {
  toPixels = panel.getPixelTransform();
  Point2D pt = new Point2D.Double(x, y);
  pt = toPixels.transform(pt, pt);
  Graphics2D g2 = (Graphics2D) g;
  g2.translate(pt.getX(), pt.getY());
  AffineTransform trans = new AffineTransform();
  trans.translate(-width/2, -height/2);
  trans.rotate(-theta, width/2, height/2);
  trans.scale(width/image.getWidth(null), height/image.getHeight(null));
  g2.drawImage(image, trans, null);
  g2.translate(-pt.getX(), -pt.getY());
  drawFixedBounds(panel, g);
}
 
Example 13
Source File: SubstanceTextUtilities.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Paints the specified vertical text.
 * 
 * @param g
 *            Graphics context.
 * @param comp
 *            Component.
 * @param textRect
 *            Text rectangle.
 * @param text
 *            Text to paint.
 * @param mnemonicIndex
 *            Mnemonic index.
 * @param font
 *            Font to use.
 * @param color
 *            Color to use.
 * @param clip
 *            Optional clip. Can be <code>null</code>.
 * @param isFromBottomToTop
 *            If <code>true</code>, the text will be painted from bottom to top, otherwise the
 *            text will be painted from top to bottom.
 */
public static void paintVerticalText(Graphics g, JComponent comp, Rectangle textRect,
        String text, int mnemonicIndex, java.awt.Font font, java.awt.Color color,
        java.awt.Rectangle clip, boolean isFromBottomToTop) {
    if ((text == null) || (text.length() == 0))
        return;

    AffineTransform at = null;

    if (!isFromBottomToTop) {
        at = AffineTransform.getTranslateInstance(textRect.x + textRect.width, textRect.y);
        at.rotate(Math.PI / 2);
    } else {
        at = AffineTransform.getTranslateInstance(textRect.x, textRect.y + textRect.height);
        at.rotate(-Math.PI / 2);
    }
    Rectangle newRect = new Rectangle(0, 0, textRect.width, textRect.height);

    SubstanceTextUtilities.paintText(g, comp, newRect, text, mnemonicIndex, font, color, clip,
            at);
}
 
Example 14
Source File: BufferedPaints.java    From openjdk-8 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 15
Source File: BufferedPaints.java    From TencentKona-8 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 16
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);
}
 
Example 17
Source File: VehicleMapLayer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws an overlay for a vehicle on the map.
 * @param g2d the graphics2D context.
 * @param xLoc the X location from center of settlement (meters).
 * @param yLoc the y Location from center of settlement (meters).
 * @param width the vehicle width (meters).
 * @param length the vehicle length (meters).
 * @param facing the vehicle facing (degrees from North clockwise).
 * @param vehicleSvg the vehicle SVG graphics node.
 * @param overlaySvg the overlay SVG graphics node.
 */
private void drawVehicleOverlay(Graphics2D g2d, double xLoc, double yLoc,
		double vehicleWidth, double vehicleLength, double facing, GraphicsNode vehicleSvg, 
		GraphicsNode overlaySvg) {

	// Save original graphics transforms.
	AffineTransform saveTransform = g2d.getTransform();

	// Determine bounds.
	Rectangle2D partBounds = overlaySvg.getBounds();
	Rectangle2D vehicleBounds = vehicleSvg.getBounds();

	// Determine part width and length.
	double partWidth = (partBounds.getWidth() / vehicleBounds.getWidth()) * vehicleWidth;
	double partLength = (partBounds.getHeight() / vehicleBounds.getHeight()) * vehicleLength;

	// Determine transform information.
	double scalingWidth = partWidth / partBounds.getWidth() * scale;
	double scalingLength = partLength / partBounds.getHeight() * scale;
	double boundsPosX = partBounds.getX() * scalingWidth;
	double boundsPosY = partBounds.getY() * scalingLength;
	double centerX = partWidth * scale / 2D;
	double centerY = partLength * scale / 2D;
	double translationX = (-1D * xLoc * scale) - centerX - boundsPosX;
	double translationY = (-1D * yLoc * scale) - centerY - boundsPosY;
	double facingRadian = facing / 180D * Math.PI;

	// Apply graphic transforms for vehicle part.
	AffineTransform newTransform = new AffineTransform();
	newTransform.translate(translationX, translationY);
	newTransform.rotate(facingRadian, centerX + boundsPosX, centerY + boundsPosY);        

	// Draw buffered image of vehicle.
	BufferedImage image = getBufferedImage(overlaySvg, partWidth, partLength);
	if (image != null) {
		g2d.transform(newTransform);
		g2d.drawImage(image, 0, 0, mapPanel);
	}

	// Restore original graphic transforms.
	g2d.setTransform(saveTransform);
}
 
Example 18
Source File: ImageUtils.java    From JobX with Apache License 2.0 4 votes vote down vote up
public ImageUtils rotate(int angle) {
    if (angle == 0) {
        return this;
    }

    if (angle > 360) {
        angle = Math.abs(angle) % 360;
    }

    if (angle < 0) {
        if (Math.abs(angle) < 360) {
            angle = 360 + angle;
        } else {
            angle = (Math.abs(angle) / 360 + 1) * 360 + angle;
        }
    }

    int width = this.dealedImage.getWidth();
    int height = this.dealedImage.getHeight();
    int new_w, new_h;
    int new_radian = angle;
    if (angle <= 90) {
        new_w = (int) (width * Math.cos(Math.toRadians(new_radian)) + height * Math.sin(Math.toRadians(new_radian)));
        new_h = (int) (height * Math.cos(Math.toRadians(new_radian)) + width * Math.sin(Math.toRadians(new_radian)));
    } else if (angle <= 180) {
        new_radian = angle - 90;
        new_w = (int) (height * Math.cos(Math.toRadians(new_radian)) + width * Math.sin(Math.toRadians(new_radian)));
        new_h = (int) (width * Math.cos(Math.toRadians(new_radian)) + height * Math.sin(Math.toRadians(new_radian)));
    } else if (angle <= 270) {
        new_radian = angle - 180;
        new_w = (int) (width * Math.cos(Math.toRadians(new_radian)) + height * Math.sin(Math.toRadians(new_radian)));
        new_h = (int) (height * Math.cos(Math.toRadians(new_radian)) + width * Math.sin(Math.toRadians(new_radian)));
    } else {
        new_radian = angle - 270;
        new_w = (int) (height * Math.cos(Math.toRadians(new_radian)) + width * Math.sin(Math.toRadians(new_radian)));
        new_h = (int) (width * Math.cos(Math.toRadians(new_radian)) + height * Math.sin(Math.toRadians(new_radian)));
    }
    BufferedImage toStore = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = toStore.createGraphics();
    AffineTransform affineTransform = new AffineTransform();
    affineTransform.rotate(Math.toRadians(angle), width / 2, height / 2);
    if (angle != 180) {
        AffineTransform translationTransform = this.findTranslation(affineTransform, this.dealedImage, angle);
        affineTransform.preConcatenate(translationTransform);
    }
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, new_w, new_h);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawRenderedImage(this.dealedImage, affineTransform);
    g.dispose();
    this.dealedImage = toStore;

    return this;
}
 
Example 19
Source File: SynthPainterImpl.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Paints the background of the divider of a split pane. This implementation
 * invokes the method of the same name without the orientation.
 *
 * @param context SynthContext identifying the <code>JComponent</code> and
 *        <code>Region</code> to paint to
 * @param g <code>Graphics</code> to paint to
 * @param x X coordinate of the area to paint to
 * @param y Y coordinate of the area to paint to
 * @param w Width of the area to paint to
 * @param h Height of the area to paint to
 * @param orientation One of <code>JSplitPane.HORIZONTAL_SPLIT</code> or
 *                           <code>JSplitPane.VERTICAL_SPLIT</code>
 * @since 1.6
 */
public void paintSplitPaneDividerBackground(SynthContext context,
                                 Graphics g, int x, int y,
                                 int w, int h, int orientation) {
   if (orientation == JSplitPane.HORIZONTAL_SPLIT) {
        AffineTransform transform = new AffineTransform();
        transform.scale(-1, 1);
        transform.rotate(Math.toRadians(90));
        paintBackground(context, g, y, x, h, w, transform);
   } else {
        paintBackground(context, g, x, y, w, h, null);
    }
}
 
Example 20
Source File: BufferedPaints.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Note: This code is factored out into a separate static method
 * so that it can be shared by both the Gradient and LinearGradient
 * implementations.  LinearGradient uses this code (for the
 * two-color sRGB case only) because it can be much faster than the
 * equivalent implementation that uses fragment shaders.
 *
 * We use OpenGL's texture coordinate generator to automatically
 * apply a smooth gradient (either cyclic or acyclic) to the geometry
 * being rendered.  This technique is almost identical to the one
 * described in the comments for BufferedPaints.setTexturePaint(),
 * except the calculations take place in one dimension instead of two.
 * Instead of an anchor rectangle in the TexturePaint case, we use
 * the vector between the two GradientPaint end points in our
 * calculations.  The generator uses a single plane equation that
 * takes the (x,y) location (in device space) of the fragment being
 * rendered to calculate a (u) texture coordinate for that fragment:
 *     u = Ax + By + Cz + Dw
 *
 * The gradient renderer uses a two-pixel 1D texture where the first
 * pixel contains the first GradientPaint color, and the second pixel
 * contains the second GradientPaint color.  (Note that we use the
 * GL_CLAMP_TO_EDGE wrapping mode for acyclic gradients so that we
 * clamp the colors properly at the extremes.)  The following diagram
 * attempts to show the layout of the texture containing the two
 * GradientPaint colors (C1 and C2):
 *
 *                        +-----------------+
 *                        |   C1   |   C2   |
 *                        |        |        |
 *                        +-----------------+
 *                      u=0  .25  .5   .75  1
 *
 * We calculate our plane equation constants (A,B,D) such that u=0.25
 * corresponds to the first GradientPaint end point in user space and
 * u=0.75 corresponds to the second end point.  This is somewhat
 * non-obvious, but since the gradient colors are generated by
 * interpolating between C1 and C2, we want the pure color at the
 * end points, and we will get the pure color only when u correlates
 * to the center of a texel.  The following chart shows the expected
 * color for some sample values of u (where C' is the color halfway
 * between C1 and C2):
 *
 *       u value      acyclic (GL_CLAMP)      cyclic (GL_REPEAT)
 *       -------      ------------------      ------------------
 *        -0.25              C1                       C2
 *         0.0               C1                       C'
 *         0.25              C1                       C1
 *         0.5               C'                       C'
 *         0.75              C2                       C2
 *         1.0               C2                       C'
 *         1.25              C2                       C1
 *
 * Original inspiration for this technique came from UMD's Agile2D
 * project (GradientManager.java).
 */
private static void setGradientPaint(RenderQueue rq, AffineTransform at,
                                     Color c1, Color c2,
                                     Point2D pt1, Point2D pt2,
                                     boolean isCyclic, boolean useMask)
{
    // convert gradient colors to IntArgbPre format
    PixelConverter pc = PixelConverter.ArgbPre.instance;
    int pixel1 = pc.rgbToPixel(c1.getRGB(), null);
    int pixel2 = pc.rgbToPixel(c2.getRGB(), null);

    // calculate plane equation constants
    double x = pt1.getX();
    double y = pt1.getY();
    at.translate(x, y);
    // now gradient point 1 is at the origin
    x = pt2.getX() - x;
    y = pt2.getY() - y;
    double len = Math.sqrt(x * x + y * y);
    at.rotate(x, y);
    // now gradient point 2 is on the positive x-axis
    at.scale(2*len, 1);
    // now gradient point 2 is at (0.5, 0)
    at.translate(-0.25, 0);
    // now gradient point 1 is at (0.25, 0), point 2 is at (0.75, 0)

    double p0, p1, p3;
    try {
        at.invert();
        p0 = at.getScaleX();
        p1 = at.getShearX();
        p3 = at.getTranslateX();
    } catch (java.awt.geom.NoninvertibleTransformException e) {
        p0 = p1 = p3 = 0.0;
    }

    // assert rq.lock.isHeldByCurrentThread();
    rq.ensureCapacityAndAlignment(44, 12);
    RenderBuffer buf = rq.getBuffer();
    buf.putInt(SET_GRADIENT_PAINT);
    buf.putInt(useMask ? 1 : 0);
    buf.putInt(isCyclic ? 1 : 0);
    buf.putDouble(p0).putDouble(p1).putDouble(p3);
    buf.putInt(pixel1).putInt(pixel2);
}