Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analog clock using JAVA #568

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions JAVA/Analog_Clock/Clock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.applet.Applet;
import java.awt.*;
import java.util.*;
/*<applet code="Clock.class" width="300" height="300">
</applet>*/
public class Clock extends Applet
{
public void init()
{
this.setSize(new Dimension(800, 400));
setBackground(new Color(50, 50, 50));
new Thread() {
public void run()
{
while (true) {
repaint();
delayAnimation();
}
}
}.start();
}
private void delayAnimation()
{
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public void paint(Graphics g)
{
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
if (hour > 12) {
hour -= 12;
}
g.setColor(Color.white);
g.fillOval(300, 100, 200, 200);
g.setColor(Color.black);
g.drawString("12", 390, 120);
g.drawString("9", 310, 200);
g.drawString("6", 400, 290);
g.drawString("3", 480, 200);
double angle;
int x, y;
angle = Math.toRadians((15 - second) * 6);
x = (int)(Math.cos(angle) * 100);
y = (int)(Math.sin(angle) * 100);
g.setColor(Color.red);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - minute) * 6);
x = (int)(Math.cos(angle) * 80);
y = (int)(Math.sin(angle) * 80);
g.setColor(Color.blue);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - (hour * 5)) * 6);
x = (int)(Math.cos(angle) * 50);
y = (int)(Math.sin(angle) * 50);
g.setColor(Color.black);
g.drawLine(400, 200, 400 + x, 200 - y);
}
}
Binary file added JAVA/Analog_Clock/Images/a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JAVA/Analog_Clock/Images/ac.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JAVA/Analog_Clock/Images/ac1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JAVA/Analog_Clock/Images/b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JAVA/Analog_Clock/Images/c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions JAVA/Analog_Clock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## GUI Calculator

<p align="center">
<img src="./Images/ac1.png">
</p>

It is the Analog Clock written in Java with the help of awt components , Applets. To create GUI based applications, Java provides Applet framework which is a graphical user interface framework that includes a set of classes that are flexible.

To create a Analog clock, we will use Calendar and math classes . The Applet class is used to construct a top-level window for a Java application. The Calendar class is used to get the present time.

## **Quick Start**
- Clone this repository

```
git clone https://github.com/abhijeet007rocks8/Dev-Scripts.git
```
- Change Directory

```
cd JAVA
```
```
cd Analog_Clock
```
```
javac Clock.java
```
```
appletviewer Clock.java
```

## **Installation and Dependencies**
- Install JDK kit for java devlopment which supports Applets.
- Install any latest version IDE for java like: Net-Beans, Eclipse, BlueJ, etc.
- Then follow the Quick start steps inside any IDE and then compile and run the project.

## Packages used

### awt package
AWT stands for Abstract Window Toolkit. In Java, AWT is a package used to develop GUI or window-based applications in java.
But `java.awt` components are platform-dependent i.e. components are displayed according to the view of operating system. AWT components are heavyweight i.e. its components depends on the resources of OS (Operating System).

`java.awt` package provides classes such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

### Usage

```java
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
```

## Working

<p align="center">
<img src="./Images/ac.png">
</p>

The Analog clock to display the current time.


## Screenshots

<p align="center">

<img src="./Images/ac2.png">
<br>
<br>
<img src="./Images/a.png">
<br>
<br>
<img src="./Images/b.png">
<br>
<br>
<img src="./Images/c.png">
</p>


## Demonstration
https://media.geeksforgeeks.org/wp-content/uploads/20190514184622/analogClock1.mp4

**Author : Jayanth MKV**

**This Project is contributed to Dev-Script under GSSOC-2022 by Jayanth MKV**