Question

This is a Clock program in Java. Can you fix my Java program error, and add function to display c...

This is a Clock program in Java.

Can you fix my Java program error, and add function to display current time currently?

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Clock extends javax.swing.JFrame {
   public int hour;
   public int min;
   public int sec;
   ClockDial cd;

   public Clock() {
       setSize(510, 530);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       cd = new ClockDial(this);
       getContentPane().add(cd);
       Date curr = new Date();
       String time = curr.toString();
       hour = Integer.parseInt(time.substring(11, 13));
       min = Integer.parseInt(time.substring(14, 16));
       sec = Integer.parseInt(time.substring(17, 19));
       ClockEngine.setPriority(ClockEngine.getPriority() + 3);
       ClockEngine.start();
   }

   public static void main(String args[]) {
       new Clock().setVisible(true);
   }

   Thread ClockEngine = new Thread();

   {
       int newsec, newmin;

       public void run() {
           while(true) {
               newsec = (sec + 1) % 60;
               newmin = (min + (sec + 1) / 60) / 60;
               hour = (hour + (min + (sec + 1) / 60) % 60) % 12;
               sec = newsec;
               min = newmin;

               try {
                   Thread.sleep(1000);
               } catch(InterruptedException ex) {}
               cd.repaint();
           }
       }
   };
}

class ClockDial extends JPanel{
   Clock parent;
   public ClockDial(Clock pt) {
       setSize(520, 530);
       parent = pt;
   }

