Basic Calendar


Java Source:

/*
	Copyright (C) 2000  Erik Bonder
	e-mail: ropez@mail.lv

	Project: calendar
	Version: 0.1
	Filename: calendar.java
	Date&Time: 22.09.2000 19:33
*/

/*
	TODO

	23.09.2000 22:13	Configurable date format (url)

*/

/*
	Parameters

*	color1, ...	Colors
*	url		Url base
*	date		Current date, format: MM.YYYY
*	datelist	Existing dates: 18.08.2000-24.08.2000&10.09.2000&20.09.2000
*	dateformat	Format of the date: DD.MM.YYYY
*	monthnames	Names of monthes: Ubauba&AAAA&...
*	daysofweek	Names of day of week (seven chars): PDFGHJJ

*/

/*
**	Includes
*/
import	java.awt.*;
import	java.applet.*;
import	java.io.*;
import	java.util.*;
import	java.net.*;

/*
**	Classes
*/
/*---------------------------*/
public class	calendar extends Applet
{
/*
**	Variables
*/
	Color[]		colormap = new Color[7];
	Cursor[]	cursors = new Cursor[2];
	Rectangle[]	controls = new Rectangle[5];
	int		year, month;
	int		firstday, daycnt = 30;
	String[]	monthnames = new String[12];
	String[]	daysofweek = new String[7];
	boolean[]	urlexist = new boolean[31];
	String		urlbase;
	int		datelistsize;
	int[]		datelist1, datelist2;

	Image		gimg;
	Graphics	g;
	Font		fnt = new Font( "Courier New", Font.PLAIN, 12 );
	FontMetrics	fntm = getFontMetrics( fnt );

/*
**	Functions
*/
/*---------------------------*/
	String genurl( int day )
	{
		return urlbase + ( day < 10 ? "0" : "" ) + String.valueOf( day ) + "." +
			( month < 9 ? "0" : "" ) + String.valueOf( month + 1 ) + "." +
			String.valueOf( year );
	}
/*---------------------------*/
	Color str2col( String hex )
	{
		return new Color( ( Integer.decode( "0x" + hex.substring( 0, 2 ) ) ).intValue(),
			( Integer.decode( "0x" + hex.substring( 2, 4 ) ) ).intValue(),
			( Integer.decode( "0x" + hex.substring( 4, 6 ) ) ).intValue() );
	}
/*---------------------------*/
	int daysellapsed( int d, int m, int y )
	{
		int		a, b;
		int[]		c = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

		a = y - 1;
		b = a * 365 + a / 4 - a / 100 + a / 400 + c[m];
		if ( ( ( y % 4 ) == 0 && ( ( y % 100 ) != 0 || ( y % 400 ) == 0 ) ) && m > 1 ) b++;
		return b + d;
	}
/*---------------------------*/
	void parsedatelist( String str )
	{
		int		i, a, b, i1, i2;

		for ( b = a = 0; ( b = str.indexOf( '&', b + 1 ) + 1 ) > 0; a++ );
		a++;
		datelistsize = a;
		datelist1 = new int[a];
		datelist2 = new int[a];
		for ( i1 = i = 0; i < a; i++ )
		{
			datelist1[i] = daysellapsed( Integer.parseInt( str.substring( i1, i1 + 2 ) ),
				Integer.parseInt( str.substring( i1 + 3, i1 + 5 ) ) - 1,
				Integer.parseInt( str.substring( i1 + 6, i1 + 10 ) ) );
			b = i1;
			i1 = str.indexOf( '-', b ) + 1;
			i2 = str.indexOf( '&', b ) + 1;
			if ( i1 == 0 ) i1 = 99999;
			if ( i2 == 0 ) i2 = 99999;
			if ( i1 < i2 )
			{
				datelist2[i] = daysellapsed( Integer.parseInt( str.substring( i1, i1 + 2 ) ),
					Integer.parseInt( str.substring( i1 + 3, i1 + 5 ) ) - 1,
					Integer.parseInt( str.substring( i1 + 6, i1 + 10 ) ) );
			} else datelist2[i] = datelist1[i];
			i1 = i2;
		}
	}
/*---------------------------*/
	void updatemonth()
	{
		int		i, a, b;
		int c[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		c[1] = ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) ) ? 29 : 28;

		firstday = ( daysellapsed( 1, month, year ) + 6 ) % 7;
		daycnt = c[month];

		b = daysellapsed( 1, month, year );
		for ( i = 0; i < daycnt; i++, b++ )
		{
			for ( a = 0; a < datelistsize; a++ ) if ( b >= datelist1[a] && b <= datelist2[a] ) break;
			urlexist[i] = ( ( a == datelistsize ) ? false : true );
		}
	}
