Entrar
¿Nuevo usuario? Inscribirme
DesarrolloJava · Desarrollo de Software en Java/J++/J#
? ¿Ya estás suscrito? Entra a Yahoo!

Consejos

¿Sabías que...?
Podés añadir enlaces a sitios relacionados de tu grupo.

Mensajes

  Mensajes Ayuda
Avanzado
Consulta sobre un applet reloj   Lista de mensajes  
Responder | Reenviar Mensaje #1595 de 25908 |
    Perdón por ser un poco pesado. Esta vez va de applets.
 
    Cómo modificar este código fuente para pintar de otro color distinto al del fondo el círculo del reloj.-
---
 
import java.util.*;
import java.awt.*;
import java.applet.*;
 
public class ClockDateTime // todas inhieren en la clase Java
extends Applet
implements Runnable
{
 Thread Timer = null;
 
 Font oFont = new Font( "TimesRoman", Font.PLAIN, 14 );
 
 Date dat = null;
 Date dFecha = new Date();
 
 String cUltimaFecha = dFecha.toLocaleString();
 
 Color bgColor;
 
 int nLastxs=0, nLastys=0,
     nLastxm=0, nLastym=0,
     nLastxh=0, nLastyh=0;
 int nOffset = 5, nRadio = 50;
 int xCenter = nRadio + (int)(3.5 * nOffset), yCenter = nRadio + nOffset;
 
public void init() // los parámetros tienen que leerse en el método  init()
{String cBgColor = getParameter( "bgColor" );
 if( cBgColor != null)
 {int nRGB = Integer.parseInt( cBgColor, 16 ); // hexadecimal es base 16
  bgColor = new Color( nRGB );
 }
 else
  bgColor = Color.white;
 
 int x = 4 * nRadio + 10,
     y = 4 * nRadio + 30;
 resize( x, y ); // Set clock window size
}
 
public void start()
{if( Timer == null )
 {Timer = new Thread( this );
  Timer.start();
 }
}
 
public void stop()
{Timer = null;
}
 
public void run()
{while ( Timer != null )
 {try { Thread.sleep( 100 ); } catch (InterruptedException e){} // 100 milisegundos
  repaint();
 }
 Timer = null;
}
 
public void update( Graphics g )
{paint( g );
}
 
// Paint is the main part of the program
public void paint( Graphics g )
{int xh, yh,
     xm, ym,
     xs, ys,
     s, m, h;
 float n;
 dat = new Date();
 s = dat.getSeconds();
 m = dat.getMinutes();
 h = dat.getHours();
 String cHoy = dat.toLocaleString();
 
// a = s * pi/2 - pi/2 // to switch 0,0 from 3:00 to 12:00
// x = r(cos a) + xCenter, y = r(sin a) + yCenter
 n = s * 3.14f/(nRadio-20) - 3.14f/2;
 xs = (int)( Math.cos( n ) * (nRadio-5 ) + xCenter );
 ys = (int)( Math.sin( n ) * (nRadio-5 ) + yCenter );
 n = m * 3.14f/(nRadio-20) - 3.14f/2;
 xm = (int)( Math.cos( n ) * (nRadio-10) + xCenter );
 ym = (int)( Math.sin( n ) * (nRadio-10) + yCenter );
 n = ( h * (nRadio-20) + m/2 ) * 3.14f/180 - 3.14f/2;
 xh = (int)( Math.cos( n ) * (nRadio-20) + xCenter );
 yh = (int)( Math.sin( n ) * (nRadio-20) + yCenter );
 
// Draw the circle and numbers
 
 g.setFont( oFont );
 
 g.setColor( Color.blue );
 
 setBackground( bgColor ); // Añadido por JESE
 circle( xCenter, yCenter, nRadio, g );
 
 g.setColor( Color.darkGray );
 g.drawString( "12", xCenter - nOffset - 2         , yCenter - nRadio + 2 * nOffset + 3 );
 g.drawString( "9" , xCenter - nRadio + nOffset    , yCenter + 3                        );
 g.drawString( "3" , xCenter + nRadio - 2 * nOffset, yCenter + 3                        );
 g.drawString( "6" , xCenter - 3                   , yCenter + nRadio - nOffset         );
 
// Erase if necessary
 g.setColor( getBackground() );
 if( xs != nLastxs || ys != nLastys )
 {g.drawLine( xCenter, yCenter, nLastxs, nLastys );
  g.drawString( cUltimaFecha, nOffset, 2 * nRadio + 23 );
 }
 if( xm != nLastxm || ym != nLastym )
 {g.drawLine( xCenter    , yCenter - 1, nLastxm, nLastym );
  g.drawLine( xCenter - 1, yCenter    , nLastxm, nLastym );
 }
 if( xh != nLastxh || yh != nLastyh )
 {g.drawLine( xCenter    , yCenter - 1, nLastxh, nLastyh );
  g.drawLine( xCenter - 1, yCenter    , nLastxh, nLastyh );
 }
 
// Redraw
 g.setColor( Color.darkGray );
 g.drawString( cHoy, nOffset, 2 * nRadio + 23 );
 
 g.setColor( Color.green );
 g.drawLine( xCenter, yCenter, xs, ys );
 
 g.setColor( Color.cyan );
 g.drawLine( xCenter    , yCenter - 1, xm, ym );
 g.drawLine( xCenter - 1, yCenter    , xm, ym );
 
 g.setColor( Color.red );
 g.drawLine( xCenter    , yCenter - 1, xh, yh );
 g.drawLine( xCenter - 1, yCenter    , xh, yh );
 
 nLastxs = xs; nLastys = ys;
 nLastxm = xm; nLastym = ym;
 nLastxh = xh; nLastyh = yh;
 cUltimaFecha = cHoy;
 dat = null;
}
 
// Circle is just Bresenham's algorithm for a scan converted circle
public void circle( int x0, int y0, int r, Graphics g )
{int x, y;
 float d;
 
 x = 0;
 y = r;
 plot8points( x0, y0, x, y, g );
 
 d = 5/4 - r;
 while ( y > x )
 {if( d < 0 )
  {d = d + 2*x + 3;
   x++;
  }
  else
  {d = d + 2 * ( x - y ) + 5;
   x++;
   y--;
  }
  plot8points( x0, y0, x, y, g );
 }
}
 
// Plot points allows calculation to only cover 45 degrees of the circle,
// and then mirror
public void plot8points( int x0, int y0, int x, int y, Graphics g ) // pinta 8 pixels
{g.drawLine( x0+x, y0+y, x0+x, y0+y );
 g.drawLine( x0+y, y0+x, x0+y, y0+x );
 g.drawLine( x0+y, y0-x, x0+y, y0-x );
 g.drawLine( x0+x, y0-y, x0+x, y0-y );
 g.drawLine( x0-x, y0-y, x0-x, y0-y );
 g.drawLine( x0-y, y0-x, x0-y, y0-x );
 g.drawLine( x0-y, y0+x, x0-y, y0+x );
 g.drawLine( x0-x, y0+y, x0-x, y0+y );
}
}
---
 
    Gracias.
