Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Analog Clock working but seconds repainting in java  RSS feed

 
sam onikosi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made an Analog Clock and its working but when a remove the filloval (Background) the seconds hand keep repeating itself


here is the code


 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sam,

I'm not sure what the problem is. The seconds are not running at all, unless you resize your frame, and
the fillOval (or not) makes no difference.

A remark: your class extends JPanel. Now, it is normal to only override the 'paintComponent' method
of a JPanel, unless you know exactly what you are doing.

I took the liberty to spice up your program a little. It now runs the second as it should, and I put
in some anti-aliasing, to make the clock a bit less jagged. Have a look!

Greetz,
Piet
 
sam onikosi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Piet,
It works very fine
the only problem i am having now is i want to use swing(drag and drop) to design it, by creating Jframe form and drag drop Jpanel it,
i did and use .getgraphics() to paint the clock inside the new jpanel but it was not work

below is the code



thanks once again
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sam,

I must say I'm a bit puzzled. For the level of coding is quite advanced, you have the clock
perfectly in order, with all the cosines and sines, but, I must say, the GUI is a mess.

I would strongly advise not to use a GUI Builder in this case. We are only dealing
with a panel that bears a clock, and a frame to hold the panel. That is so little
code, that it is much easier to do that by hand. Only if you have a panel or a frame
of high complexity, would I go for a GUI builder. The code that is needed for the
clock is something that you have to do anyway, no GUI builder will do that for you.

You cannot hope to update a panel from within some other thread, by just getting
hold of that panels graphics surrounding. So you have no alternative than to put
all the complex clock code within the panel that is responsible for this clock.

Next, for these timely events, there is a Timer class available (the Swing version),
that makes these time based events very much easier to handle. See the Timer API
and the Oracle tutorial on timers.

All the changes are too much to describe here, so again I took the liberty to make
some adjustments, albeit it quite more than in my first reply.

I put the clock panel now in a separate class, so that you can use this panel in
other programs as well.

I ask you to have a close look at what I did. I left the calculations as they were,
I only took the size of the panel into consideration. If you drag the frame to some
other size, then the clock changes along. However, I did not get the formula's
correct for that case. I should have used sines and cosines, since we're dealing
with an ellipsis now, but I would like to leave that up to you.

So, study the gui, and of course, if you have any further questions, do not hesitate.

Greetz,
Piet
 
sam onikosi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piets thank you very much, i took your advice and it was all done you cant imagine the kind of fun i am having with the app.
thanks im very grateful.

one last question can i change the hands (minute, hour, second) to an image just to make it more funny

thanks
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sam,

you ar right: this is a very nice program!

You know, I tried to get the second hand to move continuously, but if failed,
since you measure the time, and that has its seconds as integers...

And you can certainly use images for your hands. Make sure that each image
has a well known point that is centered at the middle of the clock, so that
the rotation is correct.

You can set a rotation with: g2d.rotate(angle), and to make the formulas
a bit easier, you can also set the (0,0) location to the center of your panel, with
the command: g2d.translate(center_x, center_y).

Well, enough for you to have some interesting hours with experiments waiting for you!

Greetz,
Piet
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!