Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,822 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,760 people online right now. Registration is fast and FREE... Join Now!




clock applet shows wrong time!

 

clock applet shows wrong time!

xoxou

24 Oct, 2008 - 07:38 AM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2008
Posts: 2

Hi,i have written an analog clock web applet, and it works fine. But the problem is that the hour hand and the minute hand have interchanged positions.So if the time is 5 'o' clock,it shows 12:25!!.i cant figure out where the problem is.please help.
here's the code for the clock:
CODE

import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.applet.*;

public class Clock extends Applet implements Runnable {

    final static int midX = 180;
    final static int midY = 180;
    final static int rad = 90;
    Thread aclock = null;
    ClockHand seconds, minutes, hours;
    int sec, min, hr;
    Font fnt;
    boolean compulsoryRun = true;
    Calendar time;
    Calendar clocktime;
    String Timestart;
    String Timstart;
    Date dat;
    String da;
    String date;

    public void init() {
        setBackground(Color.green);
        setForeground(Color.gray);
        clocktime = Calendar.getInstance();
        time = Calendar.getInstance();
        fnt = new Font("Arial", Font.BOLD, 24);
    }

    public void start() {
        compulsoryRun = true;
        aclock = new Thread(this);
        seconds = new ClockHand(clocktime.get(Calendar.SECOND), rad - 15, 15, Color.black, 0);
        minutes = new ClockHand(clocktime.get(Calendar.MINUTE), rad * 5 / 7, Color.red, 9);
        hours = new ClockHand(clocktime.get(Calendar.HOUR_OF_DAY), rad - 15, 15, Color.blue, 14);
        sec = time.get(Calendar.SECOND);
        min = time.get(Calendar.MINUTE);
        hr = time.get(Calendar.HOUR_OF_DAY);
        hr = hr % 12;
        dat = new Date();
        da = new String();
        date = new String();
        da = dat.toString();
        date = da.substring(0, 10);
        aclock.start();
    }

    public void stop()
{
        aclock = null;
        compulsoryRun = false;
        Timestart += "stopped";
        Timstart += "stopped";
    }

    public void run()
{
        repaint();
        while (compulsoryRun)
  {
            repaint(midX - rad - 5, midY - rad - 5, midX + rad + 5, midY + rad + 5);
            try
    {
                Thread.sleep(1000);
     } catch (InterruptedException ex)
           {
                System.out.println("Exception raised is: " + ex);
            }
            sec++;
            if (sec == 60)
                {
                sec = 0;
                min++;
            }
            if (min == 60) {
                min = 0;
                hr++;
            }
            if (hr == 13) {
                hr = 1;
                Timstart = hr + ":" + min + ":" + sec;
            }
            seconds.value++;
            if (seconds.value == 60) {
                seconds.value = 0;
                minutes.value++;
            }
            if (minutes.value == 60) {
                minutes.value = 0;
                hours.value++;
            }
            if (hours.value == 13) {
                hours.value = 1;
                Timestart = (int) hours.value + ":" + (int) minutes.value + ":" + (int) seconds.value;
            }
        }
    }

    private void assignAngles() {
        seconds.ang = (seconds.value) * Math.PI / 30;
        minutes.ang = (minutes.value) * Math.PI / 30 + (seconds.value) * Math.PI / 1800;
        hours.ang = (hours.value) * Math.PI / 6 + (minutes.value) * Math.PI / 360;
    }

    public void paint(Graphics g) {
        update(g);
    }

    public void update(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.yellow);
        g.fillOval(midX - rad, midY - rad, 2 * rad, 2 * rad);
        g.setColor(Color.black);
        g.drawOval(midX - rad, midY - rad, 2 * rad, 2 * rad);
        g.setColor(Color.gray);
        g.drawOval(midX - rad - 2, midY - rad - 2, 2 * rad + 4, 2 * rad + 6);
        g.drawOval(midX - rad - 3, midY - rad - 3, 2 * rad + 6, 2 * rad + 6);
        g.setColor(Color.black);    
        g.fillRect(midX+rad/2-28,midY-7,68,14);
        g.setColor(Color.white);
        g.drawString(date,midX+rad/2-28,midY+4);
        for (int i = 0; i < 60; i++) {
            if (i % 5 == 0) {
                g.setColor(Color.black);
                g.drawLine(midX + (int) ((rad * 6 / 7) * Math.sin(Math.toRadians(i * 6))), midY - (int) ((rad * 6 / 7) * Math.cos(Math.toRadians(i * 6))), midX + (int) (rad * Math.sin(Math.toRadians(i * 6))),
                        midY - (int) (rad * Math.cos(Math.toRadians(i * 6))));
            } else {
                g.setColor(Color.gray);
                g.drawLine(midX + (int) ((rad * 6.5 / 7) * Math.sin(Math.toRadians(i * 6))), midY - (int) ((rad *
                        6.5 / 7) * Math.cos(Math.toRadians(i * 6))), midX + (int) (rad * Math.sin(Math.toRadians(i *
                        6))), midY - (int) (rad * Math.cos(Math.toRadians(i * 6))));
            }
        }
        showStatus(Timestart);
        showStatus(Timstart);
        assignAngles();

