Results 1 to 8 of 8
  1. #1
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default 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);
    	}
    
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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 11:22 PM. Reason: courtesy

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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?

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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);
    	}
    	
    }

  8. #8
    Join Date
    Sep 2009
    Location
    bangalore
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Problem in JSP rendering
    By srkumarj2ee@gmail.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 03:02 AM
  2. [SOLVED] AffineTransform
    By robocop in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 06:54 PM
  3. Help with JTable rendering
    By daniel2008 in forum AWT / Swing
    Replies: 8
    Last Post: 01-18-2009, 04:51 AM
  4. AffineTransform help
    By tones in forum New To Java
    Replies: 4
    Last Post: 12-19-2008, 08:24 AM
  5. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •