Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java(AWT):将文本放入框中_Java_Fonts_Awt - Fatal编程技术网

Java(AWT):将文本放入框中

Java(AWT):将文本放入框中,java,fonts,awt,Java,Fonts,Awt,我有一个扩展框架的应用程序。然后,它将使用以下命令显示几行文本: Font f = new Font("Arial", Font.PLAIN, 10); g.setFont(f); g.drawString("Test|great Yes ^.", x, y + 10); 现在发生的是,文本不适合放在周围的框中。例如,我希望文本适合[x,y]-[x+宽度,y+10](不关心宽度),但它稍微低于y+10行。现在对于大多数字符('T'、'e'等),这是合适的,但“|”和'g'不适合!它们位于y+1

我有一个扩展框架的应用程序。然后,它将使用以下命令显示几行文本:

Font f = new Font("Arial", Font.PLAIN, 10);
g.setFont(f);
g.drawString("Test|great Yes ^.", x, y + 10);
现在发生的是,文本不适合放在周围的框中。例如,我希望文本适合[x,y]-[x+宽度,y+10](不关心宽度),但它稍微低于y+10行。现在对于大多数字符('T'、'e'等),这是合适的,但“|”和'g'不适合!它们位于y+10线以下。似乎您不能使用:在y+字符高度绘制。但是什么有效呢

为了理解我的意思,这里有一些示例代码:

import java.awt.*;

public class test extends Frame
{
        public test()
        {
                /* retrieve max window size */
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice[] gs = ge.getScreenDevices();
                GraphicsConfiguration [] gc = gs[0].getConfigurations();
                Rectangle r = gc[0].getBounds();
                setSize(r.width, r.height);
                setVisible(true);
        }

        public void paint(Graphics g)
        {
                final int windowWidth  = getSize().width;
                final int windowHeight = getSize().height;
                g.setColor(Color.BLUE);
                g.fillRect(0, 0, windowWidth, windowHeight);
                g.setColor(Color.WHITE);
                g.fillRect(0, 100, windowWidth, 110);
                int textHeight = 100;
                Font f = new Font("Arial", Font.PLAIN, textHeight);
                g.setFont(f);
                g.setColor(Color.BLACK);
                g.drawString("Test|great Yes ^.", 10, 100 + textHeight);
        }

        public void guiLoop()
        {
                for(;;) { try { Thread.sleep(1000); } catch(Exception e) { } }
        }

        public static void main(String [] args)
        {
                new test().guiLoop();
        }
}
我还尝试了以下代码:

public void paint(Graphics g)
{
        final int windowWidth  = getSize().width;
        final int windowHeight = getSize().height;
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, windowWidth, windowHeight);
        g.setColor(Color.WHITE);
        g.fillRect(0, 100, windowWidth, 110);
        int textHeight = 100;

        String str = "Test|great Yes ^.";
        Font f = new Font("Arial", Font.PLAIN, textHeight);
        Rectangle2D boundingRectangle = f.getStringBounds(str, 0, str.length(), new FontRenderContext(null, false, false));
        f = f.deriveFont((float)(textHeight * (textHeight / boundingRectangle.getHeight())));
        boundingRectangle = f.getStringBounds(str, 0, str.length(), new FontRenderContext(null, false, false));
        g.drawString(str, 10, 100 + (int)boundingRectangle.getHeight());

        g.setFont(f);
        g.setColor(Color.BLACK);
        g.drawString(str, 10, 100 + textHeight);
}
这稍微好一些:文本较小,因此可能适合,但仍然存在y位置不正确的问题


感谢您的帮助

如何使用
FontMetrics
?您可以使用
g.getFontMetrics()
Graphics
对象中获取它

然后,您可以检索最大下降或上升高度,或者直接检索高度(使用
getHeight
),因此您的实现与字体无关,应该可以正常工作。。检查文档

编辑(解释评论):
没有一种直接的方法可以告诉字符串以适合盒子的方式绘制自己。你必须自己做这件事。。例如,从最大字体大小开始,检查宽度是否适合该框,否则减小大小并重试。对于高度,您应该首先确定(或获得)最大字体高度,然后您可以设置框的像素数。

我想我解决了这个问题:

boundingBoxHeight:文本应位于其中的框的高度 Y偏移开始绘制字体的位置

            Font f = new Font("Arial", Font.PLAIN, boundingBoxHeight);
            g.setFont(f);
            FontMetrics fm = g.getFontMetrics();
            double shrink = ((double)textHeight / (double)fm.getHeight());
            double newSize = (double)textHeight * shrink;
            double newAsc  = (double)fm.getAscent() * shrink;
            int yOffset = (int)newAsc - fm.getLeading();
            f = f.deriveFont((float)newSize);
            g.setFont(f);

            g.drawString(str, 10, 100 + yOffset);

但是文本上方有很多空白。

Guilloop()真的有必要吗?是的,特别是FontMetrics.stringWidth():是的,但是如何处理这些指标呢?我尝试在y+fontmetrics.getAscent()处绘制字符串,这也会将文本绘制得太低。另一个问题是,这些方法会告诉您文本将变得多大:是否有一种方法可以说此字符串必须适合此边界框?我认为stringWidth不是这种情况,因为我们讨论的是高度。您应该使用getHeight,文档中是这样说的:“获取此字体中文本行的标准高度。这是相邻文本行基线之间的距离。它是前导+上升+下降的总和。”您无法绑定字符串,它应该怎么做?自动调整大小?