关于java:Java基础-awt-Graphics2D-生成矩形图片并向内写入字符串

46次阅读

共计 2264 个字符,预计需要花费 6 分钟才能阅读完成。

  •     JDK:OpenJDK-11
  •      OS:CentOS 7.6.1810
  •      IDE:Eclipse 2019‑03
  • typesetting:Markdown

code

package per.jizuiku.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * @author 给最苦
 * @date 2019/06/30
 * @blog segmentfault.com/u/jizuiku
 */
public class Demo {

    /**
     * 
     * @param args
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {

        // 失去图片缓冲区
        int width = 100;
        int height = 50;
        int imageType = BufferedImage.TYPE_INT_BGR;
        BufferedImage myImage = new BufferedImage(width, height, imageType);

        // 失去画笔
        Graphics2D pen = (Graphics2D)myImage.getGraphics();

        // 设置笔的色彩, 即背景色
        pen.setColor(Color.WHITE);

        // 画出一个矩形
        // 坐标 x 坐标 y 宽度 100 长度 50
        pen.fillRect(0, 0, 100, 50);

        // monospace 粗体显示 大小 12
        Font font = new Font("monospace", Font.BOLD, 25);
        pen.setFont(font);
        // 字的色彩 和 背景的色彩 要不同的
        pen.setColor(Color.blue);

        // 写字
        pen.drawString("abcd", 20, 35);

        ImageIO.write(myImage, "JPEG", new FileOutputStream("abcd.jpg"));

    }
}

result

sourceCode

/**
    * Renders the text of the specified {@code String}, using the
    * current text attribute state in the {@code Graphics2D} context.
    * The baseline of the
    * first character is at position (<i>x</i>,&nbsp;<i>y</i>) in
    * the User Space.
    * The rendering attributes applied include the {@code Clip},
    * {@code Transform}, {@code Paint}, {@code Font} and
    * {@code Composite} attributes.  For characters in script
    * systems such as Hebrew and Arabic, the glyphs can be rendered from
    * right to left, in which case the coordinate supplied is the
    * location of the leftmost character on the baseline.
    * @param str the string to be rendered
    * @param x the x coordinate of the location where the
    * {@code String} should be rendered
    * @param y the y coordinate of the location where the
    * {@code String} should be rendered
    * @throws NullPointerException if {@code str} is
    *         {@code null}
    * @see         java.awt.Graphics#drawBytes
    * @see         java.awt.Graphics#drawChars
    * @since       1.0
    */
public abstract void drawString(String str, int x, int y);

resource

  • [JDK] openjdk.java.net
  • [doc – 参考] docs.oracle.com/en/java/javase/11
  • [标准 – 举荐] yq.aliyun.com/articles/69327
  • [标准 – 举荐] google.github.io/styleguide
  • [源码] hg.openjdk.java.net
  • [OS] www.centos.org
  • [IDE] www.eclipse.org/downloads/packages
  • [平台] segmentfault.com


感激帮忙过 给最苦 的人们。
Java、Groovy 和 Scala 等基于 JVM 的语言,优良,值得学习。
标准的命名和代码格局等,有助于沟通和了解。
JVM 的配置、监控与优化,比拟实用,值得学习。

欢送关注微信公众号:悟为生心

正文完
 0