使用DecimalFormat(强制点)和drawString()(y似乎是错误的)

发布于 2024-11-19 20:51:46 字数 3264 浏览 5 评论 0原文

两个问题合而为一,但我有一个非常短的测试用例来展示我的问题:

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

并且生成了此图像:

Numbers.png

我的问题:

  1. 如何强制 DecimalFormat 使用点而不是逗号,而不切换区域设置?
  2. 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:

Numbers.png

My questions:

  1. How to force the DecimalFormat to use dot and not comma, without switching locale?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-11-26 20:51:46

对于 1:您可以使用 format.applyLocalizedPattern("#00.0#"); 指定格式,而不是 format.setGroupingUsed(false);

另一种选择是使用自定义格式符号:

DecimalFormatSymbols decimalSymbol = new DecimalFormatSymbols(Locale.getDefault());
decimalSymbol.setDecimalSeparator('.');
format.setGroupingUsed(false);

有关本地化模式的更多信息和一些示例可以在此处找到:
http://download.oracle.com/javase/tutorial/i18n/format /decimalFormat.htmlhttp://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 of format.setGroupingUsed(false);

Another option is to use custom format symbols:

DecimalFormatSymbols decimalSymbol = new DecimalFormatSymbols(Locale.getDefault());
decimalSymbol.setDecimalSeparator('.');
format.setGroupingUsed(false);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文