如何在 drawString Java 中更改字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18249592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Change Font Size in drawString Java
提问by zbz.lvlv
How to make the font size bigger in g.drawString("Hello World",10,10);
?
如何使字体变大g.drawString("Hello World",10,10);
?
采纳答案by John Snow
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
Where fontSize is a int. The API for drawStringstates that the x and y parameters are coordinates, and have nothing to do with the size of the text.
其中 fontSize 是一个整数。drawString的API声明 x 和 y 参数是坐标,与文本大小无关。
回答by daveca
Because you can't count on a particular font being available, a good approach is to derive a new font from the current font. This gives you the same family, weight, etc. just larger...
因为您不能指望特定字体可用,所以一个好方法是从当前字体派生出一种新字体。这给你相同的家庭,重量等,只是更大......
Font currentFont = g.getFont();
Font newFont = currentFont.deriveFont(currentFont.getSize() * 1.4F);
g.setFont(newFont);
You can also use TextAttribute.
您还可以使用 TextAttribute。
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, currentFont.getFamily());
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, (int) (currentFont.getSize() * 1.4));
myFont = Font.getFont(attributes);
g.setFont(myFont);
The TextAttribute method often gives one even greater flexibility. For example, you can set the weight to semi-bold, as in the example above.
TextAttribute 方法通常提供更大的灵活性。例如,您可以将权重设置为半粗体,如上例所示。
One last suggestion... Because the resolution of monitors can be different and continues to increase with technology, avoid adding a specific amount (such as getSize()+2 or getSize()+4) and consider multiplying instead. This way, your new font is consistently proportional to the "current" font (getSize() * 1.4), and you won't be editing your code when you get one of those nice 4K monitors.
最后一个建议...因为显示器的分辨率可能会有所不同,并且会随着技术不断增加,所以避免添加特定数量(例如 getSize()+2 或 getSize()+4),而应考虑相乘。这样,您的新字体始终与“当前”字体 (getSize() * 1.4) 成正比,并且当您获得那些不错的 4K 显示器之一时,您将不会编辑您的代码。
回答by Tiffany Tran
Font myFont = new Font ("Courier New", 1, 17);
The 17 represents the font size. Once you have that, you can put:
17 代表字体大小。一旦你有了它,你可以把:
g.setFont (myFont);
g.drawString ("Hello World", 10, 10);
回答by Kitty
code example below:
下面的代码示例:
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
g.drawString("Welcome to the Java Applet", 20 , 20);
回答by Laxman G
I've an image located at here, Using below code. I am able to contgrol any things on the text that i wanted to write (Eg,signature,Transparent Water mark, Text with differnt Font and size).
我有一张图片位于这里,使用下面的代码。我能够控制我想写的文本上的任何内容(例如,签名、透明水印、具有不同字体和大小的文本)。
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class ImagingTest {
public static void main(String[] args) throws IOException {
String url = "http://images.all-free-download.com/images/graphiclarge/bay_beach_coast_coastline_landscape_nature_nobody_601234.jpg";
String text = "I am appending This text!";
byte[] b = mergeImageAndText(url, text, new Point(100, 100));
FileOutputStream fos = new FileOutputStream("so2.png");
fos.write(b);
fos.close();
}
public static byte[] mergeImageAndText(String imageFilePath,
String text, Point textPosition) throws IOException {
BufferedImage im = ImageIO.read(new URL(imageFilePath));
Graphics2D g2 = im.createGraphics();
Font currentFont = g2.getFont();
Font newFont = currentFont.deriveFont(currentFont.getSize() * 1.4F);
g2.setFont(newFont);
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, currentFont.getFamily());
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, (int) (currentFont.getSize() * 2.8));
newFont = Font.getFont(attributes);
g2.setFont(newFont);
g2.drawString(text, textPosition.x, textPosition.y);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(im, "png", baos);
return baos.toByteArray();
}
}
回答by Jesus Bastiand
All you need to do is this: click on (window) on the dropdown manue on top of your screen. click on (Editor). click on (zoom in) as many times as you need to.
您需要做的就是:单击屏幕顶部下拉菜单上的(窗口)。单击(编辑器)。根据需要多次单击(放大)。