Results 1 to 8 of 8
- 09-17-2009, 08:07 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
Java 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); } }
- 09-17-2009, 08:14 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
I have also tried setting the character attributes as described here:
javatechniques.com/blog/setting-jtextpane-font-and-color/
This works with just regular point size fonts, but not the derived bitmap fonts I require.
-
Your code works for me. Note that the JLabel text will be bold (Font.BOLD), while the JTextPane's font will be plain because this is the default for their standard fonts which you are deriving from.
- 09-17-2009, 10:20 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Thanks for the reply Fubarable,
What version Java are you using? I'm restricted to using 1.4.
Here's a screen shot of what I'm seeing, can you verify that yours is different?
i686.photobucket.com/albums/vv226/StormyWaters86/JTextPaneProblem.jpg
The problem I'm seeing is that the font's width is not being resized by a factor of 3 in the JTextPane which is what I'm looking for.Last edited by StormyWaters; 09-17-2009 at 10:22 PM. Reason: courtesy
-
I'm using Java 1.6 set at a "compliance level of 1.5. My text in the label looks exactly the same as the text in the JTextPane except that the label's text is not bold -- in short, it looks very different from the image that you are showing.
- 09-18-2009, 04:04 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Alright I see what you are seeing, but I need a solution for Java 1.4 as this is what I'm limited to.
Any suggestions or ideas on how to achieve that result in Java 1.4?
- 09-18-2009, 09:50 PM #7
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
I've decided to just try to get this fully working using the same environment as you, but ran into another problem.
I'm using a custom font which I made and placed into the jdk's lib/font folder so it can be found. I'm seeing the same results as when ran under a 1.4 compile.
Here's a screen shot: I've decided to just try to get this fully working using the same environment as you, but ran into another problem.
I'm using a custom font which I made and placed into the jdk's lib/font folder so it can be found. I'm seeing the same results as when ran under a 1.4 compile.
Here's a screen shot:
http://i686.photobucket.com/albums/v...CustomFont.jpg
Can you get the same results using a custom font instead of grabbing the basic font of the label/textpane?
Here's a basic example again:
Java Code: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 Tester { static JDialog dialog; static JPanel panel; static JLabel label; static JTextPane tpane; static Font specialFont; public static Font getSpecialFont(){ if (specialFont == null){ specialFont = new Font("ZEBRASTANDARD", Font.PLAIN, 12); specialFont = specialFont.deriveFont(AffineTransform.getScaleInstance(3, 1)); } return specialFont; } /** * @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()); aString.addAttribute(TextAttribute.FONT, getSpecialFont()); 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(getSpecialFont()); 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); } }
- 09-18-2009, 09:56 PM #8
Member
- Join Date
- Sep 2009
- Location
- bangalore
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Problem in JSP rendering
By srkumarj2ee@gmail.com in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2009, 02:02 AM -
[SOLVED] AffineTransform
By robocop in forum New To JavaReplies: 2Last Post: 03-25-2009, 05:54 PM -
Help with JTable rendering
By daniel2008 in forum AWT / SwingReplies: 8Last Post: 01-18-2009, 03:51 AM -
AffineTransform help
By tones in forum New To JavaReplies: 4Last Post: 12-19-2008, 07:24 AM -
JLabel Rendering
By random4534 in forum New To JavaReplies: 3Last Post: 12-16-2008, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks