Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

 

Font Class

 

 

Description

Exercises

 

Description

Output

Code

 

import java.awt.*;

import javax.swing.*;

public class Fonts extends JFrame                          //JFrame is a Container    "window holder" for components

{

   // set title bar and dimensions for window

   public Fonts()

   {

      super( "Using fonts" );

 

      setSize( 420, 125 );

      setVisible( true );

   } 

   // display Strings in different fonts and colors

   public void paint( Graphics g )                //paint is a method that requires a parameter of type Graphics - a class

   {

      // call superclass's paint method  

      super.paint( g );

      g.setFont( new Font( "Serif", Font.BOLD, 12 ) );      //Font class, type, size

      g.drawString( "Serif 12 point bold.", 20, 50 );            //add some identifying text

 

      g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) );

      g.drawString( "Monospaced 24 point italic.", 20, 70 );

 

      g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) );

      g.drawString( "SansSerif 14 point plain.", 20, 90 );

 

      g.setColor( Color.RED );

      g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) );

      g.drawString( g.getFont().getName() + " " + g.getFont().getSize() + " point bold italic.", 20, 110 );

 

   }

 

   public static void main( String args[] )

   {

      Fonts application = new Fonts();                                                                    //creating an instance of the Fonts class

      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );        //close the window when finished

   }

 

}  

 

 

Exercises

 

 

Exercise 1

 

Part a

 

 

 

Same as above with the following changes

 

   First line Arial, 14 point bold Italic, Color Red

   Second Line: Monospaced, 14  point non Italic, color blue

   Third Line: Serif, 14 point bold, color green

   Fourth line: space (skip a line)

   Fifth Line, SanSerif, 12 point plain, color black

 

Part b

 

   Modify the starburst program so that the "explosion" occurs in the 1st and 3rd quadrants.

   I am defining quadrants clockwise.

     ♦  Divide a square into 4 equal parts

     ♦  The part in the upper right hand corner is quadrant 1 and the quadrant in the bottom left hand corner is quadrant 3.