Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Applet Analog clock
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Neha Mukund Tiwari
Analog clock
1 Answer(s)      3 years and 9 months ago
Posted in : Applet


how to develope analog clock using java?


Advertisement
View Answers

June 10, 2011 at 1:30 PM


001 import java.awt.*;

002 import java.applet.*;

003 import java.util.Date;

004

005 public class ac extends Applet implements Runnable

006 {

007 Graphics og;

008 Image oi;

009 Thread t = null;

010 int width, height, delay = 500;

011

012 Date currTime;

013 int timeH, timeM, timeS, radius = 50, lenH, lenM, lenS,

014 lenIn, cx, cy, x1, y1, x2, y2;

015

016 public void init()

017 {

018 super.init();

019 width = size().width;

020 height = size().height;

021 resize(width, height);

022

023 lenH = 5 * radius / 10;

024 lenM = 6 * radius / 10;

025 lenS = 7 * radius / 10;

026 lenIn = 8 * radius / 10;

027 cx = width / 2;

028 cy = height / 2;

029

030 oi = createImage(width, height);

031 og = oi.getGraphics();

032 }

033

034 public void start()

035 {

036 if (t == null)

037 {

038 t = new Thread(this);

039 t.start();

040 }

041 }

042

043 public void stop()

044 {

045 t.stop();

046 t = null;

047 }

048

049 public void run()

050 {

051 Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);

052 while (true)

053 {

054 repaint();

055 try

056 {

057 Thread.sleep(delay);

058 }

059 catch (InterruptedException e) {}

060 }

061 }

062

063 public void update(Graphics g)

064 {

065 paint(g);

066 }

067

068 public void paint(Graphics g)

069 {

070 og.setColor(new Color(170, 170, 170));

071 og.fillRect(0, 0, width, height);

072 currTime = new Date();

073 timeH = currTime.getHours();

074 timeM = currTime.getMinutes();

075 timeS = currTime.getSeconds();

076

077 if (timeH >= 12)

078 timeH -= 12;

079

080 for (int i = 1; i < 13; i++)

081 {

082 og.setColor(new Color(20, 20, 20));

083 x2 = (int)(cx + radius * Math.sin(i * 2 * 3.14159f / 12));

084 y2 = (int)(cy - radius * Math.cos(i * 2 * 3.14159f / 12));

085 if (i % 3 != 0)

086 {

087 x1 = (int)(cx + 0.9f * radius * Math.sin(i * 2 * 3.14159f / 12));

088 y1 = (int)(cy - 0.9f * radius * Math.cos(i * 2 * 3.14159f / 12));

089 }

090 else

091 {

092 x1 = (int)(cx + 0.8f * radius * Math.sin(i * 2 * 3.14159f / 12));

093 y1 = (int)(cy - 0.8f * radius * Math.cos(i * 2 * 3.14159f / 12));

094 }

095 og.drawLine(x1, y1, x2, y2);

096 og.setColor(new Color(230, 230, 230));

097 og.drawLine(x1 - 1, y1 - 1, x2 - 1, y2 - 1); 098 }

099 og.setColor(new Color(20, 20, 20));

100 x2 = (int)(cx + lenH * Math.sin((timeH + timeM / 60.0f + timeS / 3600.0f)

101 * 2 * 3.14159f / 12));

102 y2 = (int)(cy - lenH * Math.cos((timeH + timeM / 60.0f + timeS / 3600.0f)

103 * 2 * 3.14159f / 12));

104 og.drawLine(cx, cy, x2, y2);

105 og.setColor(Color.red);

106 og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);

107 108 og.setColor(new Color(20, 20, 20));

109 x2 = (int)(cx + lenM * Math.sin((timeM + timeS / 60.0f) * 2 * 3.14159f / 60));

110 y2 = (int)(cy - lenM * Math.cos((timeM + timeS / 60.0f) * 2 * 3.14159f / 60));

111 og.drawLine(cx, cy, x2, y2);

112 og.setColor(Color.green);

113 og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); 114

115 og.setColor(new Color(20, 20, 20));

116 x2 = (int)(cx + lenS * Math.sin(timeS * 2 * 3.14159f / 60));

117 y2 = (int)(cy - lenS * Math.cos(timeS * 2 * 3.14159f / 60));

118 og.drawLine(cx, cy, x2, y2);

119 og.setColor(Color.blue);

120 og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);

121

122 og.setColor(new Color(20, 20, 20));

123 og.drawOval((width - 2 * radius) / 2, (height - 2 * radius) / 2, 2 * radius,

124 2 * radius);

125 og.setColor(new Color(230, 230, 230));

126 og.drawOval((width - 2 * radius) / 2 - 1, (height - 2 * radius) / 2 - 1,

127 2 * radius, 2 * radius);

128

129 g.drawImage(oi, 0, 0, this);

130 }

131 }

source code for the analog clock < applet code=ac.class width=120 height=120>

HTML example for the analog clock



