source for an analog clock(GUI)

Please help me, i need an analog & digital clock source code in graphical user interface

i would be pleased if anyone helps

Comments

  • : 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
  • 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 :((((

  • : 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.
  • thanks a lot, i will try that :D
  • : 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
  • : : 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).
  • 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 :(
  • : 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?
  • cannot find symbol
    symbol : variable g
    location: class Text2
    g.drawLine(center.x, center.y, hand.x, hand.y);
    ^
    10 errors
  • : 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.
  • please man please give me all the code
  • [b][red]This message was edited by Vilanye at 2007-4-10 20:20:27[/red][/b][hr]
    : please man please give me all the code
    :
    He has already "helped" you more then he probably should of.

    What will giving you the code teach you?

    Nothing.

    How will it help you?

    It might help you pass, but is passing worth anything if you don't know how to program?

    Show some effort and ask specific questions

    [italic][blue]Just my 2 bits[/blue][/italic]



  • man can tell you tell me all the procedures i should pass another time here pls
  • : man can tell you tell me all the procedures i should pass another time here pls
    :
    These are the fields and methods I would create:
    [code]
    public AnalogueClock {

    private int size;
    private int hours;
    private int minutes;
    private int seconds;

    private Point hoursToLocation(int hours, Point center, int length)
    private void systemTime()
    private void drawMarks(Graphics g)
    private void drawFace(Graphics g)
    private void drawHand(Graphics g, int length)
    public AnalogueClock(int size)
    public void draw(Graphics g)
    public Calendar getTime()

    }
    [/code]
    The names of the procedures should be a clear enough indication of their function.
  • graphics is an abstract class so u have to keep it in paint methord
    ie
    public void paint(Graphics g)
Sign In or Register to comment.

Howdy, Stranger!

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

Categories