Font Transforms, JTextPane, and AttributeSet
From: Will Friedman (williamf_at_alum.mit.edu)
Date: 03/28/04
- Next message: Thijs David: "JTree with checkboxes in it : Newbie"
- Previous message: peepn: "Jtrees and ToolTipManager"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Mar 2004 05:39:50 -0800
I am displaying styled text in a JTextPane using a
DefaultStyledDocument and setting the attributes of a
SimpleAttributeSet. I wish to alter the appearance of a character
using an AffineTransform on the font being used to display the text
for that character. Unfortunately, I am only able to set the font for
the attributes of that character using StyledConstants.SetFontFamily()
and StyledConstants.setFontSize(), which utterly disregards my
transform. Is there any way to correct this problem? Why is it that
there is no StyledConstants.setFont() method in the first place? A
code sample that demonstrates the problem by displaying text using the
unadjusted and adjusted font in JLabels and then in a JTextPane is
attached below.
Thanks in advance,
Will
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
public class Example extends JFrame {
public Example() {
super("Example");
this.setBackground(Color.black);
this.setResizable(false);
Font courier = new Font("Courier New", Font.PLAIN, 14);
FontRenderContext frc =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(1,1).createGraphics().getFontRenderContext();
Rectangle2D.Double maxCharSi ze = new Rectangle2D.Double();
maxCharSize.setRect(courier.getMaxCharBounds(frc));
System.out.println("max char size: " + maxCharSize.getHeight());
Rectangle2D.Double actualCharSize = new Rectangle2D.Double();
actualCharSize.setRect(courier.createGlyphVector(frc,
"\u2195").getGlyphMetrics(0).getBounds2D());
System.out.println("actual char size: " +
actualCharSize.getHeight());
double scaleFactor = maxCharSize.getHeight() /
actualCharSize.getHeight();
System.out.println("Scale factor: " + scaleFactor);
Font courierAdjusted =
courier.deriveFont(AffineTransform.getScaleInstance(1.0,
scaleFactor));
JPanel panel = new JPanel();
JLabel unadjusted = new JLabel("\u2195");
unadjusted.setFont(courier);
JLabel adjusted = new JLabel("\u2195");
adjusted.setFont(courierAdjusted);
panel.add(unadjusted);
panel.add(adjusted);
JTextPane pane = new JTextPane();
SimpleAttributeSet attr = new SimpleAttributeSet();
DefaultStyledDocument doc = new DefaultStyledDocument();
try {
StyleConstants.setFontFamily(attr, courier.getFamily());
StyleConstants.setFontSize(attr, courier.getSize());
StyleConstants.setBackground(attr, Color.black);
StyleConstants.setForeground(attr, Color.white);
doc.insertString(doc.getLength(), "\u2195", attr);
StyleConstants.setFontFamily(attr, courierAdjusted.getFamily());
StyleConstants.setFontSize(attr, courierAdjusted.getSize());
doc.insertString(doc.getLength(), "\u2195", attr);
} catch (Exception e) {
System.out.println("Creating the doc failed");
}
pane.setEditable(false);
pane.setBackground(Color.black);
pane.setDocument(doc);
panel.add(pane);
this.getContentPane().add(panel);
}
public static void main(String[] args) {
Example ex = new Example();
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.pack();
ex.setVisible(true);
}
}
- Next message: Thijs David: "JTree with checkboxes in it : Newbie"
- Previous message: peepn: "Jtrees and ToolTipManager"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|