Java by Example - fonts, random numbers and timers

back 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 next

This applet demonstrates the use of True Type Fonts in Java. Basically you have only the choice between TimesRoman, Courier and Helvetica if you want your applet to run on all machines. Fonts are objects which have to be created with new. You can add the BOLD and ITALIC attributes seperately or in combination. The size can be set in the last parameter of the Font constructor.

//Sourcecode

import java.awt.*;
import java.applet.*;

//demonstrates the use of fonts
public class Project6 extends Applet
{
    Font font1 = new Font("Helvetica", Font.PLAIN,  22);
    Font font2 = new Font("TimesRoman", Font.PLAIN,  20);
    Font font3 = new Font("Courier", Font.PLAIN,  18);

    Font font4 = new Font("Helvetica", Font.BOLD,  16);
    Font font5 = new Font("Helvetica", Font.ITALIC,  16);
    Font font6 = new Font("Helvetica", Font.BOLD + Font.ITALIC,  16);

    public void paint(Graphics g)
    {
        g.setFont(font1);
        g.drawString("This is Font 1 (Helvetica)", 30,30);

        g.setFont(font2);
        g.drawString("This is Font 2 (TimesRoman)", 30,80);

        g.setFont(font3);
        g.drawString("This is Font 3 (Courier)", 30,130);

        g.setFont(font4);
        g.drawString("This is Helvetica Bold", 30,180);

        g.setFont(font5);
        g.drawString("This is Helvetica Italic", 30,230);

        g.setFont(font6);
        g.drawString("This is Helvetica Bold and Italic", 30,280);
    }
}

Here you can see how random numbers are generated. You have to call Math.random() (the funcion random() from the Java package Math) which returns a floating point pseudo-random number between 0 and 1. To produce bigger numbers you must multiply the result with an integer. The expression (int) "casts" the floating point return value of the random function to the integer value you need for your variable. Be aware that positions behind the decimal point are cut off when you cast that way, not rounded!

//Sourcecode

import java.awt.*;
import java.applet.*;

//demonstrates the use of random numbers
//this program produces a number between 1 and 6 when you click

public class Project7 extends Applet
{
    int randomNumber;

    public boolean mouseDown(Event evt,int x,int y)
    {
        //here it all happens.. Math.random() returns a floating point number
        //between 0 and 1, so you have to multiply with an integer to produce
        //integer random numbers and finally add 1 to exclude the 0
        randomNumber=(int)(Math.random()*6)+1;
        repaint();

        return true;
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.setFont(new Font("Helvetica",Font.PLAIN,12));
        g.drawString("Click for random number between 1 and 6", 30,20);

        g.setColor(Color.blue);
        g.setFont(new Font("Helvetica",Font.BOLD,180));

        //in the beginning randomNumber is 0, so we suppress it here
        if(randomNumber!=0)
            g.drawString(""+randomNumber, 100,210);
    }
}

This applet demonstrates how to use a thread, in order to generate periodically repeating events (here: produce and display a random number between 1 and 6). You just have to halt the thread for a given period (here: 1000 milliseconds = 1 second). To avoid the flicker you can observe here, we need to use double buffering. This technique is explained in the next chapter.

//Sourcecode

import java.awt.*;
import java.applet.*;

//demonstrates the use of random numbers and the use of timer intervals.
//our class has to implement the "Runnable" interface, if we use threads

public class Project8 extends Applet implements Runnable
{
    Thread runner;
    int randomNumber;

    public void start()
    {
        if (runner == null)
        {
            runner = new Thread (this);
            runner.start();
        }
    }

    public void stop()
    {
        if (runner != null)
        {
            runner.stop();
            runner = null;
        }
    }

    public void run()
    {
        while(true)
        {
            //halt the thread for 1 second here
            try {runner.sleep(1000);}
            catch (Exception e) { }

            randomNumber=(int)(Math.random()*6)+1;
            repaint();
        }
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.setFont(new Font("Helvetica",Font.PLAIN,12));
        g.drawString("A random number between 1 and 6 every second", 15,20);

        //we draw a drop shadow, shifted right and down
        g.setFont(new Font("Helvetica",Font.BOLD,180));
        g.setColor(Color.black);
        g.drawString(""+randomNumber, 105,215);

        g.setColor(Color.blue);
        g.drawString(""+randomNumber, 100,210);
    }
}

back 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 next

0 - setup - getting your tools ready
1 - basic graphics functions
2 - simple methods and basic data types
3 - IF, ELSE and SWITCH: basic control structures
4 - introducing the operators
5 - methods with and without a return value
6 - using methods and basic mouse functions
7 - fonts, random numbers and timers
8 - flicker free graphics, GIF and JPEG display
9 - animation with GIF pictures, sprite animation
10 - loops, advanced color functions
11 - random colors and arrays
12 - digital clocks, HTML page parameters
13 - introducing classes and objects
14 - using the Vector class
15 - using mouseMove and mouseDrag
16 - keyboard commands and playing sound
17 - detecting collisions and intersections
18 - a Bouncing Balls applet
19 - fun with letters and words
20 - rotating lines and polygons
21 - sorting and shuffling


© 2000 by Johannes Wallroth
www.programming.de

watson@programming.de