如何使用图形在多行上输出字符串 [英] How to output a String on multiple lines using Graphics

查看:106
本文介绍了如何使用图形在多行上输出字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用 g.drawString()来绘制一些stings,我的程序重写了 public void paint(Graphics g,int x,int y); someString,x + 10,y + 30);



现在someString可能很长,因此可能不适合一行。



在多行上写文本的最佳方法是什么?例如,在一个矩形(x1,y1,x2,y2)中?

解决方案

感谢Epaga的暗示和一些网上的例子(不是很明显找到!我主要用打破用于文本布局的行),我可以使组件显示包装文本。它是不完整的,但至少它显示了预期的效果。

  class TextContainer extends JPanel 
{
private int m_width;
private int m_height;
private String m_text;
private AttributedCharacterIterator m_iterator;
private int m_start;
private int m_end;

public TextContainer(String text,int width,int height)
{
m_text = text;
m_width = width;
m_height = height;

AttributedString styledText = new AttributedString(text);
m_iterator = styledText.getIterator();
m_start = m_iterator.getBeginIndex();
m_end = m_iterator.getEndIndex();
}

public String getText()
{
return m_text;
}

public Dimension getPreferredSize()
{
return new Dimension(m_width,m_height);
}

public void paint(Graphics g)
{
super.paintComponent(g);

Graphics2D g2 =(Graphics2D)g;
FontRenderContext frc = g2.getFontRenderContext();

LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator,frc);
measurer.setPosition(m_start);

float x = 0,y = 0;
while(measurer.getPosition()< m_end)
{
TextLayout layout = measurer.nextLayout(m_width);

y + = layout.getAscent();
float dx = layout.isLeftToRight()?
0:m_width - layout.getAdvance();

layout.draw(g2,x + dx,y);
y + = layout.getDescent()+ layout.getLeading();
}
}
}

它适合一个圆圈(唉,没有理由,似乎):

  public void paint(Graphics g)
{
super.paintComponent(g);

Graphics2D g2 =(Graphics2D)g;
FontRenderContext frc = g2.getFontRenderContext();

LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator,frc);
measurer.setPosition(m_start);

float y = 0; $(测试者.getPosition()< m_end)
{
double ix = Math.sqrt((m_width / 2-y)* y);
float x = m_width / 2.0F - (float)ix;
int width =(int)ix * 2;

TextLayout layout = measurer.nextLayout(width);

y + = layout.getAscent();
float dx = layout.isLeftToRight()?
0:width - layout.getAdvance();

layout.draw(g2,x + dx,y);
y + = layout.getDescent()+ layout.getLeading();
}
}

虽然我不太确定dx计算。

My Program overrides public void paint(Graphics g, int x, int y); in order to draw some stings using g.drawString(someString, x+10, y+30);

Now someString can be quite long and thus, it may not fit on one line.

What is the best way to write the text on multiple line.
For instance, in a rectangle (x1, y1, x2, y2)?

解决方案

Thanks to Epaga's hint and a couple of examples on the Net (not so obvious to find! I used mainly Break a Line for text layout), I could make a component to display wrapped text. It is incomplete, but at least it shows the intended effect.

class TextContainer extends JPanel
{
    private int m_width;
    private int m_height;
    private String m_text;
    private AttributedCharacterIterator m_iterator;
    private int m_start;
    private int m_end;

    public TextContainer(String text, int width, int height)
    {
        m_text = text;
        m_width = width;
        m_height = height;

        AttributedString styledText = new AttributedString(text);
        m_iterator = styledText.getIterator();
        m_start = m_iterator.getBeginIndex();
        m_end = m_iterator.getEndIndex();
    }

    public String getText()
    {
        return m_text;
    }

    public Dimension getPreferredSize()
    {
        return new Dimension(m_width, m_height);
    }

    public void paint(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        FontRenderContext frc = g2.getFontRenderContext();

        LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator, frc);
        measurer.setPosition(m_start);

        float x = 0, y = 0;
        while (measurer.getPosition() < m_end)
        {
            TextLayout layout = measurer.nextLayout(m_width);

            y += layout.getAscent();
            float dx = layout.isLeftToRight() ?
                    0 : m_width - layout.getAdvance();

            layout.draw(g2, x + dx, y);
            y += layout.getDescent() + layout.getLeading();
        }
    }
}

Just for fun, I made it fitting a circle (alas, no justification, it seems):

public void paint(Graphics g)
{
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;
    FontRenderContext frc = g2.getFontRenderContext();

    LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator, frc);
    measurer.setPosition(m_start);

    float y = 0;
    while (measurer.getPosition() < m_end)
    {
        double ix = Math.sqrt((m_width / 2 - y) * y);
        float x = m_width / 2.0F - (float) ix;
        int width = (int) ix * 2;

        TextLayout layout = measurer.nextLayout(width);

        y += layout.getAscent();
        float dx = layout.isLeftToRight() ?
                0 : width - layout.getAdvance();

        layout.draw(g2, x + dx, y);
        y += layout.getDescent() + layout.getLeading();
    }
}

I am not too sure about dx computation, though.

这篇关于如何使用图形在多行上输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