「java获取高度」java取长度

博主:adminadmin 2022-12-19 11:42:06 61

本篇文章给大家谈谈java获取高度,以及java取长度对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java 怎样获取标题栏的高度

自己已找到方法(没办法,没人回,只能靠自己。。):

用java.lang.Container的getInsets()方法可以获得标题栏的宽度和高度,Insets对象是容器边界的表示形式,有top,left,right,bottom四个属性可用。

An Insets object is a representation of the borders of a container. It specifies the space that a container must leave at each of its edges. The space can be a border, a blank space, or a title.

java 能获取到文字所使用某种字体后的宽度及高度吗

如果你指的是文字写入图片时的宽度的话,是可以获取的到的。

高度等于字体大小,即size。

宽度可以通过FontMetrics接口可以获取的到。

参考代码如下:

// 创建图片对象

BufferedImage bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

//得到Graphics2D 对象

Graphics2D g2d = (Graphics2D) bimg.getGraphics();

// 设置字体

Font font = new Font("宋体", Font.BOLD, 30);

g2d.setFont(font);

FontMetrics fm = g2d.getFontMetrics(font);

// 获取文字宽度

int textWidth = fm.stringWidth(text);

Java中字体的高度如何取得

可以使用:java.awt.Font类的getStringBounds函数,参考代码如下(57行):

package test;

import javax.imageio.ImageIO;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Program {

public static void main(String[] args) { 

try {

getImage(Font.BOLD);

System.out.println("finished.");

} catch (IOException e) { 

e.printStackTrace();

}

}

public static void getImage(int fontStyle) 

throws FileNotFoundException, IOException {

        // 得到图片缓冲区

        int width = 100;

        int height = 50;

        int imageType = BufferedImage.TYPE_INT_BGR;

        BufferedImage targetImage = new BufferedImage(width, height, imageType);

 

        // 得到画笔

        Graphics graphics = targetImage.getGraphics();

 

        // 设置背景色为白色

        graphics.setColor(Color.WHITE);

 

        // 画出一个矩形

        // 坐标x 坐标y 宽度100 长度50

        graphics.fillRect(0, 0, width, height);

 

        // 微软雅黑 粗体显示 大小25

        Font font = new Font("微软雅黑", fontStyle, 25);

        graphics.setFont(font);

 

        // 设置字的颜色 和 背景的颜色 要不同的

        graphics.setColor(Color.RED);

        

        int x = 20, y = 25;

 

        // 写字

        graphics.drawString("中文", 20, 35);

        

        // 获取到文字区域大小

        Rectangle2D rect = font.getStringBounds("中文", ((Graphics2D)graphics).getFontRenderContext());

        

        // 绘制方框将文字圈起来

        graphics.drawRect(x, (int)(y - (rect.getHeight() / 2)), (int)rect.getWidth(), (int)rect.getHeight());

 

        ImageIO.write(targetImage, "JPEG", new FileOutputStream("E:\\" + fontStyle + ".jpg"));

 

    }

}

圈起来的文字如下:

关于java获取高度和java取长度的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-19,除非注明,否则均为首码项目网原创文章,转载请注明出处。