如何在 g.drawString("Hello World",10,10);
中增大字体大小?
如何在 g.drawString("Hello World",10,10);
中增大字体大小?
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
fontSize 是一个整数。 drawString API 中指定的 x 和 y 参数是坐标,与文本大小无关。
g.getFont().getFontName()
替换字体名称参数来设置字体大小而不改变字体名称。 - shieldgenerator7deriveFont(float size)
。 - Yohanes Khosiawan 许先汉g.getFont().getFontName()
是最好的选择,但在我测试过的所有系统上,它都是 Dialog
。 - Scruffy由于字体的可用性无法保证,因此一个好的方法是从当前字体中派生一个新的字体,从而获得相同的字族、字重等属性,只是字体更大了...
Font currentFont = g.getFont();
Font newFont = currentFont.deriveFont(currentFont.getSize() * 1.4F);
g.setFont(newFont);
您也可以使用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);
TextAttribute方法通常会给用户带来更大的灵活性。例如,您可以像上面的示例一样将字体加粗到半粗。
最后一个建议...由于显示器的分辨率可能不同,并且随着技术的进步而不断增加,避免添加特定数量(例如getSize()+2
或getSize()+4
),考虑使用乘法。这样,您的新字体大小将始终与“当前”字体成比例(getSize() * 1.4
),并且在获得其中一款漂亮的4K显示器时,您不必编辑代码。
Font myFont = new Font ("Courier New", 1, 17);
g.setFont (myFont);
g.drawString ("Hello World", 10, 10);
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
g.drawString("Welcome to the Java Applet", 20 , 20);
我有一张图片,位于这里。使用以下代码,我能够控制任何我想写的文本(例如,签名、透明水印、不同字体和大小的文本)。
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();
}
}
setFont()
吗? - trashgodString string =“Hello World”;
然后创建一个Font对象Font stringFont = new Font(“SansSerif”,Font.PLAIN,18);
接下来将Font对象设置为g或g2d对象g2d.setFont(stringFont);
现在将g2d(或g)对象应用于您的drawstring对象g2d.drawString(string,Xposition,Yposition)
其中X和Y是定位坐标的整数。 - Max West