mirror of
https://github.com/jie65535/note.git
synced 2025-06-01 17:29:10 +08:00
增加 java.awt 笔记
This commit is contained in:
parent
dd3284ab99
commit
5968189cd3
91
java/AWT-TextRender.md
Normal file
91
java/AWT-TextRender.md
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
---
|
||||||
|
tags: #java.awt #Graphics2D #TextRender
|
||||||
|
---
|
||||||
|
|
||||||
|
# Graphics2D 绘制文字
|
||||||
|
|
||||||
|
本文主要记录使用 `Graphics2D` 进行文本渲染相关内容。
|
||||||
|
|
||||||
|
## 参考代码
|
||||||
|
|
||||||
|
```java
|
||||||
|
package com.test.testImage;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.FontMetrics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.RenderingHints;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
public class Graphics2DTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
String text = "文字居中";
|
||||||
|
int width = 500;
|
||||||
|
int height = 400;
|
||||||
|
// 创建BufferedImage对象
|
||||||
|
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
|
||||||
|
// 获取Graphics2D
|
||||||
|
Graphics2D g2d = image.createGraphics();
|
||||||
|
// 设置背景色
|
||||||
|
g2d.setBackground(new Color(255,255,255));
|
||||||
|
//g2d.setPaint(new Color(0,0,0));
|
||||||
|
// 设置前景色
|
||||||
|
g2d.setColor(Color.red);
|
||||||
|
// 绘制填充矩形 填充背景
|
||||||
|
g2d.clearRect(0, 0, width, height);
|
||||||
|
// 载入字体
|
||||||
|
Font font=new Font("宋体",Font.PLAIN,64);
|
||||||
|
// 设置字体
|
||||||
|
g2d.setFont(font);
|
||||||
|
// 设置抗锯齿
|
||||||
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
// 计算文字长度,计算居中的x点坐标
|
||||||
|
// 获取字体度量参数
|
||||||
|
FontMetrics fm = g2d.getFontMetrics(font);
|
||||||
|
// 测量文本长度
|
||||||
|
int textWidth = fm.stringWidth(text);
|
||||||
|
// 获得文本居中的起点x位置
|
||||||
|
int widthX = (width - textWidth) / 2;
|
||||||
|
// 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
|
||||||
|
g2d.drawString(text,widthX,100);
|
||||||
|
// 释放对象
|
||||||
|
g2d.dispose();
|
||||||
|
// 保存文件
|
||||||
|
ImageIO.write(image, "jpg", new File("D:/test.jpg"));
|
||||||
|
}
|
||||||
|
catch(Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 图像缓冲区
|
||||||
|
```java
|
||||||
|
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics2D g2d = image.createGraphics();
|
||||||
|
```
|
||||||
|
|
||||||
|
## 抗锯齿
|
||||||
|
```java
|
||||||
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测量文本
|
||||||
|
```java
|
||||||
|
// 获取字体度量参数
|
||||||
|
FontMetrics fm = g2d.getFontMetrics(font);
|
||||||
|
// 测量文本长度
|
||||||
|
int textWidth = fm.stringWidth(text);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 导出文件
|
||||||
|
```java
|
||||||
|
ImageIO.write(image, "jpg", new File("D:/test.jpg"));
|
||||||
|
```
|
13
java/AWT.md
Normal file
13
java/AWT.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
tags: #java.awt #Graphics2D
|
||||||
|
---
|
||||||
|
# java.awt
|
||||||
|
|
||||||
|
`AWT` 全称 `Abstract window toolkit` `抽象窗口工具包`,提供了一组丰富的平台无关的方式来创建图形用户界面的库。
|
||||||
|
|
||||||
|
本人并不关心其用于制作用户界面的部分,主要使用它的图形库进行图像渲染,因此笔记中不会记录窗口或者控件之类的内容,仅记录渲染功能。
|
||||||
|
|
||||||
|
有用的链接:
|
||||||
|
|
||||||
|
- [API Reference: Package java.awt](https://www.apiref.com/java11-zh/java.desktop/java/awt/package-summary.html) AWT包的接口中文参考文档
|
||||||
|
|
Loading…
Reference in New Issue
Block a user