使用DecimalFormat(强制点)和drawString()(y似乎是错误的)
两个问题合而为一,但我有一个非常短的测试用例来展示我的问题:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
public class Numbers {
public static void main(String[] args) throws IOException {
double[] numbers = { 10000.12, 20000.23, 3000.45 };
DecimalFormat format = new DecimalFormat("0.00");
format.setGroupingUsed(false);
BufferedImage image = new BufferedImage(400, 150, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.setBackground(Color.YELLOW);
g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 24));
FontMetrics metrics = g2d.getFontMetrics();
for (int i = 0; i < numbers.length; i++) {
String str = format.format(numbers[i]);
System.out.println(i + ": " + str);
int w = metrics.stringWidth(str);
int h = metrics.getHeight();
int x = 100 * i;
g2d.drawString(str, x - w/2, 0);
}
g2d.dispose();
ImageIO.write(image, "PNG", new File("Numbers.png"));
}
}
当我使用它时,我得到:
C:\Temp>javac -version
javac 1.6.0_24
C:\Temp>javac Numbers.java
C:\Temp>java Numbers
0: 10000,12
1: 20000,23
2: 3000,45
并且生成了此图像:
我的问题:
- 如何强制 DecimalFormat 使用点而不是逗号,而不切换区域设置?
- w 似乎是正确的,但是 0 作为高度有什么问题 - 为什么数字打印在屏幕外?
谢谢你!亚历克斯
更新:
我最终使用了区域设置。 str.replace(".", ",") 注释也很酷。
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
public class Numbers {
public static void main(String[] args) throws IOException {
double[] numbers = { 10000.12, 20000.23, 3000.45 };
BufferedImage image = new BufferedImage(400, 150, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.setBackground(Color.GRAY);
g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 24));
FontMetrics metrics = g2d.getFontMetrics();
NumberFormat format = NumberFormat.getInstance(Locale.US);
format.setGroupingUsed(false);
if (format instanceof DecimalFormat) {
((DecimalFormat) format).applyPattern("0.00");
}
for (int i = 0; i < numbers.length; i++) {
String str = format.format(numbers[i]);
System.out.println(i + ": " + str);
int w = metrics.stringWidth(str);
int h = metrics.getHeight();
int x = 100 * i;
g2d.drawString(str, x - w/2, h);
}
g2d.dispose();
ImageIO.write(image, "PNG", new File("Numbers.png"));
}
}
Two questions in one, but I have a very short test case demonstrating my problems:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
public class Numbers {
public static void main(String[] args) throws IOException {
double[] numbers = { 10000.12, 20000.23, 3000.45 };
DecimalFormat format = new DecimalFormat("0.00");
format.setGroupingUsed(false);
BufferedImage image = new BufferedImage(400, 150, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.setBackground(Color.YELLOW);
g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 24));
FontMetrics metrics = g2d.getFontMetrics();
for (int i = 0; i < numbers.length; i++) {
String str = format.format(numbers[i]);
System.out.println(i + ": " + str);
int w = metrics.stringWidth(str);
int h = metrics.getHeight();
int x = 100 * i;
g2d.drawString(str, x - w/2, 0);
}
g2d.dispose();
ImageIO.write(image, "PNG", new File("Numbers.png"));
}
}
When I use it I get:
C:\Temp>javac -version
javac 1.6.0_24
C:\Temp>javac Numbers.java
C:\Temp>java Numbers
0: 10000,12
1: 20000,23
2: 3000,45
and this image produced:
My questions:
- How to force the DecimalFormat to use dot and not comma, without switching locale?
- The w seems to be correct, but what is wrong with 0 as the height - why are numbers printed offscreen?
Thank you! Alex
UPDATE:
I've ended up using Locale. The str.replace(".", ",") comment is cool too.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
public class Numbers {
public static void main(String[] args) throws IOException {
double[] numbers = { 10000.12, 20000.23, 3000.45 };
BufferedImage image = new BufferedImage(400, 150, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.setBackground(Color.GRAY);
g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 24));
FontMetrics metrics = g2d.getFontMetrics();
NumberFormat format = NumberFormat.getInstance(Locale.US);
format.setGroupingUsed(false);
if (format instanceof DecimalFormat) {
((DecimalFormat) format).applyPattern("0.00");
}
for (int i = 0; i < numbers.length; i++) {
String str = format.format(numbers[i]);
System.out.println(i + ": " + str);
int w = metrics.stringWidth(str);
int h = metrics.getHeight();
int x = 100 * i;
g2d.drawString(str, x - w/2, h);
}
g2d.dispose();
ImageIO.write(image, "PNG", new File("Numbers.png"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 1:您可以使用
format.applyLocalizedPattern("#00.0#");
指定格式,而不是format.setGroupingUsed(false);
另一种选择是使用自定义格式符号:
有关本地化模式的更多信息和一些示例可以在此处找到:
http://download.oracle.com/javase/tutorial/i18n/format /decimalFormat.html 和 http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html#numberpattern
对于你的第二个问题,当你使用
g2d.drawString(str, x, y)
基线位于 x,y,而不是绘制矩形时的左上角位置。基线基本上是 a、b、c 等字母的底部。逗号和字母(例如 g、j、y)延伸到基线以下。您基本上需要将文本的高度添加到要显示文本的 y 位置。For 1: You can specify the format using
format.applyLocalizedPattern("#00.0#");
instead offormat.setGroupingUsed(false);
Another option is to use custom format symbols:
More information about localized patterns and some examples can be found here:
http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html and http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html#numberpattern
For your second question, When you use
g2d.drawString(str, x, y)
the baseline is at x,y not the top left position as when you draw a rectangle. The baseline is basically the bottom of letters like a, b, c. Commas and letters like g, j, y extend below the baseline. You basically need to add the height of the text to the y position where you want to display the text.