Related Tutorials/Questions & Answers:
Analog clock
Analog clock  how to develope analog clock using java
analog to digital
analog to digital  how to convert analog to digital in java ?   Hi, What is to be converted? Is it sound or video? Thanks
Advertisements
JavaScript Clock Timer
JavaScript Clock Timer...; In this section, we are going to show you a clock timer using JavaScript... time is completed. Here is the code <html> <h2>Clock
Clock Applet in Java
Java - Clock Applet in Java       Introduction Here is a sample of running clock provided by the java applet to illustrate how to use the clock in an applet. This program shows
how can create clock baisc in java
how can create clock baisc in java   think u for help me ..  ...()); content.add(timeField); setTitle("Clock"); setSize(100,70...) { JFrame clock = new DisplayClock(); clock.setVisible(true
how can create clock baisc in java
how can create clock baisc in java   think u for help me ..  ...()); content.add(timeField); setTitle("Clock"); setSize(100,70...) { JFrame clock = new DisplayClock(); clock.setVisible(true
how can create clock baisc in java
how can create clock baisc in java   think u for help me ..  ...()); content.add(timeField); setTitle("Clock"); setSize(100,70...) { JFrame clock = new DisplayClock(); clock.setVisible(true
Display PHP clock with user input date and time
Display PHP clock with user input date and time  The following PHP code displays a clock with current date and time. I want the clock to receive user input date and time. How can this be done? <?php date<em>
How to display mysql database records as per clock timing?
How to display mysql database records as per clock timing?  I want to display database records as per clock timing
how to build a colorful clock in JSP without using Flash.
how to build a colorful clock in JSP without using Flash.  I have to make a colorful clock using java programming or some API in a JSP page. Please send me a code for it as soon as possible
Time Tracker
Time Tracker       Time Tracker is a simple tool to keep track of the time you spend on any task. Think of it as a to-do-list with a clock. And yes, it's free. Read full Description
How Voice over Internet Protocol (VoIP) Works
protocol work that it is an advance technology that converts the analog audio... "Internet protocol" instead of traditional analog systems. In this technology, analog voice calls are converted into small discrete packets of data
VoIP Equipment
converts analog voice signals to data and vice-versa so you can speak to anyone... on the Netopia Analog Telephone Adapter (ATA), this distributed solution enables
VoIP Adapters
; VoIP Analog Telephone adapter Inexpensive, easy-to-install...) port for connection to existing analog phones, fax machines, PBX and key... next generation VoIP analog telephone adaptor based on industry open standards
OOP - Java Beginners
a client program for the class clock that creates an array of Clock objects...); public static void main(String[]args) { Clock myClock[]=new Clock[4]; int y=myClock.getTime(); myClock.printTime(); } } public class Clock
Java - Swing AWT
Java  write an application program to show an animation with a single line,which changes its position in a clock-wise and anti clock-wise direction.This line should produce an effect of a spinning line
java - Java Beginners
java  write main method for display clock applet including html source code
What is AMPS?
What is AMPS?   HII, What is AMPS?   Hello, AMPS stands for Advanced Mobile Phone Service. It is a first-generation analog, circuit-switched cellular phone network
digital diary
digital diary  please post the code for a simple digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
digital diary
digital diary  please post the code for a simple digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
digital diary
digital diary  please post the code for a simple digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
OOP using Java - Java Beginners
the object mysteryClock of the Clock type, and initialize the instance variables hr,min...; Hi friend, Code to solve the problem : class Clock { int hr...(String[] args) { Clock mysteryClock = new Clock(); mysteryClock.hr=7
DSL Filter
device. DSL filter, also known as micro filter is an analog low-pass filter device that installs on telephone and other analog device for preventing interference between analog devices and DSL service operating on the same line. DSL filters
closing connections in finally block
closing connections in finally block  Hi, What will happen if I close connection before closing the resultset in the finally clock? e.g. finally... { ... conn.close(); rs.Close(); } If i do as above ... what will happen
FlashTutorials
.     Flash MX 2004 Analog Clock This tutorial will explain how to create an Analog Clock in Flash MX 2004. The free
VoIP Modem
buffers of 200 milliseconds (ms) to protect against clock skew...; VOIP over Modem If you are replacing an analog PSTN line with VOIP... to compress voice, not the analog signals sent and received by modems. A second
How to display the current time
=CYAN> <TR><TD> <FORM NAME="clock_form"> <INPUT TYPE=TEXT NAME="clock" SIZE=25> </FORM> <SCRIPT LANGUAGE="JavaScript"> <..._form.clock.blur(); setTimeout("clockTick()", 1000); } clockTick(); // End of clock -->
What is 3G ?
What is 3G ?  hello, What is 3G ?   hii samar, Third generation (3G) wireless networks will offer faster data transfer rates than current networks. The First generation of wireless (1G) was analog cellular. The second
VoIP Telephone Adapter
; VoIP Analog Telephone Adapter This Wiki is frequently visited... Adapter for the home environment. The DVG-2001S converts any existing analog... will enable users to send and receive fax the same way as a traditional analog
Loops
Loops  ??Using for loops, Write a program to simulate a clock countdown. The program prompts the user to enter the number of seconds, displays a message at every second, and terminates when the time expires. Use method
Advertisements
 

 

 

DMCA.com