Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

source for an analog clock(GUI)

lars_peterlars_peter Posts: 8Member
Please help me, i need an analog & digital clock source code in graphical user interface

i would be pleased if anyone helps
«1

Comments

  • zibadianzibadian Posts: 6,349Member
    : Please help me, i need an analog & digital clock source code in graphical user interface
    :
    : i would be pleased if anyone helps
    :
    Here is a function to convert hours to a cartesian location.
    [code]
    public Point hoursToLocation(int hours, Point center, int scale) {
    Point result = new Point();
    Point.x = Math.round(Math.sin(hours/24*2*Math.PI)*scale) + center.x;
    Point.y = center.y - Math.round(Math.cos(hours/24*2*Math.PI)*scale);
    return Point;
    }
    [/code]
    From this it is simple enough to paint a line between the center and the location returned by this function. Scale is the length of the line.
    A similar function is necessary for the minute to location.
    To paint a line you need to subclass a panel and override its paint() method. A simple line can be painted using the Graphics.drawLine() method.
    The last thing you'll need is to get the time from the system. For this you'll need the Calendar class. See this for more info: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
  • lars_peterlars_peter Posts: 8Member
    hey please if you could post the whole thing, you would make my live perfect :) . i get frustrated when i read the hints you gave me, because i
    just can do it :((((

  • zibadianzibadian Posts: 6,349Member
    : hey please if you could post the whole thing, you would make my live perfect :) . i get frustrated when i read the hints you gave me, because i
    : just can do it :((((
    :
    :
    Knowing how to code a subclass is the basis of Java programming, so that shouldn't be too difficult. Overriding a method is the next step in learning Java, so you should also know that. Here's the code to get the currect time:
    [code]
    Calendar now = Calendar.getInstance();
    int hours = now.get(Calendar.HOUR_OF_DAY);
    int minutes = now.get(Calendar.MINUTE);
    int seconds = now.get(Calendar.SECOND);
    [/code]
    First try to paint the current time, without any animation. Animation in this case is somewhat trickier. There are also a lot of excellent tutorials in the Java API (http://java.sun.com/j2se/1.5.0/docs/api/). It is worth your time to take a look at them.
  • lars_peterlars_peter Posts: 8Member
    thanks a lot, i will try that :D
  • lars_peterlars_peter Posts: 8Member
    : thanks a lot, i will try that :D
    :
    hey friend can you give me another simplified method, because if i will do that one, the teacher definetly thinks that i didnt do it myself :( .
    We are now at the first lessons of swing you know
  • zibadianzibadian Posts: 6,349Member
    : : thanks a lot, i will try that :D
    : :
    : hey friend can you give me another simplified method, because if i will do that one, the teacher definetly thinks that i didnt do it myself :( .
    : We are now at the first lessons of swing you know
    :
    For an analog clock you don't need swing. Heck, you don't even need the AWT, you can override the Applet.paint(). But it is better to paint it in a panel, because then you can include it in other programs.
    Here's a code to draw the hour handle:
    [code]
    Point center = new Point(getWidth()/2, getHeight/2);
    int scale = Math.round(getWidth()/2.5);
    Point hand = hoursToLocation(hours, center, scale);
    g.drawLine(center.x, center.y, hand.x, hand.y);
    [/code]
    The minute and second hands should be easy enough. The markers around the clock are not much more difficult. Draw a line for every marker you want (such as quarter hour markers). Then change the color to the background color and draw a shorter line over the original one. This will "erase" a large part of the line and "reveal" the marker.
    If the clock doesn't look right, just play with the scale. With a little recoding of the scale, you can even create oval clocks or even rectangular clocks (including the hands).
  • lars_peterlars_peter Posts: 8Member
    i've got some problem(don't laugh).
    here i've done this

    public class Text2 {

    public Text2()
    {
    Point center = new Point(getWidth()/2, getHeight/2);
    int scale = Math.round(getWidth()/2.5);
    Point hand = hoursToLocation(hours, center, scale);
    g.drawLine(center.x, center.y, hand.x, hand.y);
    }
    }

    and it wont compile :(
  • zibadianzibadian Posts: 6,349Member
    : i've got some problem(don't laugh).
    : here i've done this
    :
    : public class Text2 {
    :
    : public Text2()
    : {
    : Point center = new Point(getWidth()/2, getHeight/2);
    : int scale = Math.round(getWidth()/2.5);
    : Point hand = hoursToLocation(hours, center, scale);
    : g.drawLine(center.x, center.y, hand.x, hand.y);
    : }
    : }
    :
    : and it wont compile :(
    :
    What's the error it is giving?
  • lars_peterlars_peter Posts: 8Member
    cannot find symbol
    symbol : variable g
    location: class Text2
    g.drawLine(center.x, center.y, hand.x, hand.y);
    ^
    10 errors
  • zibadianzibadian Posts: 6,349Member
    : cannot find symbol
    : symbol : variable g
    : location: class Text2
    : g.drawLine(center.x, center.y, hand.x, hand.y);
    : ^
    : 10 errors
    :
    You should add the variable g as a parameter to your function. The paint() method should then provide its parameter to your function.
«1
Sign In or Register to comment.