Write a program that draws a clock face with a time that the user enters in two text fields (one for the hours, one for the minutes).

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;  

public class ClockViewerFrame extends JFrame
{   
    public ClockViewerFrame()   
    { 
       ClockComponent component = new ClockComponent();
       add(component);
       createTextField();      
       createButton();      
       createPanel();       
       setSize(FRAME_WIDTH, FRAME_HEIGHT);   
    } 
    private void createTextField()   
    {      
        hourLabel = new JLabel("Hour: ");       
        final int FIELD_WIDTH = 10;      
        hourField = new JTextField(FIELD_WIDTH);      
        hourField.setText("");      
        minuteLabel = new JLabel("Hour: ");       
        minuteLabel = new JLabel("Minute: ");      
        minuteField = new JTextField(FIELD_WIDTH);      
        minuteField.setText("");   
    }        
    private void createButton()   
    {      
        button = new JButton("Draw");        
        class AddInterestListener implements ActionListener      
        {         
            public void actionPerformed(ActionEvent event)         
            { 
                int radius = 100;
                int minuteLength=(int)(radius*0.8);
                int hourLength=(int)(minuteLength*0.8);
                int hour = (int) Double.parseDouble(hourField.getText());
                int minute = (int) Double.parseDouble(minuteField.getText());
                int minuteX= 100 +(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0));
                int minuteY= 100 -(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0));
                int hourX= 100 +(int)(hourLength*Math.sin((minute/60.0+hour)*Math.PI/6.0));
                int hourY=100 -(int)(hourLength*Math.cos((minute/60.0+hour)*Math.PI/6.0));
                repaint();
            }
        }
        ActionListener listener = new AddInterestListener();      
        button.addActionListener(listener);    
    }   

    private void createPanel()   
    {      
        panel = new JPanel();      
        panel.add(hourLabel);      
        panel.add(hourField);      
        panel.add(minuteLabel);      
        panel.add(minuteField);      
        panel.add(button);      
        add(panel);    
    }   
    public void paint(Graphics g) 
    {          
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        ClockComponent component = new ClockComponent();

    }
    private JLabel hourLabel;   
    private JLabel minuteLabel;   
    private JTextField hourField;   
    private JTextField minuteField;   
    private JButton button;    
    private JLabel resultLabel;   
    private JPanel panel;    
    public static double minuteX;   
    public static double hourX;   
    public static double hourY;   
    public static double minuteY;   
    private static final int FRAME_WIDTH = 450;   
    private static final int FRAME_HEIGHT = 600;  
}

import javax.swing.JFrame;
/**
   This program displays a clock face with a time that the user specified.
*/
public class ClockViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new ClockViewerFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("ClockViewer");
      frame.setVisible(true);
   }
}


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
/**
 * Write a description of class ClockComponent here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ClockComponent extends JComponent
{
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        int width=this.getWidth();
        int height=this.getHeight();

        Ellipse2D.Double face 
           = new Ellipse2D.Double(width/2 + 100, height/2 + 100, 100, 100); 
        Point2D.Double centre
           = new Point2D.Double(width/2,height/2);
        Point2D.Double hourOuter
           = new Point2D.Double(width/2 + 60 ,height/2 + 60);
        Point2D.Double minuteOuter
           = new Point2D.Double(width/2 + 80 ,height/2 + 80);
        Line2D.Double hourHand
           = new Line2D.Double(centre, hourOuter);
        Line2D.Double minuteHand
           = new Line2D.Double(centre, minuteOuter);
        g2.draw(face);
        g2.draw(hourHand);
        g2.draw(minuteHand);

    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

actually i can not draw the clock in the Jframe, can anyone help me.i am new in java and dont konw how to do it

This article has been dead for over six months. Start a new discussion instead.