L
lov2code
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;
}
}
}
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;
}
}
}