import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Fonts extends Applet implements ActionListener,KeyListener
{
                String text="";
                Button boldbutton,italicbutton,largebutton;
                boolean bold=false;
                boolean italic=false;
                boolean large=false;
                public void init()
                {
                                boldbutton=new Button("Bold Font");
                                italicbutton=new Button("Italic Font");
                                largebutton=new Button("Large Font");
                                boldbutton.addActionListener(this);
                                italicbutton.addActionListener(this);
                                largebutton.addActionListener(this);
                                add(boldbutton);
                                add(italicbutton);
                                add(largebutton);
                                addKeyListener(this);
                                requestFocus();
                }
                public void actionPerformed(ActionEvent event)
                {
                                if(event.getSource()==boldbutton)
                                                bold=!bold;
                                if(event.getSource()==italicbutton)
                                                italic=!italic;
                                if(event.getSource()==largebutton)
                                                large=!large;
                                requestFocus();
                                repaint();
                }
                public void paint(Graphics g)
                {
                                String fontname="Courier";
                                int type=Font.PLAIN;
                                int size=36;
                                Font font;
                                FontMetrics fm;
                                if(bold)
                                                type=type | Font.BOLD;
                                if(italic)
                                                type=type | Font.ITALIC;
                                if(large)
                                                size=72;
                                font=new Font(fontname,type,size);
                                g.setFont(font);
                                fm=getFontMetrics(font);
                                int xloc=(getSize().width-fm.stringWidth(text))/2;
                                int yloc=(getSize().height+fm.getHeight())/2;
                                g.drawString(text,xloc,yloc);
                }
                public void keyTyped(KeyEvent e)
                {
                                text=text+e.getKeyChar();
                                repaint();
                }
                public void keyPressed(KeyEvent e)         {}
                public void keyReleased(KeyEvent e)      {}
}

 

<HTML>
                <APPLET
                                CODE=Fonts.class
                                WIDTH=600
                                HEIGHT=200>
                </APPLET>
</HTML>

JAVA 40.PNG

Advertisements