• Java基础 awt Graphics2D 生成矩形图片并向内写入字符串


    •     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 www.cnblogs.com/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
    • [ 平台 ] www.cnblogs.com


    感谢帮助过 给最苦 的人们。
    Java、Groovy和Scala等基于JVM的语言,优秀,值得学习。
    规范的命名和代码格式等,有助于沟通和理解。
    JVM的配置、监控与优化,比较实用,值得学习。

  • 相关阅读:
    python目录操作【os和os.path】
    Zabbix4.0 zabbix 快速监控主机
    Zabbix 4.0 钉钉报警
    MySql:sql99语法的连接查询
    bat脚本中存在多条指令,但只执行到某条指令不继续向下执行的一种解决方法
    基类与接口类中的虚析构函数(virtual destructor)
    TortoiseGit使用指南;
    Rust编译问题Blocking waiting for file lock on package cache
    win10安装visual C++ 6.0,在最后显示安装程序正在更新您的系统,然后就无响应
    从实现装饰者模式中思考C++指针和引用的选择
  • 原文地址:https://www.cnblogs.com/jizuiku/p/11110235.html
Copyright © 2020-2023  润新知