   @Override
   public void paintComponent(Graphics g) {
       g.setColor(Color.BLACK);
       g.fillRect(0, 0, getWidth(), getHeight());
       g.setColor(Color.WHITE);
       g.fillOval(5, 5, 480, 480);
       g.setColor(Color.BLACK);
       g.fillOval(10, 10, 470, 470);
       g.setColor(Color.WHITE);
       g.fillOval(237, 237, 15, 15);
       g.setFont(g.getFont().deriveFont(Font.BOLD, 32));

       for(int i = 1; i <= 12; i++)
           g.drawString(Integer.toString(i), 240-(i/12)*11+(int)(210*Math.sin(i*Math.PI/6)),253-(int)(210*Math.cos(i)));

       double minsecdeg = (double)Math.PI/30;
       double hrdeg=(double)Math.PI/6;
       int tx, ty;
       int xpoints[] = new int[3];
       int ypoints[] = new int[3];

       tx = 245 + (int)(210*Math.sin(parent.sec*minsecdeg));
       ty = 245 - (int)(210*Math.cos(parent.sec*minsecdeg));
       g.drawLine(245, 245, tx, ty);

       tx = 245 + (int)(190*Math.sin(parent.min*minsecdeg));
       ty = 245 - (int)(190*Math.cos(parent.min*minsecdeg));
       xpoints[0]=245;
       xpoints[1]=tx+2;
       xpoints[2]=tx-2;
       ypoints[0]=245;
       ypoints[1]=ty+2;
       ypoints[2]=ty+2;
       g.fillPolygon(xpoints, ypoints, 3);;

       tx = 245 + (int)(160*Math.sin(parent.hour*hrdeg+parent.min*Math.PI/360));
       ty = 245 + (int)(160*Math.cos(parent.hour*hrdeg+parent.min*Math.PI/360));
       xpoints[1]=tx+4;
       xpoints[2]=tx-4;
       ypoints[1]=ty+4;
       ypoints[2]=ty+4;
       g.fillPolygon(xpoints, ypoints, 3);
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

I fixed the errors of the code and updated the code.

Clock.java:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Clock extends javax.swing.JFrame {
public int hour;
public int min;
public int sec;
ClockDial cd;
public Clock() {
setSize(510, 530);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
cd = new ClockDial(this);
getContentPane().add(cd);
Date curr = new Date();
String time = curr.toString();
hour = Integer.parseInt(time.substring(11, 13));
min = Integer.parseInt(time.substring(14, 16));
sec = Integer.parseInt(time.substring(17, 19));
ClockEngine.setPriority(ClockEngine.getPriority() + 3);
ClockEngine.start();
}
public static void main(String args[]) {
new Clock().setVisible(true);
}
Thread ClockEngine = new Thread()
{
int newsec, newmin;
public void run() {
while(true) {
newsec = (sec + 1) % 60;
newmin = (min + (sec + 1) / 60) / 60;
hour = (hour + (min + (sec + 1) / 60) % 60) % 12;
sec = newsec;
min = newmin;
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {}
cd.repaint();
}
}
};
}
class ClockDial extends JPanel{
Clock parent;
public ClockDial(Clock pt) {
setSize(520, 530);
parent = pt;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.fillOval(5, 5, 480, 480);
g.setColor(Color.BLACK);
g.fillOval(10, 10, 470, 470);
g.setColor(Color.WHITE);
g.fillOval(237, 237, 15, 15);
g.setFont(g.getFont().deriveFont(Font.BOLD, 32));
for(int i = 1; i <= 12; i++)
g.drawString(Integer.toString(i), 240-(i/12)*11+(int)(210*Math.sin(i*Math.PI/6)),253-(int)(210*Math.cos(i)));
double minsecdeg = (double)Math.PI/30;
double hrdeg=(double)Math.PI/6;
int tx, ty;
int xpoints[] = new int[3];
int ypoints[] = new int[3];
tx = 245 + (int)(210*Math.sin(parent.sec*minsecdeg));
ty = 245 - (int)(210*Math.cos(parent.sec*minsecdeg));
g.drawLine(245, 245, tx, ty);
tx = 245 + (int)(190*Math.sin(parent.min*minsecdeg));
ty = 245 - (int)(190*Math.cos(parent.min*minsecdeg));
xpoints[0]=245;
xpoints[1]=tx+2;
xpoints[2]=tx-2;
ypoints[0]=245;
ypoints[1]=ty+2;
ypoints[2]=ty+2;
g.fillPolygon(xpoints, ypoints, 3);;
tx = 245 + (int)(160*Math.sin(parent.hour*hrdeg+parent.min*Math.PI/360));
ty = 245 + (int)(160*Math.cos(parent.hour*hrdeg+parent.min*Math.PI/360));
xpoints[1]=tx+4;
xpoints[2]=tx-4;
ypoints[1]=ty+4;
ypoints[2]=ty+4;
g.fillPolygon(xpoints, ypoints, 3);
}
}
Output:

6 12 7 5 2 3

Add a comment
Know the answer?
Add Answer to:
This is a Clock program in Java. Can you fix my Java program error, and add function to display c...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java question. I am creating a clock program in Java GUI, but it has some problem....

    Java question. I am creating a clock program in Java GUI, but it has some problem. First, The time of the clock is not correct. It has to point at the current time. Second, the position of the clock character is wrong. Please fix it for me please. import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Date; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Clock extends javax.swing.JFrame { public int hour; public int min; public int sec; ClockDial cd; public Clock() {...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • **JAVA** Hi! Can't figure this out, but if you could give it a shot, I'd appreciate...

    **JAVA** Hi! Can't figure this out, but if you could give it a shot, I'd appreciate it! Everything is explained in the program via comments, and I just need help implementing the methods. Instructions for the methods are commented on top of said method. It's supposedly straightforward. Methods that need to be solved are in bold. Some might already be implemented. Just based off what's in this class, is supposed to lead to the outcome. This is all the info...

  • JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but...

    JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but i need to create a jar file . the instructions are below. It has to pass some parameters. I am really confused on doing so.l I dont know what to do next. Can someone help me and give detailed descritiopm opn how you ran the jar file in CMD import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Connect4 {               ...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • I need this program converted to c++ Please help if you can import java.util.*; // Add...

    I need this program converted to c++ Please help if you can import java.util.*; // Add two arrays of the same size (size) // Each array is a representation of a natural number // The returned array will have the size of (size + 1) elements public class Fibonacci { private static String arrToString(int[] arr) {           String s = "";           for (int i = 0; i < arr.length; i++) {               s = s + arr[i];           }...

  • CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that...

    CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that keeps an appointment book. Make a class Appointment that stores a description of the appointment, the appointment day, the starting time, and the ending time. Your program should keep the appointments in a sorted vector. Users can add appointments and print out all appointments for a given day. When a new appointment is added, use binary search to find where it should be inserted...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT