Showing posts with label examples. Show all posts
Showing posts with label examples. Show all posts

Tuesday, 8 November 2011

Java: A Network Application (4)

Aah, had a migraine last night and now I don't. Best feeling ever. So I can happily move on to the NodeListener today, as promised! Meaning tomorrow we have the actual faux-application, then we're done. One day of bullshitting and stuff, then onto actual game stuff.

This is actually a very short class, and it's not ideal, since it uses the Application itself, before it's been created. I just fixed the errors popping up using the automatic stuff Netbeans does. Just created an empty class and method as a placeholder until I actually get around to making the application later tonight.

Monday, 7 November 2011

Java: A Network Application (3)

Somehow, I didn't get enough sleep, still. Oh well, time for more postin'! Today, we're continuing right off the network node from Saturday, and we'll be done with that. Tomorrow, we go to the node listener, then Wednesday we write the actual application. I'll give you until then to throw out any questions or requests about anything before we move on to those (in all probability, shitty) games!

So, diving into it:

Saturday, 5 November 2011

Java: A Network Application (2)

Today's an early one, but also short, since I've had no sleep yet! Let's start at...well, the start. We'll want the lowest level of objects first, which is the NetworkNode itself, so we can have a Listener listen for it, and then the application to interact with it. This should be the only class I'm doing in 2 parts, the others look short, at a glance.

We're going to be using Random, which is in the Java libraries. I don't think we've used it yet, but it's fairly straightforward. Create a random object, then use the next...() method, you'll see below:

Friday, 4 November 2011

Java: More Multithreading: A Network Application (1)

This is the last example in the book, and I sure hope it helps me understand multithreading better than the other ones... Also while I remember: Texas Hold 'Em might be easier than I at first imagined, since, like 21 sticks (which I also realized is normally played with 1-4 sticks...oops!), it's (sort of) turn based. Nothing needs to happen simultaneously, so yay!

For now though, one more example on multithreading: A Network Application. From the text:
"The main purpose of this section is not to solve any particular problem but to show what an event is,

Thursday, 3 November 2011

Java: Let's All Go On A Tangent!

Man, someone mentioned how often I used the word simple yesterday, but I only meant to use it for very select, small parts. The bigger picture is still....not. Don't worry, I'm as lost as you! Or more so, because I'm forced to write code despite not knowing quite what half of it does.


EDIT: Holy crap I use that word a lot, especially given how little I understand. Fuck. I guess if I 
say it enough I hope it'll come true?

I am frankly sick of multithreading at this point, so, lets relax for today. I looked up someone's 21 sticks game, then modified it to make it prettier. 21 sticks is a game with 2 players, where, oddly enough, there are 21 sticks. Each player then takes turns removing either 1 or 2 sticks from the pile until none are left. The player to take the last stick(s) is the loser.

Wednesday, 2 November 2011

Java: Shared Queue 4: Complete Test + Source Code

Gah, late thread, sorry! This one's a bit of a doozy, so bear with me! I'll start off with a brief overview:

Step 1: We need to actually create the thread-safe queue to use.
Step 2: Set up all the producers (don't run them yet, we don't want threads to start running before all are in place)
Step 3: Set up all the consumers. We can start them running now, since both producers and consumers are ready. Since the queues are as of yet, empty, this isn't an issue. Seeing an empty queue, the consumers will yield.
Step 4: Start the producers.
Step 5: Wait for the producers to finish. Once they are, no new values are going to be added to the queue.

Tuesday, 1 November 2011

Java: Shared Queue's 3: Testing Classes

Apparently, no one did shit, so I guess discussing that is outta the question :P So straight onto how I tested it! I'll put both the consumer and producer classes into an umbrella class just so its all together. I'll leave the actual running of the tests to tomorrow, since I'm a bum (or a tortoise?) and haven't actually like, got there yet. I mean, for your benefit, so we aren't moving too fast. Clearly.

I do have the actual classes to be used in the testing though, and here they are:

Monday, 31 October 2011

Java Threads: Shared Java Queue 2: Queue Harder

Sorry, couldn't help myself with the title.

Anyway, just a short post today, and that's because our Synchronized Queue object basically already conforms to the Queue objects (or rather the interface). Its methods are monitored forwarding methods. That is, they deal with all the necessary object locking (synchronize), but that's it. Beyond that, they just call the methods of the actual Queue.
TL;DR: It's now up to actual Queues to be doing this right. We've already protected everything with our wrapper class.

Sunday, 30 October 2011

Java: MultiThreading Example 2: A Shared Queue

In response to one of the comments: Yes. I was either going to go to html after we were done with making a simple game or 2, or to python. Python really should have been done first, its a much nicer language, but...I didn't! So there.

Back to another Java example, until this whole multithreading thing is understood properly, I looked at the dumbass book again, but I found something that looked plausible: A shared queue. I mentioned this when going over potential difficulties you may encounter without synchronizing, so this seems like a great way to learn how to make something thread-safe. On top of that, there'll be flipping between threads, unlike the clock program!

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!

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

