Java Graphics无法更改Font

时间:2009-04-16 18:04:49

标签: java graphics word-wrap

我正在尝试使用Graphics对象进行自定义绘制。 我想用自动换行样式绘制文本,所以我使用了一些我在本页的某处找到的示例代码。

我的问题是我无法以任何方式改变字体大小。 请帮帮我,我附上了下面的可执行源代码。

由于

克里斯

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.swing.JFrame;

public class JavaFontProblem extends JFrame {

    public JavaFontProblem() {
        setSize(new Dimension(1024, 768));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        String longtext = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";

        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Verdana", Font.PLAIN, 250));

        AttributedString atString = new AttributedString(longtext);
        AttributedCharacterIterator iterator = atString.getIterator();

        LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, g2d
                .getFontRenderContext());
        measurer.setPosition(iterator.getBeginIndex());

        int m_width = 500;

        float x = 50, y = 50;
        while (measurer.getPosition() < iterator.getEndIndex()) {
            TextLayout layout = measurer.nextLayout(m_width);

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

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

    }

    public static void main(String[] args) {
        new JavaFontProblem().setVisible(true);
    }

}

2 个答案:

答案 0 :(得分:3)

atString.addAttribute(TextAttribute.FONT, myFont, 0, atString.length());

答案 1 :(得分:1)

您需要发送longtext的长度...而不是atString length

此外,您必须声明自己的Font对象。它也不需要你的setFont。

import java.awt.font.TextAttribute;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.swing.JFrame;

public class JavaFontProblem extends JFrame {

    public JavaFontProblem() {
        setSize(new Dimension(1024, 768));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        String longtext = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";

        g2d.setColor(Color.BLACK);
        //g2d.setFont(new Font("Verdana", Font.PLAIN, 250));

        Font myFont = new Font("Verdana", Font.PLAIN, 250);

        AttributedString atString = new AttributedString(longtext);
        atString.addAttribute(TextAttribute.FONT, myFont, 0, longtext.length());


        AttributedCharacterIterator iterator = atString.getIterator();

        LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, g2d
                        .getFontRenderContext());
        measurer.setPosition(iterator.getBeginIndex());

        int m_width = 500;

        float x = 50, y = 50;
        while (measurer.getPosition() < iterator.getEndIndex()) {
                TextLayout layout = measurer.nextLayout(m_width);

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

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

    }

    public static void main(String[] args) {
        new JavaFontProblem().setVisible(true);
    }

}