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.*;
import java.awt.*;
import java.applet.*;
public class ClockDateTime // todas inhieren en la clase Java
extends Applet
implements Runnable
{
Thread Timer = null;
extends Applet
implements Runnable
{
Thread Timer = null;
Font oFont = new Font( "TimesRoman", Font.PLAIN, 14 );
Date dat = null;
Date dFecha = new Date();
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;
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;
{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
}
y = 4 * nRadio + 30;
resize( x, y ); // Set clock window size
}
public void start()
{if( Timer == null )
{Timer = new Thread( this );
Timer.start();
}
}
{if( Timer == null )
{Timer = new Thread( this );
Timer.start();
}
}
public void stop()
{Timer = null;
}
{Timer = null;
}
public void run()
{while ( Timer != null )
{try { Thread.sleep( 100 ); } catch (InterruptedException e){} // 100 milisegundos
repaint();
}
Timer = null;
}
{while ( Timer != null )
{try { Thread.sleep( 100 ); } catch (InterruptedException e){} // 100 milisegundos
repaint();
}
Timer = null;
}
public void update( Graphics g )
{paint( 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();
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 );
// 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 );
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 );
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 );
}
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.darkGray );
g.drawString( cHoy, nOffset, 2 * nRadio + 23 );
g.setColor( Color.green );
g.drawLine( xCenter, yCenter, xs, ys );
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.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 );
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;
}
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;
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 );
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 );
}
}
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 );
}
}
// 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.