JTextPane Font's with AffineTransform Rendering
Hello everyone,
I'm currently having a little problem with getting a font to be displayed how I want inside a JTextPane.
I'm trying to replicate a Bitmap Font where instead of point size, a height magnification and width magnification is used. I've been able to draw the font correctly using AffineTransform to scale the font as needed on a Label, but I need to figure out how exactly I could replicate this inside a JTextPane or similar component.
Currently it seems to be ignoring the scaling on the derived font.
Any suggestions would be appreciated.
Here's a short example.
Code:
package Test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.text.AttributedString;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
public class Test2 {
static JDialog dialog;
static JPanel panel;
static JLabel label;
static JTextPane tpane;
/**
* @param args
*/
public static void main(String[] args){
dialog = new JDialog();
dialog.setSize(450, 450);
panel = new JPanel();
label = new JLabel(){
public final void paint(java.awt.Graphics g) {
super.paint(g);
//Draw the text in the TextPane
if (tpane != null){
try {
String text = tpane.getText();
if (!text.trim().equals("")){
AttributedString aString = new AttributedString(tpane.getText());
Font font = this.getFont();
font = font.deriveFont(AffineTransform.getScaleInstance(3, 1));
aString.addAttribute(TextAttribute.FONT, font);
g.drawString(aString.getIterator(), 15, 15);
}
} catch (Throwable e){
e.printStackTrace();
}
}
}
};
label.setPreferredSize(new Dimension(200,200));
tpane = new JTextPane();
tpane.setPreferredSize(new Dimension(200,200));
tpane.setFont(tpane.getFont().deriveFont(AffineTransform.getScaleInstance(3, 1)));
tpane.setText("This is what it looks like");
tpane.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent arg0) {
label.repaint();
}
});
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.NORTH);
panel.add(tpane, BorderLayout.SOUTH);
dialog.add(panel);
dialog.setModal(true);
dialog.setVisible(true);
System.exit(0);
}
}