Wednesday, 26 October 2011

Java: A Clock Example

Well, I may have mentioned before that the book was a load of shit when it came to threads, after all, and it's completely thrown me off how to proceed! The code we have is essentially useless because of this, but I have been looking into it.

It turns out that having a class use "implements Runnable" instead of extends Thread is the preferable way to do things like this. The class is then not an extension of a thread, but can be run by one!

I've actually completed my clock, but it's not very pretty, and annoying to understand. Here's the file if you want to look at it, though. I'll save the actual code and source for when it's not as confusing a mess as it currently is :D

Monday, 24 October 2011

Java: Thread Scheduling & an Example (Part 1)

What's up? We're going to go right into the last bit of technical information before an example program, since those are frankly easier to learn from (for me, at least!).

Thread Scheduling
A program that uses many threads needs to be subject to some set of rules to determine which thread should be running at any one time, and how to switch between threads. Without this switching around (scheduling), there's no real reason to use threads in the first place, since some threads will never have a chance to run.
Because of this, threads are given priority levels (higher priority runs before a lower priority thread).
TL;DR: Needs to be a way to flip between threads, and to prioritize them, otherwise, pointless.

Thursday, 20 October 2011

Java: TEXT EDITOR COMPLETE (sorta)

Woop, well we have everything in place, so the only things left to do are "New" and "Close", put them in a menu bar (along with everything else), and give them shortcuts! After today, we'll start work on Threads, which will allow us, hopefully, to work on some of the simpler games.

An issue I don't feel like working on is when you hit new/close/load, an option to save doesn't come up. Also there's no Save vs. Save As. But that's okay, since I wasn't planning on this replacing Notepad anyway, and its more of a matter of building it than learning anything new.

Here's the new and close methods:

Wednesday, 19 October 2011

Java: Completing the Text Editor Functionality II

As per the comments a couple days ago: Yes, we absolutely can get to simple games, but to do that, we need to learn about threads! So I'll do that right after this text editor is done! I suppose turn based games require it less, but still.

Okay, let's get to finishing this thing. Since load() was just a version of reading files, it follows that save will be a similar version of saving (durr) them. Keep in mind that our finished project will be very rudimentary, and you'll probably still be better off using notepad and stuff, but its still functional, right?
I changed a line in the code for load, when initializing the JFileChooser. It is identical to the initialized JFileChooser in the following:



Tuesday, 18 October 2011

Java: Completing the Text Editor Functionality I

Well, we have our text editor, which looks like:


Now, we need to add functionality to the buttons, but we want our methods of save/load etc. to be just that, their own methods, which the buttons call, so first, we need to actually make the methods, and have the action listeners come in later!

Let's go in order, starting with Load. This is some new material, so pay attention: JFileChooser is what we're using. This took me a while to figure out, too, so we`ll only be doing Load today, Save will be easier with this knowledge, so I can probably do the remaining 4 tomorrow!

Monday, 17 October 2011

Java: More GUI Stuff!

So it turns out, in discovering and using the Netbeans GUI creator, I skipped like, a bajillion pages in the textbook, which are all about bringing up buttons and setting up events, and the proper way to put things together (A jFrame being the bottom level, to hold everything, then a jPanel for each section on the Frame and so on.). So uh. I'm kinda out of the GUI chapter, save a couple of examples!

I guess we'll do those (its some relatively cool stuff), until someone in the comments section has a specific request, or we can just move on to multi-threading and such, which will be the final thing to learn about.

Anyway, for now, we can have a simple text editor! Let's start off by designing it, visually.

Sunday, 16 October 2011

Java: Core GUI Concepts III - Example Program (The easy-ish way: Functionality)

I feel that I should note this GUI builder isn't part of the textbook. I just looked at the generating of code and thought "Fuck this, there's gotta be a better way!" And then there was! Also, post on Sunday since I had that day-off earlier in the week.

Anyway, now that we have the visuals more or less done, we should work on functionality. First off, we want to get out of the design tab, and move into the code tab, like so:


Saturday, 15 October 2011

Java: Core GUI Concepts II - Example Program (The easy-ish way: Visuals)

Okay, I think I got this worked out! Let's have a shitload of screenshots!


Let's start off by making a new file in Netbeans. But instead of the regular Java file, scroll down to the Swing GUI Frame, then select JFrame. This will, predictably, give you a JFrame to work with. Fun times.

Friday, 14 October 2011

Java: Core GUI Concepts I - Example Program

Well, good to know that TL;DR is a hit! Moving on, GUIs themselves are complicated to teach beyond "Here's the libraries, get to it", so we'll start off with an example program! This should hopefully give us an idea of how certain aspects are used and whatnot.

The example we'll be using is just a window with a button. The button will toggle between 2 text messages, "Hello" and "Goodbye". It's surprisingly complex, so I'll only be doing this for example purposes, so you can see how the code works. Tomorrow, I'll be recreating the program the easy way (I hope), so don't fret if this is ridiculous. Here is the end result: