Thursday, 27 October 2011

Java: Multithread Clock (Cont)

Pfft, overslept. I am not good with that, and reading DWei's blog on his sleeping troubles made me more acutely aware of my own :-(

Anyway, the problem I was mentioning yesterday was in the region of "I need to make my own JFrame for this to work, but the GUI builder is a pile of dicks, and won't let me customize JFrame code.
Unfortunately, I've not found a way around this, so its probably a good thing I manually did the GUI stuff that one time, right? :D

Let's start with the JPanel, today, since its the highest level, being on top of the frame and all.

Now that you've had a chance to look at it (maybe I should go through it so you have a chance to mentally form it, THEN give you the code, next time? Let me know in the comments which you'd prefer!) , we can go through it! First couple of lines are just to set the font. Unless you're okay with the default which is like, size 10 Arial or some shit. You can essentially set this to whatever you want.

The next 2 methods are actually in reverse order, which the program doesn't care about. They're only like that because I needed to add in the zero method later, when I came across some annoying shit, as noted in the comments. Poor planning on my part, but I'm not a thinker, I'm a doer!

The method timeNow uses the Calendar class (Netbeans will automatically tell you when an import is needed, remember!) to grab info for us. Pretty sure we can use the Date class, too, but why use both? You can see the Calendar class has all sorts of fields we can use, I opted, in the final program, just to use the time. Hours, minutes and seconds.

With the ability now to get the time, we need some way to display it. And don't give me that setText crap, I want something I can redraw later! So we have this little ditty. It uses the Graphics class, which I haven't talked about, but I guess I'll need to when we do more complex stuff than just numbers (One of the games, that is). Until then, we'll just go with the flow and set the graphic object's font to whatever we had up in the constructor.

Black is the default color, so that next line was essentially useless, but like with getting the month and day above, I just wanted to show you what you could do :) The final line is to actually draw the time on the panel! There are 3 parameters to drawString, as you can see. The first encompasses what you want to write, and the next 2 are the x/y offset from the top corner of the panel. If your clock looks like shit, tweak these numbers :)

Tomorrow, the JFrame! That's all for now. Questions welcome! Comments, too!  Follow, subscribe, share etc, See you tomorrow!

And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 


  1. This is coming along nicely... I'm interested in how complicated the Texas Hold 'Em game is going to be.

  2. Probably very. I should have looked into it BEFORE putting it up as an option :D

  3. I like where this is going. (:

  4. I hope you still do the texas hold 'em game but I understand if its to difficult!

  5. We can be sleep deprived together and somehow get through it. :P

    Oh man, this looks like it has so much potential for errors. I would not enjoy debugging this.

  6. i'm going to try to do this on my website. very good-thanks!