I've got a school assignment where I have to create a simple analog and digital clock that displays the time like this "XX:XX" and draws a clock. As in this example:
I've written something in Java, but I get all kinds of errors. The goal of this assignment was learning to implement classes and define methods, etc. If anyone can give me some tips (not the actual solution) on what is wrong or what I can improve, I would be very grateful.
Main code
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ShowClock {
public static void main (String[] args) {
double hour = Double.parseDouble(JOptionPane.showInputDialog("What time is it (hours)?"));
double minutes = Double.parseDouble(JOptionPane.showInputDialog("What time is it (minutes)?"));
String time= String.valueOf(hour) + String.valueOf(minutes) ;
JFrame frame = new JFrame("test app");
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Hoe laat is het?");
Klok k = new Klok(hour, minutes);
frame.add(k);
frame.setVisible(true);;
}
}
Class Klok
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
class Klok extends JComponent {
private double hour;
private double minutes;
private String time;
int anglehour = (int)((90 - (hour + minutes / 60) * 30 ) * Math.PI / 180);
int angleminutes = (int)((90 - minutes * 6.0) * Math.PI / 180);
int xendpointhour = (int)(150+(75*Math.cos(anglehour)));
int yendpointhour = (int)(150-(75*Math.sin(anglehour)));
int xendpointminutes = (int)(150+(75*Math.cos(angleminutes)));
int yendpointminutes = (int)(150-(75*Math.sin(angleminutes)));
public void draw (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawString(time, 0, 400);
g2.drawOval(75,75, 150, 150);
g2.drawLine(150,150, xendpointhour, yendpointhour);
g2.drawLine(150, 150, xendpointminutes, yendpointminutes);
}
}
UPDATE: I still don't quite get it. I think I need a simple explanation on how constructors and methods work, because my book is not very descriptive. Sorry for bothering anyone wit these noob questions...
Main Class
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class KlokTonen {
public static void main(String[] args) {
double hour = Double.parseDouble(JOptionPane.showInputDialog("What time is it (hours)?"));
double minutes = Double.parseDouble(JOptionPane.showInputDialog("What time is it (minutes)?"));
String time= String.valueOf(hour) + String.valueOf(minutes) ;
JFrame frame = new JFrame("test app"); frame.setSize(300,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Hoe laat is het?");
Klok k = new Klok(hour, minutes);
frame.add(k);
frame.setVisible(true);;
}
}
Second Class
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class Klok extends JComponent {
private double hour;
private double minutes;
private String time;
public void draw (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int angleHour = (int)((90 - (hour + minutes / 60) * 30 ) * Math.PI / 180);
int angleMinutes = (int)((90 - minutes * 6.0) * Math.PI / 180);
int xEndPointHour = (int)(150+(75*Math.cos(angleHour)));
int yEndPointHour = (int)(150-(75*Math.sin(angleHour)));
int xEndPointMinutes = (int)(150+(75*Math.cos(angleMinutes)));
int yEndPointMinutes = (int)(150-(75*Math.sin(angleMinutes)));
g2.drawString(time, 0, 400);
g2.drawOval(75,75, 150, 150);
g2.drawLine(150,150, xEndPointHour, yEndPointHour);
g2.drawLine(150, 150, xEndPointMinutes, yEndPointMinutes);
}
}