aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Clock face Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Clock face" Watch "Clock face" New topic
Author

Clock face

Mark Phylus
Greenhorn

Joined: Mar 28, 2004
Posts: 18
I would like to put a circular border around my clock and then add the 12,3,6, and 9 to the clocks face. Can anyone show me the implemention?
Thanks a lot.


import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class Clock1 extends JPanel implements Runnable
{
int width = 400;
int height = 400;
Thread t = null;
boolean threadSuspended;
int hours=0, minutes=0, seconds=0;
String timeString = "";
Clock1()
{
setPreferredSize(new Dimension (width, height));
//height = getSize().height;
setBackground( Color.black );
} public void start()
{ if ( t == null )
{ t = new Thread( this );
t.setPriority( Thread.MIN_PRIORITY );
threadSuspended = false;
t.start();
}
else
{
if ( threadSuspended )
{ threadSuspended = false;
synchronized( this )
{ notify();
}

}
}
}
public void stop()
{
threadSuspended = true;
} public void run()
{
try
{
while (true)
{
// Here's where the thread does some work
Calendar cal = Calendar.getInstance();
hours = cal.get( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = cal.get( Calendar.MINUTE );
seconds = cal.get( Calendar.SECOND );
SimpleDateFormat formatter = new SimpleDateFormat( "hh:mm:ss", Locale.getDefault() );
Date date = cal.getTime();
timeString = formatter.format( date );
// Now the thread checks to see if it should suspend itself
if ( threadSuspended )
{
synchronized( this )
{ while ( threadSuspended )
{
wait();
}
}
}
repaint();
t.sleep( 1000 );
// interval given in milliseconds
}
}
catch (InterruptedException e)
{
}
}
void drawHand( double angle, int radius, Graphics g )
{ angle -= 0.5 * Math.PI;
int x = (int)( radius*Math.cos(angle) );
int y = (int)( radius*Math.sin(angle) );
g.drawLine( width/2, height/2, width/2 + x, height/2 + y );
}
void drawWedge( double angle, int radius,Graphics g )
{ angle -= 0.5 * Math.PI;
int x = (int)( radius*Math.cos(angle) );
int y = (int)( radius*Math.sin(angle) );
angle += 2*Math.PI/3;
int x2 = (int)( 5*Math.cos(angle) );
int y2 = (int)( 5*Math.sin(angle) );
angle += 2*Math.PI/3;
int x3 = (int)( 5*Math.cos(angle) );
int y3 = (int)( 5*Math.sin(angle) );
g.drawLine( width/2+x2, height/2+y2, width/2 + x, height/2 + y);
g.drawLine( width/2+x3, height/2+y3, width/2 + x, height/2 + y );
g.drawLine( width/2+x2, height/2+y2, width/2 + x3, height/2 + y3);
}
public void paintComponent ( Graphics g )
{ super.paintComponent (g);
g.setColor( Color.white );
drawWedge( 2*Math.PI * hours / 12, width/5, g );
drawWedge( 2*Math.PI * minutes / 60, width/3, g );
drawHand( 2*Math.PI * seconds / 60, width/2, g );
g.setColor( Color.white );
g.drawString( timeString, 10, height-10 );
System.out.println ("paintComponent");

}
public static void main (String [] args)
{
Clock1 clock = new Clock1 ();
clock.start ();
JFrame jf = new JFrame ();
Container c = jf.getContentPane ();
c.add (clock);
jf.setBounds (10, 10, 400,400);
jf.setVisible (true);
}
}
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Howdy.
The API will tell you the methods of the Graphic class that you will need. The paintComponent method is the place to put your changes. The circle is simple enough, just use drawOval(), the numbers can be put in with drawString().
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Mark Phylus
Greenhorn

Joined: Mar 28, 2004
Posts: 18
Thanks guys. This turned out to be really great.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clock face