Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem to change a font size.

Reply
Thread Tools

Problem to change a font size.

 
 
lov2code
Guest
Posts: n/a
 
      05-17-2004
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;
}
}

}
 
Reply With Quote
 
 
 
 
Fahd Shariff
Guest
Posts: n/a
 
      05-17-2004
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..."
 
Reply With Quote
 
 
 
 
Fahd Shariff
Guest
Posts: n/a
 
      05-17-2004
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..."
 
Reply With Quote
 
lov2code
Guest
Posts: n/a
 
      05-17-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Fahd Shariff) wrote in message news:<(E-Mail Removed). com>...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swing Font, it's Java Font? ot native? how install new font? mttc Java 2 07-03-2009 07:29 PM
How to get the text in html tag.like<div...><font...>Text</font></ =?Utf-8?B?Tmlja3k=?= ASP .Net 2 02-20-2005 03:03 PM
Font class, external font files question Karl Hungus ASP .Net 1 02-04-2004 04:26 PM
JavaRE 1.4.2 font alias in /lib/font.properties (Linux) Nick Spinel Java 0 07-25-2003 08:28 AM
physical font specified but true type font file not available Michael Glavitsch Java 1 07-15-2003 07:09 PM



Advertisments