how to create a clock using swing

i need a clock in swing. not basically a real clock but i need 2 lines one for hour and one for min with 2 text fields where user can type hour and min and the clock on the top should display the time by the user specific needs. how can i do that? with ellipse and point2d classes?

[288 byte] By [schumachera] at [2007-11-27 10:34:44]
# 1

> i need a clock in swing. not basically a real clock

> but i need 2 lines one for hour and one for min with

> 2 text fields where user can type hour and min and

> the clock on the top should display the time by the

> user specific needs. how can i do that? with ellipse

> and point2d classes?

If you don't need a realtime clock just use two JTextFields and a JLabel to display the input..

deAppela at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...
# 2

how to draw the lines? i mean i am confused.

schumachera at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...
# 3

> how to draw the lines? i mean i am confused.

Don't be confused. Take a tutorial http://java.sun.com/docs/books/tutorial/2d/index.html

cotton.ma at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...
# 4

package cruft;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

import javax.swing.event.*;

public class TextTest

{

public static void main(String[] args)

{

Clock frame = new Clock();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class Clock extends JFrame

{

public Clock()

{

setTitle("Clock");

DocumentListener listener = new ClockFieldListener();

JPanel panel = new JPanel();

panel.add(new JLabel("Hours:"));

hourField = new JTextField("12", 3);

panel.add(hourField);

hourField.getDocument().addDocumentListener(listener);

panel.add(new JLabel("Minutes:"));

minuteField = new JTextField("00", 3);

panel.add(minuteField);

minuteField.getDocument().addDocumentListener(listener);

add(panel, BorderLayout.SOUTH);

clock = new ClockPanel();

add(clock, BorderLayout.CENTER);

pack();

}

public void setClock()

{

try

{

int hours = Integer.parseInt(hourField.getText().trim());

int minutes = Integer.parseInt(minuteField.getText().trim());

clock.setTime(hours, minutes);

}

catch (NumberFormatException e) {}

}

public static final int DEFAULT_WIDTH = 300;

public static final int DEFAULT_HEIGHT = 300;

private JTextField hourField;

private JTextField minuteField;

private ClockPanel clock;

private class ClockFieldListener implements DocumentListener

{

public void insertUpdate(DocumentEvent event) { setClock(); }

public void removeUpdate(DocumentEvent event) { setClock(); }

public void changedUpdate(DocumentEvent event) {}

}

}

class ClockPanel extends JPanel

{

public ClockPanel()

{

setPreferredSize(new Dimension(2 * RADIUS + 1, 2 * RADIUS + 1));

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

Ellipse2D circle = new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS);

g2.draw(circle);

double hourAngle = Math.toRadians(90 - 360 * minutes / (12 * 60));

drawHand(g2, hourAngle, HOUR_HAND_LENGTH);

double minuteAngle = Math.toRadians(90 - 360 * minutes / 60);

drawHand(g2, minuteAngle, MINUTE_HAND_LENGTH);

}

public void drawHand(Graphics2D g2, double angle, double handLength)

{

Point2D end = new Point2D.Double(

RADIUS + handLength * Math.cos(angle),

RADIUS - handLength * Math.sin(angle));

Point2D center = new Point2D.Double(RADIUS, RADIUS);

g2.draw(new Line2D.Double(center, end));

}

public void setTime(int h, int m)

{

minutes = h * 60 + m;

repaint();

}

private double minutes = 0;

private int RADIUS = 100;

private double MINUTE_HAND_LENGTH = 0.8 * RADIUS;

private double HOUR_HAND_LENGTH = 0.6 * RADIUS;

}

Message was edited by:

fastmike

fastmikea at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...
# 5

Hmmmmm.

cotton.ma at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...
# 6

> Hmmmmm.

Hmmmmm.

fastmikea at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> > package cruft;

> ..................

> public class TextTest

> {

> ..................

> ..................

>private double minutes = 0;

> private int RADIUS = 100;

>private double MINUTE_HAND_LENGTH = 0.8 * RADIUS;

> private double HOUR_HAND_LENGTH = 0.6 * RADIUS;

>

>

> Message was edited by:

> fastmike

fastmike

Again, spoonfeeding != teaching.

Again, you are not helping here, you are in fact harming the learning process.

Again, we are not impressed with your so-called coding abilities. Any one of us could just paste up the code and in fact create code far better than your ****, yet we know better.

Again, you are doing the forum a great disservice.

Please change your behavoir or just leave.

petes1234a at 2007-7-28 18:30:49 > top of Java-index,Java Essentials,Java Programming...