Java Code Examples for java.awt.Graphics2D#setTransform()

The following examples show how to use java.awt.Graphics2D#setTransform() . 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: ImageUtilities.java    From hortonmachine with GNU General Public License v3.0 7 votes vote down vote up
public static BufferedImage rotateImageByDegrees( BufferedImage img, double angle ) {
    double rads = Math.toRadians(angle);
    double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads));
    int w = img.getWidth();
    int h = img.getHeight();
    int newWidth = (int) Math.floor(w * cos + h * sin);
    int newHeight = (int) Math.floor(h * cos + w * sin);

    BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotated.createGraphics();
    AffineTransform at = new AffineTransform();
    at.translate((newWidth - w) / 2, (newHeight - h) / 2);

    int x = w / 2;
    int y = h / 2;

    at.rotate(rads, x, y);
    g2d.setTransform(at);
    g2d.drawImage(img, 0, 0, null);
    g2d.setColor(Color.RED);
    g2d.drawRect(0, 0, newWidth - 1, newHeight - 1);
    g2d.dispose();

    return rotated;
}
 
Example 2
Source File: AttrStringUtils.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws the attributed string at <code>(textX, textY)</code>, rotated by 
 * the specified angle about <code>(rotateX, rotateY)</code>.
 * 
 * @param text  the attributed string (<code>null</code> not permitted).
 * @param g2  the graphics output target.
 * @param textX  the x-coordinate for the text.
 * @param textY  the y-coordinate for the text.
 * @param angle  the rotation angle (in radians).
 * @param rotateX  the x-coordinate for the rotation point.
 * @param rotateY  the y-coordinate for the rotation point.
 * 
 * @since 1.0.16
 */
public static void drawRotatedString(AttributedString text, Graphics2D g2, 
        float textX, float textY, double angle, float rotateX, 
        float rotateY) {
    ParamChecks.nullNotPermitted(text, "text");

    AffineTransform saved = g2.getTransform();
    AffineTransform rotate = AffineTransform.getRotateInstance(angle, 
            rotateX, rotateY);
    g2.transform(rotate);
    TextLayout tl = new TextLayout(text.getIterator(),
                g2.getFontRenderContext());
    tl.draw(g2, textX, textY);
    
    g2.setTransform(saved);        
}
 
Example 3
Source File: BitmapExporter.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
private void exportTo(SerializableImage image, Matrix transformation, Matrix strokeTransformation) {
    this.image = image;
    this.strokeTransformation = strokeTransformation.clone();
    this.strokeTransformation.scaleX /= SWF.unitDivisor;
    this.strokeTransformation.scaleY /= SWF.unitDivisor;

    graphics = (Graphics2D) image.getGraphics();
    AffineTransform at = transformation.toTransform();
    at.preConcatenate(AffineTransform.getScaleInstance(1 / SWF.unitDivisor, 1 / SWF.unitDivisor));
    graphics.setTransform(at);
    defaultStroke = graphics.getStroke();
    super.export();
}
 
Example 4
Source File: FontPanel.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void tlDrawLine( Graphics2D g2, TextLayout tl,
                                   float baseX, float baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    tl.draw( g2, (float) 0, (float) 0 );

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 5
Source File: Draw.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void drawPoint_Character(PointF aP, PointBreak aPB, Graphics2D g) {
    AffineTransform tempTrans = g.getTransform();
    if (aPB.getAngle() != 0) {
        //AffineTransform myTrans = new AffineTransform();
        AffineTransform myTrans = (AffineTransform) tempTrans.clone();
        myTrans.translate(aP.X, aP.Y);
        myTrans.rotate(aPB.getAngle() * Math.PI / 180);
        g.setTransform(myTrans);
        aP.X = 0;
        aP.Y = 0;
    }

    String text = String.valueOf((char) aPB.getCharIndex());
    Font wFont = new Font(aPB.getFontName(), Font.PLAIN, (int) aPB.getSize());
    g.setFont(wFont);
    FontMetrics metrics = g.getFontMetrics();
    PointF sPoint = (PointF) aP.clone();
    sPoint.X = sPoint.X - metrics.stringWidth(text) / 2;
    sPoint.Y = sPoint.Y + metrics.getHeight() / 4;
    //sPoint.X = sPoint.X - aPB.getSize() / 2;
    //sPoint.Y = sPoint.Y + aPB.getSize() / 2;        

    g.setColor(aPB.getColor());
    g.drawString(text, sPoint.X, sPoint.Y);

    if (aPB.getAngle() != 0) {
        g.setTransform(tempTrans);
    }
}
 
Example 6
Source File: ByteImage.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the image.
 * 
 * @param panel
 * @param g
 */

public void draw(DrawingPanel panel, Graphics g) {
	if (!visible) {
		return;
	}
	if(dirtyImage){
	  image = Toolkit.getDefaultToolkit().createImage(imageSource);
	}
	if (image == null) {
		panel.setMessage(DisplayRes.getString("Null Image")); //$NON-NLS-1$
		return;
	}
	Graphics2D g2 = (Graphics2D) g;
	AffineTransform gat = g2.getTransform(); // save graphics transform
	RenderingHints hints = g2.getRenderingHints();
	if (!OSPRuntime.isMac()) { // Rendering hint bug in Mac Snow Leopard
		g2.setRenderingHint(RenderingHints.KEY_DITHERING,
				RenderingHints.VALUE_DITHER_DISABLE);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_OFF);
	}
	double sx = (xmax - xmin) * panel.xPixPerUnit / ncol;
	double sy = (ymax - ymin) * panel.yPixPerUnit / nrow;
	// translate origin to pixel location of (xmin,ymax)
	g2.transform(AffineTransform.getTranslateInstance(panel.leftGutter
			+ panel.xPixPerUnit * (xmin - panel.xmin), panel.topGutter
			+ panel.yPixPerUnit * (panel.ymax - ymax)));
	g2.transform(AffineTransform.getScaleInstance(sx, sy));  // scales image to world units
	g2.drawImage(image, 0, 0, panel);
	g2.setTransform(gat); // restore graphics conext
	g2.setRenderingHints(hints); // restore the hints
}
 
