Problem to change a font size.

Discussion in 'Java' started by lov2code, May 17, 2004.

  1. lov2code

    lov2code Guest

    Hi,

    I tried to change font size by changing an existing font object, it
    did not work. Anybody can help with this? Thanks.

    =========================================================
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JApplet;
    import javax.swing.*;
    import java.util.Random;

    public class test extends JApplet implements ActionListener{
    private MyFont cacheFont;
    private Random rand;

    public void init()
    {
    Container container = getContentPane();
    container.setLayout( new BorderLayout() );

    JButton button1;
    button1 = new JButton( "Random Font");
    button1.addActionListener(this);
    container.add(button1, BorderLayout.SOUTH );

    cacheFont = new MyFont();
    rand = new Random();
    }

    public void actionPerformed( ActionEvent event )
    {
    cacheFont.setSize(rand.nextInt(30) + 20);
    Graphics g = getGraphics();
    update(g);
    g.setFont( cacheFont );
    //print out changed size, but still draw size 12.
    g.drawString( "Font size = " + cacheFont.getSize(), 15,15 );
    }

    private class MyFont extends Font
    {
    public MyFont()
    {
    super("Serif", Font.BOLD, 12);
    }

    public void setSize(int size)
    {
    this.size = size;
    }
    }

    }
    lov2code, May 17, 2004
    #1
    1. Advertising

  2. lov2code

    Fahd Shariff Guest

    To tell you the truth, i dont know why your method isn't working. I
    can, however, propose some solutions:

    1. Create a new Font every time:
    g.setFont(new Font("Serif", Font.BOLD, rand.nextInt(30) + 20)) ;

    2. Use the deriveFont() method:
    g.setFont(cacheFont.deriveFont(new Integer(rand.nextInt(30) +
    20).floatValue())) ;

    --
    Fahd Shariff
    http://www.fahdshariff.cjb.net
    "Let the code do the talking..."
    Fahd Shariff, May 17, 2004
    #2
    1. Advertising

  3. lov2code

    Fahd Shariff Guest

    To tell you the truth, i dont know why your method isn't working. I
    can, however, propose some solutions:

    1. Create a new Font every time:
    g.setFont(new Font("Serif", Font.BOLD, rand.nextInt(30) + 20)) ;

    2. Use the deriveFont() method:
    g.setFont(cacheFont.deriveFont(new Integer(rand.nextInt(30) +
    20).floatValue())) ;

    --
    Fahd Shariff
    http://www.fahdshariff.cjb.net
    "Let the code do the talking..."
    Fahd Shariff, May 17, 2004
    #3
  4. lov2code

    lov2code Guest

    (Fahd Shariff) wrote in message news:<>...
    > To tell you the truth, i dont know why your method isn't working. I
    > can, however, propose some solutions:
    >
    > 1. Create a new Font every time:
    > g.setFont(new Font("Serif", Font.BOLD, rand.nextInt(30) + 20)) ;
    >
    > 2. Use the deriveFont() method:
    > g.setFont(cacheFont.deriveFont(new Integer(rand.nextInt(30) +
    > 20).floatValue())) ;

    Thank you, but I would like not to create an new object.
    lov2code, May 17, 2004
    #4
    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. Andreas Klemt
    Replies:
    6
    Views:
    10,989
    Peter Theill
    Nov 28, 2004
  2. dave richards
    Replies:
    2
    Views:
    599
    Paul Furman
    Feb 19, 2004
  3. Wongod
    Replies:
    2
    Views:
    691
  4. wial
    Replies:
    2
    Views:
    795
  5. mttc
    Replies:
    2
    Views:
    2,361
    Roedy Green
    Jul 3, 2009
Loading...

Share This Page