[英]setBackground() method is not working for Graphics2D object
我需要有關 Graphics2D class 的幫助,我不確定為什么setBackground() 方法不起作用,渲染的圖像背景只是保持白色。
int width = 128, height = 32;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
Font font = new Font("TimesRoman", Font.BOLD, 12);
ig2.setBackground(Color.BLACK);
ig2.setFont(font);
String message = "custom text";
FontMetrics fontMetrics = ig2.getFontMetrics();
int stringWidth = fontMetrics.stringWidth(message);
int stringHeight = fontMetrics.getAscent();
ig2.setPaint(Color.red);
ig2.drawString(message, stringWidth - width/2, height / 4 + stringHeight / 6);
ImageIO.write(bi, "PNG", new File("src/output.jpeg"));
謝謝你們的時間
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.