Example 7
Source File: CEImage.java    From open-ig with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D)g;
	g2.setColor(Color.RED);
	g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
	if (icon != null) {
		int iw = icon.getWidth();
		int ih = icon.getHeight();
		
		double scalex = 1d * getWidth() / iw;
		double scaley = 1d * getHeight() / ih;
		
		double scale = Math.min(scalex, scaley);
		
		AffineTransform at = g2.getTransform();
		
		
		int dx = (int)((getWidth() - iw * scale) / 2);
		int dy = (int)((getHeight() - ih * scale) / 2);
		g2.translate(dx, dy);
		g2.scale(scale, scale);
		
		g2.drawImage(icon, 0, 0, null);
		
		g2.setTransform(at);
	}
}
 
Example 8
Source File: Draw.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Draw out string
 *
 * @param g Graphics2D
 * @param x X location
 * @param y Y location
 * @param s String
 * @param x_align X align
 * @param y_align Y align
 * @param angle Angle
 * @param useExternalFont Use external font or not
 */
public static void drawString(Graphics2D g, float x, float y, String s, XAlign x_align, YAlign y_align, float angle, boolean useExternalFont) {
    if (angle == 0) {
        drawString(g, x, y, s, x_align, y_align, useExternalFont);
    } else {
        /*AffineTransform tempTrans = g.getTransform();
        AffineTransform myTrans = transform(g, x, y, s, x_align, y_align, angle);
        g.setTransform(myTrans);
        Draw.drawString(g, s, 0, 0, useExternalFont);
        g.setTransform(tempTrans);*/

        AffineTransform tempTrans = g.getTransform();
        AffineTransform myTrans = (AffineTransform)tempTrans.clone();
        myTrans.translate(x, y);
        myTrans.rotate(-angle * Math.PI / 180);
        g.setTransform(myTrans);
        x = 0; y = 0;
        Dimension labSize = Draw.getStringDimension(s, g);
        switch (y_align) {
            case TOP:
                y += labSize.height;
                break;
            case CENTER:
                y += labSize.height / 2.0f;
                break;
        }
        switch (x_align) {
            case RIGHT:
                x = x - labSize.width;
                break;
            case CENTER:
                x = x - labSize.width / 2.0f;
                break;
        }
        Draw.drawString(g, s, x, y, useExternalFont);
        g.setTransform(tempTrans);
    }
}
 
Example 9
Source File: ChartNorthArrow.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Paint graphics
 *
 * @param g Graphics
 * @param pageLocation Page location
 * @param zoom Zoom
 */