---
José Enrique Serrano Expósito jeseadsl@...
Grupo Olivares 2000  http://www.olivares2000.org
---


Mar, 1 de Abr, 2003 7:56 pm

jesedog
Sin conexión Sin conexión
Enviar correo Enviar correo

Reenviar Mensaje #1595 de 25908 |
Desplegar mensajes Autor Ordenar por fecha

Perdón por ser un poco pesado. Esta vez va de applets. Cómo modificar este código fuente para pintar de otro color distinto al del fondo el círculo del...
JESE
jesedog
Sin conexión Enviar correo
1 de Abr, 2003
7:56 pm

Hola JESE, Para cambiar el fondo debes cambiar el parametro del applet ejemplo : codigo para llamar al applet desde un html <APPLET CODE="ClockDateTime"...
Paulina Rodas
rodas_paulina78
Sin conexión Enviar correo
1 de Abr, 2003
8:48 pm

Perdon la intromisión, tienen el applet de un reloj? Me lo pueden pasar o decirme donde esta montado para bajarlo?, gracias! ... === message truncated === ...
L.I. Gerardo Angeles ...
gerardo1971
Sin conexión Enviar correo
1 de Abr, 2003
9:53 pm
Avanzado

Copyright © 2009 Yahoo! de Argentina S.R.L. Todos los derechos reservados.
Política de privacidad - Condiciones del Servicio - Reglas de la comunidad de Yahoo! - Ayuda