You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by Thierry Kormann <Th...@sophia.inria.fr> on 2000/11/07 09:56:31 UTC

Re: Meeting with the 2D team

Hi,

I don't know if it will help the 2D team, but here is a small example that 
illustrates the difference for TextLayout.


---


import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class Text extends JComponent {

    TextLayout layout;

    public Text() {
        FontRenderContext frc = new FontRenderContext(new AffineTransform(),
                                                      true,
                                                      true);

        Map attrs = new HashMap();
        attrs.put(TextAttribute.FOREGROUND, Color.black);
        attrs.put(TextAttribute.FAMILY, "sansserif");
        attrs.put(TextAttribute.SIZE, new Float(10f));
        layout = new TextLayout("The quick brown fox", attrs, frc);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                             RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        //
        // Clip is a rectangular region
        //
        g2d.setClip(new Rectangle(10, 10, 300, 400));
        layout.draw(g2d, 50f, 90f); // draw using TextLayout: BAD!

        g2d.setColor(Color.black);
        AffineTransform Tx = AffineTransform.getTranslateInstance(50f, 110f);
        g2d.fill(layout.getOutline(Tx)); // draw the outline : OK!

        //
        // Clip is a shape
        //
        g2d.setClip(new GeneralPath(new Rectangle(10, 10, 300, 400)));
        layout.draw(g2d, 50f, 140f); // draw using TextLayout: OK!

        g2d.setColor(Color.black);
        Tx = AffineTransform.getTranslateInstance(50f, 160f);
        g2d.fill(layout.getOutline(Tx)); // draw the outline: OK!

    }

    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    public static void main(String [] args) {
        JFrame frame = new JFrame("Text");
        frame.getContentPane().add(new Text(), BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.show();
    }
}

---

Thierry.

-- 
Thierry Kormann
email: Thierry.Kormann@sophia.inria.fr  http://www.inria.fr/koala/tkormann/
Koala/Dyade/Bull @ INRIA - Sophia Antipolis