public void paintGraphics(Graphics2D g, PointF pageLocation, float zoom) {
    AffineTransform oldMatrix = g.getTransform();
    PointF aP = pageToScreen(this.getX(), this.getY(), pageLocation, zoom);
    g.translate(aP.X, aP.Y);
    g.scale(zoom, zoom);
    if (_angle != 0) {
        g.rotate(_angle);
    }
    if (_antiAlias) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } else {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }

    //Draw background color
    if (this.isDrawBackColor()){
        g.setColor(this.getBackground());
        g.fill(new Rectangle.Float(0, 0, this.getWidth() * zoom, this.getHeight() * zoom));
    }

    drawNorthArrow(g);

    //Draw neatline
    if (_drawNeatLine) {
        Rectangle.Float mapRect = new Rectangle.Float(_neatLineSize - 1, _neatLineSize - 1,
                (this.getWidth() - _neatLineSize) * zoom, (this.getHeight() - _neatLineSize) * zoom);
        g.setColor(_neatLineColor);
        g.setStroke(new BasicStroke(_neatLineSize));
        g.draw(mapRect);
    }

    g.setTransform(oldMatrix);
}
 
Example 10
Source File: ImageServices.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
@Documentation(
    value = "Rotates the Image by the given angle in degres.",
    params = {
        @Param(name = "image", value = "The Image"),
        @Param(name = "angle", value = "The angle in degres"),
    },
    result = "rotate the image",
    examples = {
        @Example(expression = "myImage.rotate(90)", result = "will rotate the image by an angle of 90 degres"),
    }
)
// @formatter:on
public MImage rotate(MImage image, Integer angle) throws IOException {
    final BufferedImage bufferedImage = MImageAWTImpl.getBufferedImage(image);

    final double rads = Math.toRadians(angle);
    final double sin = Math.abs(Math.sin(rads));
    final double cos = Math.abs(Math.cos(rads));
    final int width = bufferedImage.getWidth();
    final int height = bufferedImage.getHeight();
    final int x = width / 2;
    final int y = height / 2;
    final int newWidth = (int) Math.floor(width * cos + height * sin);
    final int newHeight = (int) Math.floor(height * cos + width * sin);

    final BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    final AffineTransform translateTransform = new AffineTransform();
    translateTransform.translate((newWidth - width) / 2, (newHeight - height) / 2);
    translateTransform.rotate(rads, x, y);

    final Graphics2D g2d = rotated.createGraphics();
    g2d.setTransform(translateTransform);
    g2d.drawImage(bufferedImage, 0, 0, null);
    g2d.dispose();

    return new MImageAWTImpl(rotated, image.getURI());
}
 
Example 11
Source File: PersonMapLayer.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
@Override
// 2014-11-04 Added building parameter
public void displayLayer(
	Graphics2D g2d, Settlement settlement, Building building,
	double xPos, double yPos, int mapWidth, int mapHeight,
	double rotation, double scale
) {

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

	// Get the map center point.
	double mapCenterX = mapWidth / 2D;
	double mapCenterY = mapHeight / 2D;

	// Translate map from settlement center point.
	g2d.translate(mapCenterX + (xPos * scale), mapCenterY + (yPos * scale));

	// Rotate map from North.
	g2d.rotate(rotation, 0D - (xPos * scale), 0D - (yPos * scale));

	// Draw all people.
	drawPeople(g2d, settlement, scale);

	// Restore original graphic transforms.
	g2d.setTransform(saveTransform);
}
 
Example 12
Source File: ChartColorBar.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Draw legend
 *
 * @param g Graphics2D
 * @param point Start point
 */
@Override
public void draw(Graphics2D g, PointF point) {

    AffineTransform oldMatrix = g.getTransform();
    g.translate(point.X + this.xshift, point.Y + this.yshift);

    //Draw background color
    if (this.drawBackground) {
        g.setColor(this.background);
        g.fill(new Rectangle.Float(0, 0, this.width, this.height));
    }

    //Draw legend
    g.setStroke(new BasicStroke(1));
    switch (this.orientation) {
        case HORIZONTAL:
            this.drawHorizontalBarLegend(g, legendScheme);
            break;
        case VERTICAL:
            this.drawVerticalBarLegend(g, legendScheme);
            break;
    }

    //Draw neatline
    if (drawNeatLine) {
        Rectangle.Float mapRect = new Rectangle.Float(0, 0, this.width, this.height);
        g.setColor(neatLineColor);
        g.setStroke(new BasicStroke(neatLineSize));
        g.draw(mapRect);
    }

    g.setTransform(oldMatrix);
}
 