        hours.disp(g);
        minutes.disp(g);
        seconds.disp(g);

        g.setColor(Color.red);
        g.fillOval(midX - 3, midY - 3, 6, 6);
        g.setColor(Color.green);
        g.fillOval(midX - 1, midY - 1, 2, 2);
        g.setColor(Color.black);
        g.setFont(fnt);
        showStatus("Current Time ---- " + hr + ":" + min / 10 + min % 10 + ":" + sec / 10 + sec % 10);
    }

    class ClockHand {

        private int leng;
        private int hlength;
        private Color handColour;
        private Shape hand;
        private double rect;
        double ang;
        double value;
        AffineTransform at;

        ClockHand(double val, Color colour, int ret) {
            leng = 100;
            value = val;
            hlength = 0;
            handColour = colour;
            rect = Math.PI * ret / 180;
            initial();

        }

        ClockHand(double val, int leng, Color colour, int ret) {
            this.leng = leng;
            this.value = val;
            hlength = 0;
            handColour = colour;
            rect = Math.PI * ret / 180;
            initial();
        }

        ClockHand(double val, int leng, int ha, Color colour, int ret) {
            this.leng = leng;
            this.value = val;
            hlength = ha;
            handColour = colour;
            rect = Math.PI * ret / 180;
            initial();
        }

        void initial() {
            int x[] = new int[4];
            int y[] = new int[4];
            x[0] = Clock.midX - hlength;
            y[0] = Clock.midY;
            x[1] = Clock.midX + (int) ((leng / 2) * Math.cos(rect));
            y[1] = Clock.midY - (int) ((leng / 2) * Math.sin(rect));
            x[2] = Clock.midX + leng;
            y[2] = Clock.midY;
            x[3] = Clock.midX + (int) ((leng / 2) * Math.cos(rect));
            y[3] = Clock.midY + (int) ((leng / 2) * Math.sin(rect));
            hand = new Polygon(x, y, 4);
        }

        void disp(Graphics g) {
            Graphics2D gd = (Graphics2D) g;
            g.setColor(handColour);

            at = AffineTransform.getRotateInstance(ang - Math.PI / 2, Clock.midX, Clock.midY);
            gd.fill(at.createTransformedShape(hand));
            gd.draw(at.createTransformedShape(hand));
        }
    }
}


And here's the html code for the applet:
CODE
<html>
<applet code="Clock" height = 350 width = 370 >
</applet>
</html>


Thanks.

User is offlineProfile CardPM
+Quote Post


g00se

RE: Clock Applet Shows Wrong Time!

25 Oct, 2008 - 01:47 AM
Post #2

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,234



Thanked: 144 times
My Contributions
Your clock hand ctors should probably be simplified - they probably confused you. If you swap some parameters around to them, it should fix it:

CODE

                // 4 args
                minutes = new ClockHand(
                        clocktime.get(Calendar.MINUTE),
                        //(rad * 5) / 7,
                        rad - 15,
                        Color.red,
                        9
                        );
                // 5 args
                hours = new ClockHand(
                        clocktime.get(Calendar.HOUR_OF_DAY),
                        //rad - 15,
                        (rad * 5) / 7,
                        15,
                        Color.blue,
                        14
                        );



This post has been edited by g00se: 25 Oct, 2008 - 01:49 AM
User is offlineProfile CardPM
+Quote Post

xoxou

RE: Clock Applet Shows Wrong Time!

25 Oct, 2008 - 08:51 AM
Post #3

New D.I.C Head
*

Joined: 24 Oct, 2008
Posts: 2

Thanks a lot biggrin.gif , that worked. icon_up.gif . helped me a lot.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:30PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month