Wednesday, 17 February 2021

Java Desktop Clock

  • This is a program for creating a desktop clock that shows the time in both digital and analogue form in java.
  • To set it as a startup app paste the jar formed from this file in your C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp folder
  • This is the code for the file:

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.MouseInfo;

import java.awt.Toolkit;

import java.awt.Window.Type;

import java.awt.geom.Ellipse2D;

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

 

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class clockjar {

 

       @SuppressWarnings("serial")

       public static void main(String[] args) throws InterruptedException {

              final JFrame frm=new JFrame();

              frm.setUndecorated(true);

              frm.setOpacity(1);

              frm.setSize(101, 101);

              frm.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width-frm.getWidth(),170);

              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frm.setAlwaysOnTop(true);

              frm.setType(Type.UTILITY);

              frm.setVisible(true);

              JPanel pnl=new JPanel();

              final SimpleDateFormat h = new SimpleDateFormat("HH");

              final SimpleDateFormat m = new SimpleDateFormat("mm");

              final SimpleDateFormat s = new SimpleDateFormat("ss");

              frm.setShape(new Ellipse2D.Double(0, 0, frm.getWidth(), frm.getHeight()));

              frm.setOpacity(0.65f);

              for(;;) {

                     frm.remove(pnl);

                     pnl=new JPanel() {

                           @Override

                           protected void paintComponent(Graphics g) {

                                  g.setColor(Color.black);

                                  g.drawOval(0,0, 100, 100);

                                  g.setColor(Color.green);

                                  g.drawOval(1,1, 98, 98);

                                  g.setColor(Color.black);

                                  g.drawOval(2,2, 96, 96);

                                  Timestamp timestamp1 = new Timestamp(System.currentTimeMillis());

                                  double hr=Double.parseDouble(h.format(timestamp1.getTime()));

                                  double min=Double.parseDouble(m.format(timestamp1.getTime()));

                                  double sec=Double.parseDouble(s.format(timestamp1.getTime()));

                                  double ahr=(hr+(min+sec/3600)/60+sec/3600);

                                  ahr=ahr*360/12;

                                  double amin=min+sec/3600;

                                  amin=amin*360/60;

                                  double asec=sec;

                                  asec=asec*360/60;

                                  ahr=(270+ahr)%360;

                                  amin=(270+amin)%360;

                                  asec=(270+asec)%360;

                                  g.drawLine(50, 50, 50+(int) (45*Math.cos(Math.toRadians(asec))), 50+(int) (45*Math.sin(Math.toRadians(asec))));

                                  Graphics2D g2 = (Graphics2D) g;

                                  g2.setStroke(new BasicStroke(2));

                                  g.setColor(Color.red);

                                  g.drawLine(50, 50, 50+(int) (35*Math.cos(Math.toRadians(amin))), 50+(int) (35*Math.sin(Math.toRadians(amin))));

                                  g2.setStroke(new BasicStroke(3));

                                  g.setColor(Color.blue);

                                  g.drawLine(50, 50, 50+(int) (20*Math.cos(Math.toRadians(ahr))), 50+(int) (20*Math.sin(Math.toRadians(ahr))));

                                  g.setColor(Color.red.darker());

                                  String minstr=(int)min+"";

                                  String hrstr=(int)hr%12+"";

                                  if ((int)min/10==0) {

                                         minstr="0"+minstr;

                                  }

                                  if ((int)hr%12/10==0) {

                                         hrstr="0"+hrstr;

                                  }

                                  g.drawString(hrstr+":"+minstr, 34, 80);

                           }

                     };

                     frm.add(pnl);

                     frm.invalidate();

                     frm.validate();

                     frm.repaint();

                     Thread.sleep(100);

                     if (MouseInfo.getPointerInfo().getLocation().x<=frm.getLocation().x+frm.getWidth()

                     && MouseInfo.getPointerInfo().getLocation().x>=frm.getLocation().x

                     && MouseInfo.getPointerInfo().getLocation().y<=frm.getLocation().y+frm.getHeight()

                     && MouseInfo.getPointerInfo().getLocation().y>=frm.getLocation().y) {

                           frm.setOpacity(0);

                     }

                     else {

                           frm.setOpacity(0.65f);

                     }

              }

       }

 

}

  • Run the file in your IDE 


No comments:

Post a Comment

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.