Question: 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).
I have Followed my example code, and come up with 3 classes. Clock, which sets the outines for drawing clock objects, ClockViewerFrame which sets up the JPanel with textfields and a draw button. and ClockViewer to run the whole thing. The problem I have is adding my clock to the panel and using the values calculated by taking user input from the text fields to set the hands of the clock in the right position.
My code thus far:
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 Clock
{
public Clock(double minuteAngle, double hourAngle, double hourRadius, double minuteRadius)
{
mAngle = minuteAngle;
hAngle = hourAngle;
hRadius = hourRadius;
mRadius = minuteRadius;
}
public void draw(Graphics2D g2)
{
Ellipse2D.Double face
= new Ellipse2D.Double(100, 100, 200, 200);
Point2D.Double centre
= new Point2D.Double(200, 200);
Point2D.Double hourOuter
= new Point2D.Double(150 + hRadius, 150 + hAngle);
Point2D.Double minuteOuter
= new Point2D.Double(200 + mRadius, 200 + mAngle);
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);
}
private double hAngle;
private double mAngle;
private double hRadius;
private double mRadius;
}
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;
public class ClockViewerFrame extends JFrame
{
public ClockViewerFrame()
{
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 hour = (int) Double.parseDouble(
hourField.getText());
int minute = (int) Double.parseDouble(
minuteField.getText());
minuteX = 100 * Math.cos(6 * minute);
minuteY = 100 * Math.sin(6 * minute);
double hourMinute = hour * 60;
hourX = 100 * Math.cos(0.5 * hourMinute);
hourY = 100 * Math.sin(0.5 * hourMinute);
}
}
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);
}
private JLabel hourLabel;
private JLabel minuteLabel;
private JTextField hourField;
private JTextField minuteField;
private JButton button;
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 = 100;
}
import javax.swing.JFrame;
public class ClockViewer
{
public static void main(String[] args)
{
JFrame frame = new ClockViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
many thanks in advance.