Java基础 awt Font 四种字体样式
- 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
*/
public static void main(String[] args) {
try {
getImage(0);
getImage(1);
// style=2,本应该是斜体效果。可是通过生成的图片却看不出来
getImage(2);
getImage(3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param fontStyle
*/
public static void getImage(int fontStyle) 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);
// Mononspace 大小25
Font font = new Font("Mononspace", fontStyle, 25);
pen.setFont(font);
// 字的颜色 和 背景的颜色 要不同的
// 第一次没有注意到,结果 运行了三四次 都发现没有字呀
pen.setColor(Color.blue);
// 写字
pen.drawString("abcd", 20, 35);
ImageIO.write(myImage, "JPEG", new FileOutputStream(fontStyle + ".jpg"));
}
}
result
fontStyle_0

ffontStyle_1

fontStyle_2

fontStyle_3

sourceCode
/**
* The plain style constant.
*/
public static final int PLAIN = 0;
/**
* The bold style constant. This can be combined with the other style
* constants (except PLAIN) for mixed styles.
*/
public static final int BOLD = 1;
/**
* The italicized style constant. This can be combined with the other
* style constants (except PLAIN) for mixed styles.
*/
public static final int ITALIC = 2;
/**
* Creates a new {@code Font} from the specified name, style and
* point size.
* <p>
* The font name can be a font face name or a font family name.
* It is used together with the style to find an appropriate font face.
* When a font family name is specified, the style argument is used to
* select the most appropriate face from the family. When a font face
* name is specified, the face's style and the style argument are
* merged to locate the best matching font from the same family.
* For example if face name "Arial Bold" is specified with style
* {@code Font.ITALIC}, the font system looks for a face in the
* "Arial" family that is bold and italic, and may associate the font
* instance with the physical font face "Arial Bold Italic".
* The style argument is merged with the specified face's style, not
* added or subtracted.
* This means, specifying a bold face and a bold style does not
* double-embolden the font, and specifying a bold face and a plain
* style does not lighten the font.
* <p>
* If no face for the requested style can be found, the font system
* may apply algorithmic styling to achieve the desired style.
* For example, if {@code ITALIC} is requested, but no italic
* face is available, glyphs from the plain face may be algorithmically
* obliqued (slanted).
* <p>
* Font name lookup is case insensitive, using the case folding
* rules of the US locale.
* <p>
* If the {@code name} parameter represents something other than a
* logical font, i.e. is interpreted as a physical font face or family, and
* this cannot be mapped by the implementation to a physical font or a
* compatible alternative, then the font system will map the Font
* instance to "Dialog", such that for example, the family as reported
* by {@link #getFamily() getFamily} will be "Dialog".
*
* @param name the font name. This can be a font face name or a font
* family name, and may represent either a logical font or a physical
* font found in this {@code GraphicsEnvironment}.
* The family names for logical fonts are: Dialog, DialogInput,
* Monospaced, Serif, or SansSerif. Pre-defined String constants exist
* for all of these names, for example, {@code DIALOG}. If {@code name} is
* {@code null}, the <em>logical font name</em> of the new
* {@code Font} as returned by {@code getName()} is set to
* the name "Default".
* @param style the style constant for the {@code Font}
* The style argument is an integer bitmask that may
* be {@code PLAIN}, or a bitwise union of {@code BOLD} and/or
* {@code ITALIC} (for example, {@code ITALIC} or {@code BOLD|ITALIC}).
* If the style argument does not conform to one of the expected
* integer bitmasks then the style is set to {@code PLAIN}.
* @param size the point size of the {@code Font}
* @see GraphicsEnvironment#getAllFonts
* @see GraphicsEnvironment#getAvailableFontFamilyNames
* @since 1.0
*/
public Font(String name, int style, int size) {
this.name = (name != null) ? name : "Default";
this.style = (style & ~0x03) == 0 ? style : 0;
this.size = size;
this.pointSize = size;
}
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的配置、监控与优化,比较实用,值得学习。
Java基础 awt Font 四种字体样式的更多相关文章
- JAVA解析XML的四种方式
java解析xml文件四种方式 1.介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这 ...
- java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现
java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析 ...
- JAVA实现单例模式的四种方法和一些特点
JAVA实现单例模式的四种方法和一些特点,需要的朋友可以参考一下 一.饿汉式单例类 复制代码 代码如下: public class Singleton { private Sing ...
- Java通过Executors提供四种线程池
http://cuisuqiang.iteye.com/blog/2019372 Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果 ...
- 【Java】详解Java解析XML的四种方法
XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML.本文将详细介绍用Java解析XML的四种方法. AD: XML现在已经成为一种通用的数据交换格式,它的平台无关性,语 ...
- java环境变量配置四种方法
原文:java环境变量配置四种方法 Java编程首要工作就是安装JDK(Java Development Kit).一通“NEXT”点完安装后就是最重要的环境变量设置了.也许有人会问为什么要设置环境变 ...
- java 遍历Map的四种方式
java 遍历Map的四种方式 CreationTime--2018年7月16日16点15分 Author:Marydon 一.迭代key&value 第一种方式:迭代entrySet 1 ...
- Java解析XML的四种方法详解 - 转载
XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML.本文将详细介绍用Java解析XML的四种方法 在做一般的XML数据交换过程中,我更乐意传递XML字符串,而不是格式化 ...
- Java基础学习笔记(四) - 认识final关键字、权限修饰符和内部类
一.final关键字 为什么要使用 final 关键字? 通过继承我们知道,子类可以重写父类的成员变量和方法.final 关键字可以用于修饰父类,父类成员变量和方法,使其内容不可以被更改. 1.被修饰 ...
随机推荐
- SpringCloud2.0 Hystrix Feign 基于Feign实现断路器 基础教程(七)
1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集 ...
- BLE——低功耗蓝牙(Bluetooth Low Energy)
1.简介 以下蓝牙协议特指低功耗蓝牙协议. 蓝牙协议是由SIG制定并维护的通信协议,蓝牙协议栈是蓝牙协议的具体实现. 各厂商都根据蓝牙协议实现了自己的一套函数库——蓝牙协议栈,所以不同厂商的蓝牙协议栈 ...
- jmeter针对websocket协议的压测
之前一直没有接触过websocket协议,所以一直对websocket的压测存在疑惑,在网上参考文章并不断尝试之后,终于有所得:第一次用jmeter的websoket插件,用的ws非加密协议,请求都能 ...
- 实验3-1 求一元二次方程的根 (20 分) 《C语言程序设计实验与习题指导(第3版)》
本题目要求一元二次方程的根,结果保留2位小数. 输入格式: 输入在一行中给出3个浮点系数a.b.c,中间用空格分开. 输出格式: 根据系数情况,输出不同结果: 1)如果方程有两个不相等的实数根,则每行 ...
- table的各种用法
使用 colgroup 和 col 实现响应式表格(table的各种用法):http://coderlt.coding.me/2017/11/20/table-colgroup/
- 推荐系统(recommender systems):均值归一化(mean normalization)
均值归一化可以让算法运行得更好. 现在考虑这样一个情况:一个用户对所有的电影都没有评分,即上图所示 的Eve用户.现在我们要学习特征向量(假设n=2) 以及用户5的向量θ(5),因为用户Eve没有对任 ...
- spark延迟调度与动态资源管理
Spark中的延迟调度 Spark的Task的调度过程有五个本地性级别:PROCESS_NODE.NODE_LOCAL.NO_PREF.RACK_LOCAL.ANY.在理想的状态下,我们肯定是想所有的 ...
- 二叉堆的构建(Java)
package com.rao.linkList; /** * @author Srao * @className BinaryHeap * @date 2019/12/3 14:14 * @pack ...
- BZOJ 2600: [Ioi2011]ricehub 双指针+贪心
不难发现,当我们要选的区间确定后,一定会把仓库安排到中间的稻草上(如果是偶数个的话中间两个都行). 然后按照坐标从小到大枚举右指针,左指针一定不递减,双指针扫一下就行了. code: #include ...
- proxysql 学习二 admin-web 启用
proxysql 从1.4.4 开始内置了一个简单的http server,可以方便进行状态信息的查看,今天在 尝试通过配置参数以及update global_variables 没有生效,set 也 ...