Código fuente del Applet
/*
Nico_Calendar.java Applet Junio 1.998 Copyright() N. Betancort
Applet que contiene un calendario que puede usarse como Interface para
manipular eventos relativos a fechas
Revision 18-julio-1999
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.text.DateFormat;
public class Nical extends Applet {
int dia = 1, mes = 1, anno = 1996;
int x = 400; int y = 360; //dimensiones
static final Color CFONDO = new Color(255,255,120); //color fondo
static final String diau[] ={"lun.au","mar.au","mie.au",
"jue.au","vie.au","sab.au","dom.au"};
Aplcal pb;
Aplista pl;
public Dimension getPreferredSize() { return new Dimension(400, 360);}
public void init(){
setLayout(null);
setSize(getPreferredSize());
setBackground(CFONDO);
fecha(); //Obtenci¢n fecha del sistema
Panel marco = new Marco(x,y, CFONDO);
marco.setBounds(0,0, x, y);
add(marco);
Panel ptitu = new Panel(); //Panel para cabecera
ptitu.setBounds(20,20,x-40,y/10);
ptitu.setBackground(CFONDO);
Label ltitu = new Label(" Calendario Interactivo "); //titulo
ltitu.setForeground(new Color(10,10,240));
ltitu.setFont(new Font("Serif",Font.BOLD, 18));
ptitu.add("Center",ltitu);
marco.add(ptitu);
pb = new Aplcal(this); //Clase que genera el calendario
marco.add(pb);
pb.disennar(mes,anno); //valores iniciales
pl = new Aplista(pb); //Clase que genera listas
marco.add(pl);
pl.setBounds(20,20+y/10,x-40,y/10);
pb.setBounds(20,20+y/5,x-40,4*y/5-40);
}
public void tocar(int k){ //m‚todo de gesti¢n de sonido
if( pl.getsonido() == true ){ play(getCodeBase(), diau[k]); }
}
//.........................................
public void start(){ repaint(); }
//..........................................
private void fecha(){ //m‚todo obtenci¢n fecha del sistema
StringTokenizer stfecha;
Integer Ifecha[] = new Integer[3];
String parte[] = new String[3];
try {
Date fechactual = new Date(); //Obtiene la fecha actual
String sfecha = DateFormat.getDateInstance(3,
Locale.US).format(fechactual);
stfecha = new StringTokenizer(sfecha, "/");
for (int i=0; i<2;i++){
parte[i] = stfecha.nextToken();
Ifecha[i] =new Integer(parte[i]);
}
this.mes = Ifecha[0].intValue();
this.dia = Ifecha[1].intValue();
sfecha = DateFormat.getDateInstance(1,
Locale.US).format(fechactual);
stfecha = new StringTokenizer(sfecha, " ");
for (int i=0; i<3;i++){
parte[i] = stfecha.nextToken();
}
Ifecha[2] =new Integer(parte[2]);
this.anno = Ifecha[2].intValue();
}
catch(NumberFormatException e2) {
this.mes = 1;
this.dia = 1;
this.anno = 1995;
}
}
//............................................
public static void main(String args[]) {
Nical Nicalen = new Nical();
Nicalen.init();
Nicalen.start();
}
//.............................................
} //Fin de clase Nical
//------------------------------------------------------------------------
class Aplista extends Panel implements ItemListener{
//Declaraciones
Choice listames = new Choice();
Choice listanno = new Choice();
Checkbox chb1 = new Checkbox("Activar sonido", false);
int inimes ;
int diassmes;
int mes = 6, anno = 1997;
Aplcal pb;
final static Color FONDO = new Color(255,255,120);
final static String nm[] = {"Meses","Enero", "Febrero","Marzo","Abril",
"Mayo","Junio","Julio","Agosto","Septiembre",
"Octubre","Noviembre","Diciembre"};
public Aplista(Aplcal pb){ //Constructor
this.pb = pb;
this.anno = pb.anno;
this.mes = pb.mes;
String slbot = null;
setLayout( new GridLayout(1,3,10,5));
setBackground(FONDO);
chb1.setBackground(FONDO);
add(chb1); //A¤adir bot¢n de selecci¢n;
//Colocar Listas
for(int i=1; i<13; i++){ listames.addItem(Devmes(i)); }
listames.addItemListener(this);
listames.select(mes-1);
add(listames);
for(int i= anno-5; i0 ){ return nm[mes] ;}
else { return "Error";}
}
//........................................
public boolean getsonido(){ return(chb1.getState());}
//........................................
public void itemStateChanged(ItemEvent ev) { //Gestor cambios mes o a¤o
boolean esanno = true;
String iname = (ev.getItem()).toString();
for(int i = 1; i<13; i++){
if(iname == Devmes(i)){ mes = i; i=14; esanno=false; }
}
if (esanno==true) {anno = (Integer.valueOf(iname)).intValue();}
pb.setVisible(false);
pb.disennar(mes, anno);
}
//........................................
} //Fin clase Aplista
//----------------------------------------------------------------------
class Aplcal extends Panel implements ActionListener{
//Declaraciones
final static Color COLOR_BK = new Color (192,192,192);
final static Color COLOR_SD = Color.red;
final static Color COLOR_FT = new Color(0,0,220);
final static Color COLOR_FJ = new Color(20,255,0);
final static Color COLOR_FD = new Color(255,255,120);
final static Color COLOR_ET = new Color(0,255,255);
final static Font TMR = new Font("Serif",Font.BOLD, 12);
final static int diasmes[] = {29,31,28,31,30,31,30,31,31,30,31,30,31};
final static String DIAS_SEM[] = {" Lunes", "Martes", "Miercol.",
" Jueves"," Viernes"," Sabado","Domingo"};
Nical padre;
Button bot[] = new Button[42];
byte botstate[]= new byte[42];
String slbot = null;
int mes, anno ;
int numlab, inimes, diassmes;
//.............................
public int Devdiasmes(int anno,int mes){
if (anno%4 == 0){ diasmes[2]=29; } //Comprobaci¢n bisiesto
else {diasmes[2]=28;}
return diasmes[mes];
}
//......................................
public int Devdiaini(int anno, int mes){ //devuelve el dia de inicio
int inter = 365*(anno - 1980) + (anno - 1981)/4;
inter = inter + 2 ; //incrementar adecuadamente
if(mes>1) { for (int i=1;i< mes; i++){inter = inter + diasmes[i];} }
int salida = inter % 7 ;
return salida; // salida = inter.mod(7);
}
//.........................................
public Aplcal(Nical padre){ //constructor sin iniciar
super();
this.padre = padre;
setLayout(new GridLayout(7,7,5,5));
setBackground(COLOR_FD);
setSize(200,200);
for (int i=0; i< 40 ;i++) {botstate[i]=0;} //Iniciar matriz de estado
for (int i=0; i<7 ; i++){ //Encabezado L M X J V S D
Label ltop = new Label(DIAS_SEM[i]);
ltop.setFont(TMR);
ltop.setBackground(COLOR_ET);
add(ltop);
}
for (int i=0; i< 40 ;i++) { //Generar matriz de botones
bot[i] = new Button();
bot[i].addActionListener(this);
add(bot[i]);
}
}
//............................................
public void disennar(int tmes, int tanno){
this.mes = tmes ;
this.anno = tanno;
this.diassmes = Devdiasmes(anno, mes);
this.inimes = Devdiaini(anno, mes);
for (int i=0; i< 40 ;i++) {
botstate[i]=0; //Puesta a cero de la matriz botstate
bot[i].setEnabled(false); //botones inactivos
bot[i].setVisible(false);
}
for(int i=inimes; i< diassmes+inimes; i++){
bot[i].setBackground(COLOR_BK);
bot[i].setEnabled(true); //activar botones de fecha
bot[i].setVisible(true);
bot[i].setLabel(slbot.valueOf(i+1-inimes));
}
// Destacar los dias festivos bootstate = -1
for (int i= 5; i<40; i=i+7){ //sabados
bot[i].setBackground(Color.red);
bot[i].setEnabled(false);
botstate[i]= -1;
}
for (int i= 6; i<40; i=i+7){ //domingos
bot[i].setBackground(Color.red);
bot[i].setEnabled(false);
botstate[i]= -1;
}
setVisible(true);
setBackground(COLOR_FD); //MODIFIC
}
//.......................................................
public void actionPerformed(ActionEvent ev) {
String accion = ev.getActionCommand();
Integer INTE = Integer.valueOf(accion);
int numero =INTE.intValue();
numlab = numero+inimes-1;
byte estado = botstate[numlab];
//Este m‚todo puede modificarse para incorporar cualquier
//otra funcionalidad asociada a la pilsaci¢n de los botones
switch(estado) {
case 0:
bot[numlab].setBackground(COLOR_FT);
botstate[numlab]= 1;
padre.tocar( numlab % 7); //prueba de sonido
break;
case 1:
bot[numlab].setBackground(COLOR_FJ);
botstate[numlab]=2;
break;
case 2:
bot[numlab].setBackground(COLOR_BK);
botstate[numlab]=0;
break;
}
}
} //Fin Clase Aplcal
//-------------------------------------------------------------------------
class Marco extends Panel {
int an = 100; int al =100;
static Color cuno = new Color(210,210,210); //borde uno
static Color cdos = new Color(100,100,100); //borde dos
static Color ctre = new Color(160,160,160); //borde tres
static Color ccua = new Color(220,100,10); //fondo marco
public Marco(int ancho, int alto, Color fondo) { //Constructor
super();
this.an = ancho ;
this.al = alto ;
this.ccua = fondo;
setLayout(null);
Dimension dim1= new Dimension(ancho, alto);
//setSize(dim1);
setBackground(cuno); //cuadro grande
repaint();
}
public void paint(Graphics g){
g.setColor(ctre);
g.fillRect(5,5, an-9, al-9); //borde frontal
g.setColor(cuno);
g.fillRect(12,12, an-23, al-23); //cuadro borde interior
g.setColor(cdos);
for(int i=0; i<5; i++){ //rebordes de 5 pixels de ancho
g.drawLine(an-i, i, an-i, al-i); //vert. dch.
g.drawLine(i, al-i, an-i, al-i); //hor. baja
g.drawLine(12+i, 12+i, 12+i, al-12-i); //vert. izq.
g.drawLine(12+i, 12+i, an-12-i, 12+i); //hor. alta
}
g.setColor(ccua);
g.fillRect(17,17, an-33, al-33); //fondo interior
} //fin m‚todo paint
} //fin de clase
//------------------------------------------------------------------------
//Fin c¢digo applet
Volver a la página principal