List of usage examples for java.awt Graphics2D setFont
public abstract void setFont(Font font);
From source file:TexturedText.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Times New Roman", Font.PLAIN, 72); g2.setFont(font); String s = "Java Source and Support"; Dimension d = getSize();/* w w w. j a v a 2 s . c o m*/ float x = 20, y = 100; BufferedImage bi = getTextureImage(); Rectangle r = new Rectangle(0, 0, bi.getWidth(), bi.getHeight()); TexturePaint tp = new TexturePaint(bi, r); g2.setPaint(tp); g2.drawString(s, x, y); }
From source file:FontTest.java
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); // measure the size of the message FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); // set (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // add ascent to y to reach the baseline double ascent = -bounds.getY(); double baseY = y + ascent; // draw the message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // draw the baseline g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // draw the enclosing rectangle Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect);/*from ww w . j av a 2 s. c om*/ }
From source file:Main.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 72); g2.setFont(font); String s = "this is a test"; float x = 50, y = 150; // Draw the baseline. FontRenderContext frc = g2.getFontRenderContext(); float width = (float) font.getStringBounds(s, frc).getWidth(); Line2D baseline = new Line2D.Float(x, y, x + width, y); g2.setPaint(Color.red);//from ww w . jav a 2s .com g2.draw(baseline); // Draw the ascent. LineMetrics lm = font.getLineMetrics(s, frc); Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent()); g2.draw(ascent); // Draw the descent. Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent()); g2.draw(descent); // Draw the leading. Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y + lm.getDescent() + lm.getLeading()); g2.draw(leading); // Render the string. g2.setPaint(Color.black); g2.drawString(s, x, y); }
From source file:LineMetricsIllustration.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 72); g2.setFont(font); String s = "Java Source and Support"; float x = 50, y = 150; // Draw the baseline. FontRenderContext frc = g2.getFontRenderContext(); float width = (float) font.getStringBounds(s, frc).getWidth(); Line2D baseline = new Line2D.Float(x, y, x + width, y); g2.setPaint(Color.red);/* ww w.j av a 2 s. c o m*/ g2.draw(baseline); // Draw the ascent. LineMetrics lm = font.getLineMetrics(s, frc); Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent()); g2.draw(ascent); // Draw the descent. Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent()); g2.draw(descent); // Draw the leading. Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y + lm.getDescent() + lm.getLeading()); g2.draw(leading); // Render the string. g2.setPaint(Color.black); g2.drawString(s, x, y); }
From source file:MainClass.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 72); g2.setFont(font); String s = "www.java2s.com"; float x = 50, y = 150; FontRenderContext frc = g2.getFontRenderContext(); float width = (float) font.getStringBounds(s, frc).getWidth(); Line2D baseline = new Line2D.Float(x, y, x + width, y); g2.setPaint(Color.lightGray); g2.draw(baseline);//from w ww.j a va2 s . c om // Draw the ascent. LineMetrics lm = font.getLineMetrics(s, frc); Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent()); g2.draw(ascent); // Draw the descent. Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent()); g2.draw(descent); // Draw the leading. Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y + lm.getDescent() + lm.getLeading()); g2.draw(leading); // Render the string. g2.setPaint(Color.black); g2.drawString(s, x, y); }
From source file:TexturedText.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(tp);/*from w ww. j a va 2 s . c om*/ g2.setFont(myFont); g2.drawString(mesg, 20, 100); }
From source file:FontShow.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Dialog", Font.PLAIN, 96); g2.setFont(font); int width = getSize().width; int height = getSize().height; String message = "Java2s"; FontRenderContext frc = g2.getFontRenderContext(); LineMetrics metrics = font.getLineMetrics(message, frc); float messageWidth = (float) font.getStringBounds(message, frc).getWidth(); // center text float ascent = metrics.getAscent(); float descent = metrics.getDescent(); float x = (width - messageWidth) / 2; float y = (height + metrics.getHeight()) / 2 - descent; int PAD = 25; g2.setPaint(getBackground());//from w ww . j a v a 2 s. c om g2.fillRect(0, 0, width, height); g2.setPaint(getForeground()); g2.drawString(message, x, y); g2.setPaint(Color.white); // Base lines drawLine(g2, x - PAD, y, x + messageWidth + PAD, y); drawLine(g2, x, y + PAD, x, y - ascent - PAD); g2.setPaint(Color.green); // Ascent line drawLine(g2, x - PAD, y - ascent, x + messageWidth + PAD, y - ascent); g2.setPaint(Color.red); // Descent line drawLine(g2, x - PAD, y + descent, x + messageWidth + PAD, y + descent); }
From source file:org.cybercat.automation.addons.common.ScreenshotManager.java
public BufferedImage applySubs(BufferedImage image, String text) { Graphics2D g2 = image.createGraphics(); g2.setFont(font); int height = image.getHeight(); int width = image.getWidth(); g2.setColor(fontColor);/*from w w w. j av a2 s .c o m*/ String[] subs = text.split("\n"); FontMetrics fMetrics = g2.getFontMetrics(); int lineHeight = fMetrics.getHeight(); int lineWidth; for (int i = subs.length; i > 0; i--) { lineWidth = fMetrics.stringWidth(subs[i - 1]); int y = height - bottomOffset - ((subs.length - i) * (lineHeight + lineOffset)); int x = (width / 2) - (lineWidth / 2); g2.drawString(subs[i - 1], x, y); } g2.dispose(); return image; }
From source file:image.text.CreateTifAndAnnotate.java
public void createAnnotatedTif(String[] args, OutputStream out) throws Exception { byte[] byteArray = new byte[] { -1, 0 }; ColorModel colorModel = new IndexColorModel(1, 2, byteArray, byteArray, byteArray); WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700, 2200, 1, 1, null); BufferedImage bufImg = new BufferedImage(colorModel, writeableRaster, false, null); // ------------------------------------------------------------------- Graphics2D g2d = bufImg.createGraphics(); g2d.setColor(Color.black);/*from w w w. j ava2 s . c o m*/ Font font = new Font("Arial Bold", Font.PLAIN, 36); g2d.setFont(font); int vertPos = 200; for (int i = 0; i < args.length; i++) { g2d.drawString(args[i], 75, vertPos); vertPos += 48; } PlanarImage planarImage = PlanarImage.wrapRenderedImage(bufImg); TIFFEncodeParam encodeParam = new TIFFEncodeParam(); encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); encodeParam.setWriteTiled(false); // false means use strips. encodeParam.setTileSize(0, 0); // tiling will make the file size much larger. encodeParam.setLittleEndian(true); // create an Intel (II) format image String SoftwareVersion[] = new String[] { "TIFF by Joe, " + this.getClass().getName() }; String docName[] = new String[] { "JoesTifAnnotator" }; // Create a new TIFF fields including a new TIFF ASCII TIFF tag. TIFFField tiffFields[] = new TIFFField[5]; tiffFields[0] = new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName); tiffFields[1] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } }); tiffFields[2] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } }); // resolution unit tiffFields[3] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, new char[] { 2 }); tiffFields[4] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion); encodeParam.setExtraFields(tiffFields); TIFFImageEncoder encoder = new TIFFImageEncoder(out, encodeParam); encoder.encode(planarImage); }
From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java
/** * Draws a single character.//www . j a va 2 s . co m * * @param g2d * {@link Graphics2D} context * @param ch * character to draw * @param x * left x position of the character * @param boxWidth * width of the box */ private void drawCharacter(Graphics2D g2d, char ch, int x, int boxWidth) { double degree = (rnd.nextDouble() * rotationAmplitude * 2) - rotationAmplitude; double scale = 1 - (rnd.nextDouble() * scaleAmplitude / 100); Graphics2D cg2d = (Graphics2D) g2d.create(); cg2d.setFont(font.deriveFont(fontSize)); cg2d.translate(x + (boxWidth / 2), height / 2); cg2d.rotate(degree * PI / 90); cg2d.scale(scale, scale); FontMetrics fm = cg2d.getFontMetrics(); int charWidth = fm.charWidth(ch); int charHeight = fm.getAscent() + fm.getDescent(); cg2d.drawString(String.valueOf(ch), -(charWidth / 2), fm.getAscent() - (charHeight / 2)); cg2d.dispose(); }