Question

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() {

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

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

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.DISPOSE_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*Math.PI/6)));

        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];

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

        //minute hand
        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);

        //hour hand
        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);

    }

}
Project ▼ 75 76 -idea 12 out 78 ▶ G Clock.java ClockGUL.iml 1111 External Libraries 10 Scratches and Consoles h.51n(İsHath .

Add a comment
Know the answer?
Add Answer to:
Java question. I am creating a clock program in Java GUI, but it has some problem....
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
  • 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...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import...

    please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Random; enum Dir{L, U, R, D}; class Food { public static int FOODSTYLE = 1; private int m = r.nextInt(Yard.WIDTH / Yard.B_WIDTH); private int n = r.nextInt(Yard.HEIGHT / Yard.B_WIDTH - 30/Yard.B_WIDTH) + 30/Yard.B_WIDTH;// 竖格 public static Random r = new Random(); public int getM() { return m; } public void setM(int m)...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • 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 {               ...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • I am creating a program where a user will enter in a price amount. And then...

    I am creating a program where a user will enter in a price amount. And then the program will determine how many 20, 10, 5, 1, etc.. they get back. I keep getting "error: unexpected type" in my findValue method. Not too sure why I am getting this. There also might be more bugs in there that I am not aware of because it wont compile. Any help is appreciated. Here is my code: import java.util.Scanner; public class prac1 {...

  • **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...

  • I was wondering if I could get some help with a Java Program that I am...

    I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...

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