Scaling

Discussion in 'Java' started by freesoft_2000, Oct 10, 2005.

  1. Hi everyone,

    I have a JTextPane with some styled text in it and i am
    trying to scale them.

    What i mean is that assuming that the text inside the
    JTextPane on startup is a 100% in size. After i click some JButton all the
    font size of the entire document is reduced to 25%

    The thing is that this JTextPane contains alot of text
    that has different font sizes thus i want each of their respective font
    sizes of the character to be 25% of its original value.

    Does anyone know how to do this?

    But is there another way i could achieve the scaling
    of my styled text in my JTextPane so that it is 25% of its original value
    by means of subclassing my JTextPane or StyledDocument class and overiding
    its paint method?

    Does anyone know how to do this?

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
     
    freesoft_2000, Oct 10, 2005
    #1
    1. Advertising

  2. "freesoft_2000" <> wrote:
    > I have a JTextPane with some styled text in it and i am
    > trying to scale them.
    >
    > What i mean is that assuming that the text inside the
    > JTextPane on startup is a 100% in size. After i click some JButton all the
    > font size of the entire document is reduced to 25%
    >
    > The thing is that this JTextPane contains alot of text
    > that has different font sizes thus i want each of their respective font
    > sizes of the character to be 25% of its original value.
    >
    > Does anyone know how to do this?
    >
    > But is there another way i could achieve the scaling
    > of my styled text in my JTextPane so that it is 25% of its original value
    > by means of subclassing my JTextPane or StyledDocument class and overiding
    > its paint method?
    >
    > Does anyone know how to do this?


    I remember a similar question in group comp.lang.java.help.
    Google for "html with jtextpane" on 4.July 2005.
    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Oct 10, 2005
    #2
    1. Advertising

  3. freesoft_2000

    Roedy Green Guest

    On Mon, 10 Oct 2005 11:43:03 -0400, "freesoft_2000"
    <> wrote or quoted :

    > The thing is that this JTextPane contains alot of text
    >that has different font sizes thus i want each of their respective font
    >sizes of the character to be 25% of its original value.


    I suppose you might do something with an AffineTransform and pass the
    original paint method a modified Graphics object. I don't know if
    fonts grow and shrink too.

    See http://mindprod.com/jgloss/affinetransform.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 10, 2005
    #3
  4. Hi everyone,

    I think you guys have misunderstood my question in that i am
    not talking about scaling for printing but something of a zoom in and zoom
    out for the JTextPane

    Here is a full compilable example where you guys can compile the code and
    see what i mean first hand

    Code:
    import java.awt.*;
    import java.util.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class JTextScalar
    
    {
    JFrame fr = new JFrame ("Frame");
    
    JScalableTextPane TextPane1 = new JScalableTextPane();
    
    StyleContext sc = new StyleContext();
    
    DefaultStyledDocument dse = new DefaultStyledDocument(sc);
    
    JScrollPane ScrollPane1 = new JScrollPane(TextPane1,
    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    StyledEditorKit StyledEditorKit1 = new StyledEditorKit();
    
    
    JViewport viewport;
    
    public void initialize()
    {
    Container pane = fr.getContentPane();
    pane.setLayout(new BorderLayout());
    fr.setSize(250,300);
    fr.setLocation(300,300);
    fr.setBackground(Color.lightGray);
    
    dse.putProperty("i18n", Boolean.TRUE);
    
    TextPane1.setEditorKit(StyledEditorKit1);
    
    //The below command line sets the document that the JTextPane will be
    //be referencing to
    
    TextPane1.setDocument(dse);
    
    viewport = new JViewport();
    viewport.setView(TextPane1);
    viewport.setScrollMode(viewport.SIMPLE_SCROLL_MODE);
    //viewport.setScrollMode(viewport.BACKINGSTORE_SCROLL_MODE);
    ScrollPane1.setViewport(viewport);
    
    pane.add("Center", ScrollPane1);
    //ScrollPane1.invalidate();
    ScrollPane1.revalidate();
    ScrollPane1.repaint();
    TextPane1.revalidate();
    TextPane1.repaint();
    
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr.pack();
    fr.setVisible(true);
    }
    
    public static void main(String args[])
    {
    JTextScalar a = new JTextScalar();
    a.initialize();
    }
    }
    
    class JScalableTextPane extends JTextPane
    
    {
    /*
    public void paintComponent(Graphics g)
    {
    //This function overrides the JPanel paintComponent function
    //and paints the buffered image on the JPEGPanel
    
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    
    g2d.scale(0.5, 0.5);
    //AffineTransform old = g2d.getTransform();
    //g2d.setTransform(old);
    //super.notify();
    //super.revalidate();
    //super.repaint();
    super.paintComponent(g2d);
    }
    */
    public void paint(Graphics g)
    {
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    
    g2d.scale(0.5,0.5);
    AffineTransform old = g2d.getTransform();
    g2d.setTransform(old);
    super.paint(g2d);
    }
    
    public Dimension getPreferredSize()
    {
    Dimension d= super.getPreferredSize();
    d.setSize(d.width*0.5,d.height*0.5);
    return d;
    }
    
    public Dimension getMinimumSize ()
    {
    Dimension d= super.getMinimumSize ();
    d.setSize(d.width*0.5,d.height*0.5);
    return d;
    }
    
    public Dimension getMaximumSize ()
    {
    Dimension d= super.getMaximumSize();
    d.setSize(d.width*0.5,d.height*0.5);
    return d;
    }
    
    public Dimension getSize ()
    {
    Dimension d= super.getSize ();
    d.setSize(d.width*0.5,d.height*0.5);
    return d;
    }
    
    }
    
    The thing is that the text is scaled corretly but ScrollPane screws up
    completely and the typing of the text is not accurate
    
    Is the way i am scaling the JTextpane correct?
    
    Any help is greatly appreciated
    
    Thank You
    
    Yours Sincerely
    
    Richard West
     
    freesoft_2000, Oct 11, 2005
    #4
  5. freesoft_2000

    zero Guest

    "freesoft_2000" <> wrote in
    news::

    > Hi everyone,
    >
    > I think you guys have misunderstood my question in that i
    > am
    > not talking about scaling for printing but something of a zoom in and
    > zoom out for the JTextPane
    >
    > Here is a full compilable example where you guys can compile the code
    > and see what i mean first hand
    >


    <snipped>

    this is very specific to Swing GUIs, so I think you may have better luck in
    either comp.lang.java.gui, or sun's own swing forum at
    http://forums.java.sun.com/forum.jspa?forumID=57
     
    zero, Oct 11, 2005
    #5
  6. freesoft_2000

    Roedy Green Guest

    On Tue, 11 Oct 2005 04:39:52 -0400, "freesoft_2000"
    <> wrote or quoted :

    >Graphics2D g2d = (Graphics2D)g;
    > g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    > RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    >
    > g2d.scale(0.5,0.5);
    > AffineTransform old = g2d.getTransform();
    > g2d.setTransform(old);
    > super.paint(g2d);

    this code here should read:

    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    AffineTransform old = g2d.getTransform();
    g2d.scale(0.5,0.5);
    super.paint(g2d);
    g2d.setTransform(old);
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 13, 2005
    #6
  7. freesoft_2000

    Roedy Green Guest

    On Tue, 11 Oct 2005 04:39:52 -0400, "freesoft_2000"
    <> wrote or quoted :

    > public void paint(Graphics g)
    > {
    > Graphics2D g2d = (Graphics2D)g;
    > g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    > RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    >
    > g2d.scale(0.5,0.5);
    > AffineTransform old = g2d.getTransform();
    > g2d.setTransform(old);
    > super.paint(g2d);
    > }


    because you are using Swing, you should be overriding paintComponent,
    not paint.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 13, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?REVXcmlnaHRfQ0E=?=

    Control Scaling???

    =?Utf-8?B?REVXcmlnaHRfQ0E=?=, Apr 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Steven Cheng[MSFT]
    Apr 27, 2005
  2. =?Utf-8?B?REVXcmlnaHRfQ0E=?=

    Control Scaling???

    =?Utf-8?B?REVXcmlnaHRfQ0E=?=, Apr 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    421
    Steven Cheng[MSFT]
    Apr 28, 2005
  3. =?Utf-8?B?REVXcmlnaHRfQ0E=?=

    Page & Control Scaling?

    =?Utf-8?B?REVXcmlnaHRfQ0E=?=, May 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    380
    Steven Cheng[MSFT]
    May 26, 2005
  4. AndrewF
    Replies:
    1
    Views:
    838
    Bruce Barker
    Oct 10, 2005
  5. Scaling PNG image

    , Apr 29, 2004, in forum: Java
    Replies:
    3
    Views:
    724
    Thomas Weidenfeller
    Apr 30, 2004
Loading...

Share This Page