Example 13
Source File: Arrow.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
public void renderPreview(Graphics2D g2d) {
    Point2D.Double s = new Point2D.Double(getXPos(), getYPos());
    //store properties
    Stroke before = g2d.getStroke();
    Composite coBefore = g2d.getComposite();
    Font fBefore = g2d.getFont();
    AffineTransform tb = g2d.getTransform();
    //start draw
    g2d.setFont(fBefore.deriveFont((float) getTextSize()));
    g2d.setStroke(getStroke());
    g2d.setColor(drawColor);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, drawAlpha));
    path = new GeneralPath();
    path.moveTo(getXPos(), getYPos() - 10);
    path.lineTo(getXPos() + 80, getYPos() - 10);
    path.lineTo(getXPos() + 80, getYPos() - 20);
    path.lineTo(getXPos() + 100, getYPos());
    path.lineTo(getXPos() + 80, getYPos() + 20);
    path.lineTo(getXPos() + 80, getYPos() + 10);
    path.lineTo(getXPos() + 0, getYPos() + 10);
    path.closePath();

    if (filled) {
        g2d.fill(path);
    } else {
        g2d.draw(path);
    }

    if (drawName) {
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTextAlpha()));
        g2d.setColor(getTextColor());
        g2d.drawString(getFormName(), (int) s.x, (int) s.y);
    }
    //reset properties
    g2d.setStroke(before);
    g2d.setComposite(coBefore);
    g2d.setFont(fBefore);
    g2d.setTransform(tb);
}
 
Example 14
Source File: ChartExportUtil.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Paints a chart with scaling options
 *
 * @param chart
 * @param info
 * @param out
 * @param width
 * @param height
 * @param resolution
 * @return BufferedImage of a given chart with scaling to resolution
 * @throws IOException
 */
private static BufferedImage paintScaledChartToBufferedImage(JFreeChart chart,
    ChartRenderingInfo info, OutputStream out, int width, int height, int resolution,
    int bufferedIType) throws IOException {
  Args.nullNotPermitted(out, "out");
  Args.nullNotPermitted(chart, "chart");

  double scaleX = resolution / 72.0;
  double scaleY = resolution / 72.0;

  double desiredWidth = width * scaleX;
  double desiredHeight = height * scaleY;
  double defaultWidth = width;
  double defaultHeight = height;
  boolean scale = false;

  // get desired width and height from somewhere then...
  if ((scaleX != 1) || (scaleY != 1)) {
    scale = true;
  }

  BufferedImage image = new BufferedImage((int) desiredWidth, (int) desiredHeight, bufferedIType);
  Graphics2D g2 = image.createGraphics();

  if (scale) {
    AffineTransform saved = g2.getTransform();
    g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
    chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info);
    g2.setTransform(saved);
    g2.dispose();
  } else {
    chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info);
  }
  return image;
}
 
Example 15
Source File: ElementDrawer.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
protected void drawRightPen(
	Graphics2D grx, 
	JRPen topPen, 
	JRPen bottomPen, 
	JRPen rightPen, 
	JRPrintElement element, 
	int offsetX, 
	int offsetY
	)
{
	Stroke rightStroke = JRPenUtil.getStroke(rightPen, BasicStroke.CAP_BUTT);
	int height = element.getHeight();
	int width = element.getWidth();
	float topOffset = topPen.getLineWidth() / 2;
	float bottomOffset = bottomPen.getLineWidth() / 2;
	
	if (rightStroke != null && height > 0)
	{
		grx.setStroke(rightStroke);
		grx.setColor(rightPen.getLineColor());

		AffineTransform oldTx = grx.getTransform();

		if (rightPen.getLineStyleValue() == LineStyleEnum.DOUBLE)
		{
			float rightPenWidth = rightPen.getLineWidth();

			grx.translate(
				element.getX() + offsetX + width + rightPenWidth / 3, 
				element.getY() + offsetY - topOffset
				);
			grx.scale(
				1,
				(height + topOffset + bottomOffset) 
					/ height 
				);
			grx.drawLine(
				0,
				0,
				0,
				height
				);

			grx.setTransform(oldTx);

			grx.translate(
				element.getX() + offsetX + width - rightPenWidth / 3, 
				element.getY() + offsetY + topOffset / 3
				);
			if(height > (topOffset + bottomOffset) / 3)
			{
				grx.scale(
					1,
					(height - (topOffset + bottomOffset) / 3) 
						/ height 
					);
			}
			grx.drawLine(
				0,
				0,
				0,
				height
				);
		}
		else
		{
			grx.translate(
				element.getX() + offsetX + width, 
				element.getY() + offsetY - topOffset
				);
			grx.scale(
				1,
				(height + topOffset + bottomOffset) 
					/ height 
				);
			grx.drawLine(
				0,
				0,
				0,
				height
				);
		}

		grx.setTransform(oldTx);
	}
}
 
Example 16
Source File: FontPanel.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 17
Source File: FontPanel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 18
Source File: FontPanel.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 19
Source File: FontPanel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 20
Source File: SetTransformEvent.java    From whyline with MIT License 2 votes vote down vote up
public void paint(Graphics2D g) {

	if(transform != null)
		g.setTransform(transform);		

}