/*---------------------------*/
	public void init()
	{
		int		i, a, b;
		String		str;

		str = getParameter( "monthnames" );
		for ( a = i = 0; i < 11; i++ )
		{
			b = str.indexOf( '&', a );
			monthnames[i] = str.substring( a, b );
			a = b + 1;
		}
		monthnames[11] = str.substring( a );

		str = getParameter( "daysofweek" );
		for ( i = 0; i < 7; i++ ) daysofweek[i] = String.valueOf( str.charAt( i ) );

		str = getParameter( "date" );
		if ( str == null )
		{
			Date		d = new Date();
			month = d.getMonth();
			year = d.getYear() + 1900;
		} else
		{
			month = Integer.parseInt( str.substring( 0, 2 ) ) - 1;
			year = Integer.parseInt( str.substring( 3, 7 ) );
		}

		urlbase = getParameter( "url" );
		parsedatelist( getParameter( "datelist" ) );

		updatemonth();

		colormap[0] = str2col( getParameter( "color0" ) );
		colormap[1] = str2col( getParameter( "color1" ) );
		colormap[2] = str2col( getParameter( "color2" ) );
		colormap[3] = str2col( getParameter( "color3" ) );
		colormap[4] = str2col( getParameter( "color4" ) );
		colormap[5] = str2col( getParameter( "color5" ) );
		colormap[6] = str2col( getParameter( "color6" ) );

		cursors[0] = new Cursor( Frame.DEFAULT_CURSOR );
		cursors[1] = new Cursor( Frame.HAND_CURSOR );

		controls[0] = new Rectangle( 2, 0, fntm.stringWidth( "<<" ), 16 );
		controls[1] = new Rectangle( 108 - fntm.stringWidth( ">>" ), 0, fntm.stringWidth( ">>" ), 16 );
		controls[2] = new Rectangle( 116, 0, fntm.stringWidth( "<<" ), 16 );
		controls[3] = new Rectangle( 180 - fntm.stringWidth( ">>" ), 0, fntm.stringWidth( ">>" ), 16 );
		controls[4] = new Rectangle( 0, 32, 182, 96 );

		gimg = createImage( 182, 128 );
		g = gimg.getGraphics();
	}
/*---------------------------*/
	public boolean mouseDown( Event evt, int x, int y )
	{
		int		i, a;

		for ( i = 0; i < 5; i++ ) if ( controls[i].inside( x, y ) ) break;
		switch ( i)
		{
			case 0:
				month = ( month + 11 ) % 12;
				break;
			case 1:
				month = ( month + 1 ) % 12;
				break;
			case 2:
				year--;
				break;
			case 3:
				year++;
				break;
			case 4:
				a = ( ( y - 32 ) / 16 ) * 7 + x / 26 - firstday;
				if ( a >= 0 && a < daycnt )
				if ( urlexist[a] )
				{
					URL		url;
					try { url = new URL( getDocumentBase(), genurl( a + 1 ) ); }
					catch( MalformedURLException _ex ) { break; }
					getAppletContext().showDocument( url );
				}
				break;
			default:
				return true;
		}
		if ( i < 4 ) updatemonth();
		repaint();
		return true;
	}
/*---------------------------*/
	public boolean mouseMove( Event evt, int x, int y )
	{
		int		i, a = 0;
		boolean		c = false;

		for ( i = 0; i < 5; i++ ) if ( controls[i].inside( x, y ) ) break;
		if ( i < 4 ) c = true; else
		if ( i == 4 )
		{
			a = ( ( y - 32 ) / 16 ) * 7 + x / 26 - firstday;
			if ( a >= 0 && a < daycnt ) c = urlexist[a];
		}
		if ( c )
		{
			if ( i == 4 ) showStatus( genurl( a + 1 ) );
			setCursor( cursors[1] );
		} else
		{
			showStatus( "" );
			setCursor( cursors[0] );
		}

		return true;
	}
/*---------------------------*/
	public void update( Graphics gc )
	{
		paint( gc );
	}
/*---------------------------*/
	public void paint( Graphics gc )
	{
		int		i, x, y;
		String		str;

		g.setFont( fnt );

		g.setColor( colormap[3] );
		g.fillRect( 0, 0, 182, 16 );

		for ( y = 0; y < 7; y++ )
			for ( x = 0; x < 7; x++)
			{
				g.setColor( colormap[( ( y & 1 ) << 1 ) + ( x & 1 ) + 1] );
				g.fillRect( x * 26, y * 16 + 16, 26, 16 );
			}

		g.setColor( colormap[0] );

		g.drawString( "<<", controls[0].x, 13 );
		g.drawString( ">>", controls[1].x, 13 );
		g.drawString( "<<", controls[2].x, 13 );
		g.drawString( ">>", controls[3].x, 13 );
		str = monthnames[month];
		g.drawString( str, 55 - fntm.stringWidth( str ) / 2, 13 );
		str = String.valueOf( year );
		g.drawString( str, 150 - fntm.stringWidth( str ) / 2, 13 );

		for ( i = 0; i < 7; i++ ) g.drawString( daysofweek[i], i * 26 + 13 - fntm.stringWidth( daysofweek[i] ) / 2, 29 );

		for ( i = 0; i < daycnt; i++ )
		{
			str = String.valueOf( i + 1 );
			g.setColor( colormap[urlexist[i] ? 6 : 5] );
			g.drawString( str, ( ( i + firstday ) % 7 ) * 26 + 20 - fntm.stringWidth( str ), ( ( i + firstday ) / 7 ) * 16 + 45 );
		}

		gc.drawImage( gimg, 0, 0, this );
	}
}
/*---------------------------*/

Back to Basic Calendar

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.