Use a scaled font within Styled JTextPane
From: SPG (steve.goodsell_at_nopoo.blueyonder.co.uk)
Date: 03/25/05
- Next message: Daniel Santos: "Re: Border layout and Jlist"
- Previous message: Thomas Baruchel: "Looking for a component for previewing formatted text"
- Next in thread: John McGrath: "Re: Use a scaled font within Styled JTextPane"
- Reply: John McGrath: "Re: Use a scaled font within Styled JTextPane"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 25 Mar 2005 08:24:34 GMT
Hi,
I want to be able to resize a font that is used within my styled JTextPane
so that it grows both horizontally and vertically when the form resizes.
I have the code to rescale the font (posted below), and that can be applied
to a label which works fine.
What I want to do is set this newly rescaled font as the font for the styled
JTextPane, but I cannot seem to make it work.
I have tried setting the font using textPane.setFont(), I have also tried
resetting the styles but there is only a fotnSize atrribute not a specific
method for setting the real font to be used for a style..
>From the code below, assume that handleResize() is called from a component
listener - when the component resizes. This is definitely being called.
Assume also that addStyles() is using a JTextPane set as a class level var,
as is the _font.
The rescale appears to work for the label but not the text pane.
Any ideas,
Steve
private void handleResize(){
Rectangle bounds = _textPane.getBounds();
try{
String line = "";
for(int i=0; i< 80; i++){
line+="X";
}
Graphics g = this.getGraphics();
FontMetrics fm = g.getFontMetrics(_font);
int strWidth = fm.stringWidth(line);
int strHeight = fm.getHeight();
int gridHeight = strHeight * 25;
//Now try and work out the scaling!!!
int componentYRange = bounds.height - bounds.y;
int componentXRange = bounds.width - bounds.x;
double scaleXFactor = (double) componentXRange / strWidth;
double scaleYFactor = (double) componentYRange / gridHeight;
AffineTransform fontTransform = new AffineTransform();
fontTransform.scale(scaleXFactor, scaleYFactor);
//New Scaled Font
_font = _font.deriveFont(fontTransform);
System.out.println(_font.getSize() + "("+scaleXFactor + ", " +
scaleYFactor+")");
Style s = _textPane.getStyle("regular");
StyleConstants.setForeground(s, new
Color((int)(Math.random()*255),
(int)(Math.random()*255),(int)(Math.random()*255) ));
_testLabel.setFont(_font); //WORKS FINE
_textPane.setFont(_font); //DOES NTO DO ANYTHING
//reapplyStyles(_textPane,s);
}catch(Exception err){
System.out.println(err.getMessage());
err.printStackTrace();
}
}
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("STYLE CHANGED!");
reapplyStyles(_textPane, (Style)e.getSource());
}
};
_textPane.setFont(_font);
Style regular = doc.addStyle("regular", null);
//StyleConstants.setFontFamily(regular, _font.getFamily());
//StyleConstants.setFontSize(regular, _font.getSize());
StyleConstants.setForeground(regular, Color.GREEN);
StyleConstants.setBackground(regular, Color.BLACK);
regular.addChangeListener(changeListener);
Style s = doc.addStyle("italic", null);
s.addAttributes(regular);
StyleConstants.setItalic(s, true);
s.addChangeListener(changeListener);
s = doc.addStyle("bold", null);
s.addAttributes(regular);
StyleConstants.setBold(s, true);
s.addChangeListener(changeListener);
s = doc.addStyle("underline", null);
s.addAttributes(regular);
StyleConstants.setUnderline(s, true);
s.addChangeListener(changeListener);
}
- Next message: Daniel Santos: "Re: Border layout and Jlist"
- Previous message: Thomas Baruchel: "Looking for a component for previewing formatted text"
- Next in thread: John McGrath: "Re: Use a scaled font within Styled JTextPane"
- Reply: John McGrath: "Re: Use a scaled font within Styled JTextPane"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|