Friday, 28 October 2011

Java: Text Clock Complete!

Oh god, the pain. I did far too many squats from a huge period of not doing anything, and to add to it, I didn't stretch properly. My legs are in so much pain right now :( You'll also notice that I changed the layout again! Black background is easier on my eyes, I assume it's not a problem for anyone else, either? Let me know if it is.
And Bersercules! Not to worry, for I am a man of my word, I will at least ATTEMPT to make the Texas Hold 'Em game!

Most of the space in these screenies is going to be taken up by comments, because, honestly, I did not understand half of what I was doing at the time. They were largely for my own benefit, but I assume you could learn from them too! As teachers go, I'm terrible, given how little I know of the subject before attempting to teach :D

Continuing with our text clock for now, though, we've just got the frame to go!

Okay, here, we're declaring our Frame, making sure its a child of actual JFrames, and implements Runnable, so it can be run by a thread. We'll want a thread variable for use later, so let's declare it now.
The constructor is only half there, since so much space was taken by the comments, but that first chunk is just standard stuff, title, dimension, close action, visibility and telling it resizing is bad. The next line I don't think we've seen before, but it grabs the area within the frame available for content, and returns it.
TL;DR: JFrame child runnable by thread. Constructor should have standard settings, title and size etc. Last line is to get an area to work with

This is the rest of the constructor. First off, we want to actually get our ClockPanel to put into the frame, then add it to the content area. Now that we have this in place, if we were to run the program, we'd have a static time, of when you started it. Would not change, so we need to start our thread running.

The start method looks at our global thread variable, and if uninitialized, creates a new thread of this object, then calls the Thread start method! In fact, I should have named our start differently to avoid confusion, but I didn't :(
TL;DR: Create time component, then add it to the content area, and put that content area back into the JFrame. Start the thread running now.

The Thread.start()  calls the run method of the class, which is just, as we've talked about before, like main() but without parameters, and only useable for threads! Seen below:

run() is now just a basic while loop that runs through as long as this is the dominant thread. Given nothing else exists in the program except the main thread, which is doing nothing, this'll always be the case for us.
repaint() will recall paintComponent, so it's important you name it that in ClockPanel!
Then we need to make sure the loop waits for a second on each execution, because goddamn would it eat up resources otherwise. It needs to be in a try-catch just because it apparently has exceptions, but I don't really see anything to be done if one came along, so I've left catch empty.
TL;DR: Run should, for us, be a loop that executes while the thread is active. The loop should repaint (performs paintComponent, important to name it that) and then wait a second between loops.

Finally, we run the ClockFrame in a main method, and get the above. Given the time on it, you can tell my post is late as fuck again!

Anyway, here's the code, source and executable (.jar), as always, in the /dist/ folder:

Now, given how little I actually understand about threads (Still, infinitely more than I did a few days ago. Yay multiplication by 0!), I'll be looking at more examples before I get to making a game of our very own. Which, incidentally, is going to be Texas Hold 'Em. I'd best get to reading, so that's all for now! Questions welcome, comments, too! Follow/Subscribe etc, and see you tomorrow!

Shameless plug for here! It's got some free public domain movies, music and books on it.

1 comment: