PDA

View Full Version : The "Who Wants To Learn Java" Project


Dru Lee Parsec
03-19-2002, 02:36 PM
OK, here's my idea. WHO WANTS TO LEARN JAVA?

Let's come up with a small programming project that the coder forum gang can write together. We can post our code snippets here, Or, I could host a small web page for the project on my own web site.

We can use KMJ's and The Linux Duck's www.codeexamples.org site for examples of how to write basic Java GUI. And I can help out by answering specific questions.

What shall we build? I have an idea but I think I'll wait to see what kind of response this thread gets first. I don't want to stifle creativity by suggesting an idea first. If I suggest something it might guide people down a particular path and I'd like to do something new and exciting.

So make a suggestion for a programming project. But remember that it should:

A: Be a reasonable request for people learning Java.
B: The people working on it will probably have coding experience, but possibly not Java experience.
C: It should be no more complex than a typical final project you would receive at school.

If you have an idea but don't know how to implement it in Java that's OK! I'll see waht I can do to guide everyone in the right direction.

Oh, and I'll help write the code if you want, but if you'd rather have me as a guide rather than as a developer then that's OK as well.

Let's Get Creative!
:D :goodjob:

kmj
03-19-2002, 04:24 PM
This sounds like a great idea; unfortunately, I probably won't have time to work on it due to school.

My first suggestion is the game Othello: I like this because it could be as simple as a two-player "hotseat" (take turns) game, you can add an AI (not too hard for this game) to play against the computer, you can add networking for multiplayer games; and for a game like this it's not hard to make a good GUI (and everyone loves to play with Swing, right?). So that's my suggestion.

TheLinuxDuck
03-19-2002, 05:28 PM
I've already got too many irons in the fire. I need to get a refresher in java , cuz I haven't used it in some time.. but not right now.. (^=

I'd like to know what you're going to do, though... the othello thing sounds qool.. or maybe that card game you (Dru) were going to originally write..

Dru Lee Parsec
03-19-2002, 05:45 PM
Ooh, Othello, I like it.

Wanna do that? I think the AI for it would be easy. Simply try every empty square that has a non-empty adjacent square and calculate how many "disk" that move would collect. Once you've calculated this for each possible move select the move that collects the most disk.

How big should the board be?

Who wants to write the GUI to make the board appear?
I think all we need is a JFrame with the board in it and a "New Game" button at the bottom. Should be simple.

Duck! It's good to see you here. :D I'm sure you could find a couple of hours to help out. I don't think Othello would take more than 10 man-hours to build.

Perfect! Good suggestion. And if we get more suggestions we could just make this the first in a continuing fun bit of community coding.

TheLinuxDuck
03-20-2002, 12:51 PM
Dru: I'd be more willing, at this point, to making any graphics needed for it. I can handle that.. (^=

Dru Lee Parsec
03-20-2002, 01:24 PM
[quote]Dru: I'd be more willing, at this point, to making any graphics needed for it.[quote]

I think we would need:
1. A white disk
2. A black disk
3. A series of images of the white disk turning over to become a black disk.

I can use the same series of images in reverse to show a black disk turning over to become a white disk.

OK, now for the design. Here's what I would suggest:

1. Use the standard code form www.codeexamples.org to produce a JFrame on the screen.

2. Add a panel to the JFrame with a BorderLayout. That panel will have two more panels added to it. The board will be added to BorderLayout.CENTER and the "New Game" button will be added to BorderLayout.SOUTH

3. Create a class that extends JButton. This class knows what it's state is. (No disk, black disk, white disk) it knows how to reset itself, and it knows how to display the disk properly based on it's status. It would also have to know how to set itself from blank to blak, or blank to white AND it would have a flip method (these could be the same method)

4. Assuming the board is 10 x 10 we would make a 10 x 10 array of these buttons and then add the buttons to a 10x10 GridLayout panel. That panel would be the board.

That would give us a GUI. The logic would be the next step.

Does anyone want to volenteer for any part of the code?

Does anyone want to volenteer ME to do any part of the code? :)

And if anyone other than KMJ and The Duck want to join in then you're more than welcome to do so. (Nafae? Inkedman? Tminos?)
(Did Tminos join up yet? I chatted with him last night about Coder Forums)

We're ramping up. This could be fun. :)

kmj
03-20-2002, 02:37 PM
with your permission, dru, I'd like to extend the "community java project" invitation to LNO...

inkedmn
03-20-2002, 02:44 PM
Dru-

well, i don't know crap about java, but i'd be more than willing to give it a shot...

i might need some hand-holding though. :)

TheLinuxDuck
03-20-2002, 03:57 PM
Q: Does java support animated gifs? If not, I'll just put together an image series, and let the coderz sort it out. (^=

TheLinuxDuck
03-20-2002, 04:57 PM
http://www.codeexamples.org/tld/othello.html

Here is the first rev of images for the game. The piece doesn't actually flip, it's more of a fade.. but it's my first rev, so we'll go with it and see from there.

Dru Lee Parsec
03-20-2002, 05:31 PM
Does java support animated gifs?

Not that I know of. And although I'm not sure about 1.4 I know that up to 1.3 it doesn't support transparency very well.

I was thinking of just using a thread to change the images every 100 milliseconds or so until we've "flipped" the image.

kmj
03-20-2002, 09:05 PM
I could take a shot at doing the AI class.

imported_Dan
03-20-2002, 10:16 PM
I've been reading a book on java, but have problems coming up with projects to test myself with. As such, I would love to get in on this, though I would most likely require someone to hold my hand through all of it. Anywho, let me know where I should go! :)

JimCamel
03-21-2002, 03:59 AM
I had to learn Java for first year computer science at uni, and so know a little. If you need some help I'm in.
Jim

Dru Lee Parsec
03-21-2002, 12:50 PM
OK, This is going to be fun. For those of you who are just getting started and are just learning Java here's what I would suggest:

Go here: http://java.sun.com/j2se/1.4/download.html
and download the SDK and install it. If you install it to (For Example) C:\JDK1.4.0 then you will need to add C:\JDK1.4.0\bin to your path.

Then you need to set up a directory where you're going to do your Java work (Mine is D:\Dev_Projects\myjava ). Set up a new environment variable called CLASSPATH that points to D:\Dev_Projects\myjava (or wherever your working directory is)

Then, go to www.codeexamples.org and check out the Java code examples. Copy them in to a file in yoru working directory. Here's some tips:

The file name MUST be the same as the name of the public class in the file (Including upper and lower case) So if the public class name is HelloWorld then it MUST be saved in a file called HelloWorld.java

Compile the file with the javac command like this:

javac HelloWorld.java

This will create one or more .class files

Run the class file like this:

java HelloWorld

Notice that it is NOT java HelloWorld.class This little inconsistency in Java beats up newbies all the time.

If you can compile and run several of the code examples then you're set. I tried to add as many comments as I could to the code examples to explain things as well as I could.

Try to get that far and let me know what issues you run into.

kmj
03-22-2002, 09:35 AM
Hey, dru, can you tell me how Java 1.4 differs from 1.3? And can you tell me if they'll ever stop with this "Java 2 1.x" nonsense? Why not just java 2.3, or java 2.4.... too many numbers in my head already! :)

Dru Lee Parsec
03-22-2002, 06:17 PM
Oh man, that's complex.

When they moved from version 1.1.8 to 1.2 they had several beta releases and then 4 release candidates. Between release candidate 3 and 4 the Sun Marketing drones decided to call this new release "Java 2" because there was so much that is different between 1.1.x and 1.2.x Hence the name "Java 2"

However, the difference between 1.1 and 1.2 is WAY bigger than the difference between 1.2 and 1.3/1.4 The biggest difference is speed, bug fixes, and the addition of regex to Java in 1.4

Also, I know you remember the post I made over at LNO describing several of the cool new things in Java 1.4 (that's the post that spawned JF's rant against me and my subsequent departure from LNO). In any case, that post listed a few of the other things that are new in 1.4

For this project I think 1.3 or 1.4 would be fine. I say 1.3 instead of 1.2 because 1.3 supports cross platform MIDI implementation. If we want to add music to the game we might want to just plug in a MIDI file to our app.

Also, 1.4 has some serious Swing bug fixes that I need in order to run my editor (Jext) in Linux. 1.4 just seems to run a lot smoother in Linux. The fonts look better, the mouse behavior is fixed, etc.

And since 1.4 is now available for free I just suggested 1.4 as the base line we could all use. Even though we wont use 90% of what 1.4 allows us to do. :-)

Hope that helps

Dru Lee Parsec
03-22-2002, 09:07 PM
OK, I've got a basic GUI up and running. There is a VERY quick and dirty web page for this project at http://www.brouelette.com/othello.html

To compile it you must download and install ANT
http://jakarta.apache.org/ant/index.html

The install instructions are pretty simple. They come down to these steps:

1. Install Ant.
2. Make an environment variable called ANT_HOME that points to the directory where you installed ant
3. Make an environment variable called JAVA_HOME that points to the directory where you installed your java development kit
4. Under the directory where you installed ANT there is a bin directory. Add that directory to your path.


When you unzip the othello.zip file it will create an Othello directory. To compile the code go to that othello directory and type

ant

To run the program simply add othello/lib to your classpath and type

go.bat

or just

go

In Linux rename the go.bat file to go and make it executable.


I knocked this out REALLY quick so there are hardly any comments in the code. But it should be pretty self explainatory.

I gotta go, so have fun with it. We'll keep in touch this weekend .

Duck! Can you email me a zip file with the images in it?

<anti spam email>
druleeparsec AT users DOT sourceforge DOT net
</anti spam email>

Thanks

kmj
03-23-2002, 02:24 PM
I won't be able to take a look at that until monday, but I have been thinking of a rough-draft algorithm for the ai...

Dru Lee Parsec
03-25-2002, 09:00 PM
Since the GUI stuff always seems tough for people to understand I've written some more of the GUI for Othello.

I added in The Duck's graphics. I also made it so you can click on cells to add a new tile. It also resets the game when the New Game buttonis pressed. There is a bug I need to fix where the graphics show up when run from the classes in the lib directory, but not when you try to run the jar file. (Easy fix, I just need to get around to it)

So here's where I would suggest we go from here (meaning you write the code, not me ;) )

1. You can now click on any open square. But in Othello you can only click on a square that is adjacent to a tile of the opposite color AND if that move would cause tiles to be flipped.

Make it so only legal moves will occur.

2. When a legal move occurs flip the appropriate tiles.

Have fun. The new code is at http://www.brouelette.com/othello.html

kmj
03-28-2002, 01:11 PM
Um, anyone else out there? I can do it, but I don't need to learn java... I already know it!!! :)

Dru Lee Parsec
03-28-2002, 06:59 PM
I know, I've been waiting for the same thing. It's been a week since I put up the last update.

Doesn't anybody want to learn Java? :(

If I don't hear anything in teh next few days I'll just knock it out in an hour or so and post it so we can have the game to play.

kmj
03-28-2002, 11:09 PM
that'd be cool :) I've had my head in moobot all week... (well, that and school and work and housesitting and... etc)

Strike
04-02-2002, 08:06 PM
Hrm, I get the following:


[ddipaolo@quinn ~/src/Othello]% ant
Buildfile: build.xml

init:

prepare:

images:
[echo] Copying images

compile:
[javac] Modern compiler is not available - using classic compiler
[javac] Compiling 5 source files to /home/ddipaolo/src/Othello/lib

BUILD FAILED

/home/ddipaolo/src/Othello/build.xml:51: Cannot use classic compiler, as it is not available A common solution is to set the environment variable JAVA_HOME to your jdk directory.

Total time: 6 seconds


I have Java SDK 1.3.1 ...

Strike
04-02-2002, 08:07 PM
Also, I am not sure what would be considered my "jdk" directory ... what exactly is it looking for?

Dru Lee Parsec
04-02-2002, 08:14 PM
Ah! there are 2 things to check:

1. Do you have the Java Development Kit or the Java Runtime Environment? If you have a javac program in your jdi1.3.1/bin directory then you have the JDK (which is what you need) if not, then you only have the JRE (which is what you need to run, but not compile java programs).

2. You need to set up an environment variable called JAVA_HOME which points to your installation directory. In my case it would be C:\jdk1.4.0 in windows and /usr/share/java/jdk1.4.0 on my linux box.

Finally, you should have your JAVA_HOME/bin directory in your path. But I think it's probably just the lack of a JAVA_HOME variable that's beating you up .

Hope it helps

Edit:

I just realized that you said you have the SDK. The SDK is the same thing as the JDK. So you probably have the right installation. It's just the JAVA_HOME thing then.

Strike
04-02-2002, 09:40 PM
Sweet, it was just setting JAVA_HOME to /usr/lib/j2sdk1.3 (debian Blackdown JDK users take note :))

Strike
04-03-2002, 12:35 AM
Hrm, that made it compile but ... I get this when I run it:


[ddipaolo@quinn ~/src/Othello]% `cat go.bat`
Exception in thread "main" java.lang.NoClassDefFoundError: com/codeforums/othello/Othello

[ddipaolo@quinn ~/src/Othello]% echo $CLASSPATH
/home/ddipaolo/src/Othello/src


[ddipaolo@quinn ~/src/Othello]% ls -l `echo $CLASSPATH`
total 4
drwxr-x--- 3 ddipaolo ddipaolo 4096 Mar 22 15:33 com

kmj
04-03-2002, 09:44 AM
I'm getting the same thing, Strike, on NT.

kmj
04-03-2002, 09:48 AM
Strike; try dropping down into the jar directory and just doing java -jar othello.jar ... seemed to work for me.

kmj
04-03-2002, 09:51 AM
hmm. except TLD's images aren't showing up, so the application doesn't appear to do anything... (since I assume the appearence hinges on the images)... so maybe this didn't work around the problem.

kmj
04-03-2002, 10:05 AM
duh, yet again, kind forkin moron saves the day. I didn't even notice jakarta created a lib file for me... go.bat is in 'othello', so having 'othello/lib' in my classpath didn't help me... but when the classpath is just 'lib', it works fine. Now, I've got the program running properly.

kmj
04-03-2002, 10:07 AM
Dru, instead of having to set your classpath, can you just add '-cp lib' to go.bat? Seems like that'd be a little more convenient.

Dru Lee Parsec
04-03-2002, 12:53 PM
Yeah, you could. Especially because the ant build.xml sets the classpath for the compile.

So -cp ./lib would work fine.

TheLinuxDuck
04-03-2002, 05:27 PM
Ok, maybe I'm missing something, too, but I'm also getting the same error that Strike last reported. I've got JAVA_HOME set correctly.. or.. is JAVA_HOME the dir where java was installed, or the dir where the files and stuff I'm working on are at?

When I go into the src.com.codeforums.othello, and try to compile it, I get this error:
Othello.java:9: package com.codeforums.othello.gui does not exist

If I run it like kmj said with the jar file, I get like he said, and no images. If I add the "-cp lib" into the go.bat, I get the error that Strike mentioned.

I haven't ever messed with packages or anything so this is weird, and, it's also been months and months since I last touched java.

<weakling>Help!!!??!</weakling>

kmj
04-03-2002, 09:10 PM
Did you use ant to compile it, as per Dru's instructrions?

JAVA_HOME is the directory java was installed to.

TheLinuxDuck
04-04-2002, 10:02 AM
No, I didn't even see that portion of his post.. (^= I skim through stuff sometimes, and miss alot..

Ok, so I installed ant, I added the ANT_HOME and JAVA_HOME, as well as CLASSPATH pointing to my build dir (which is c:\java). I used ant to compile the source, and then added "-cp lib" to the go.bat file, and then it and I still get this error.
Exception in thread "main" java.lang.NoClassDefFoundError: com/codeforums/othello/Othello

So, I'm still no better off. What exactly is ant? Why do I need it? What benefit does it have? Why not just compile source via javac and be done with it? Is it sorta like Makefile/make for C/C++?

Yea, I know, I'm a loser.

kmj
04-04-2002, 10:11 AM
ah, but this is a different error. Didn't read the info about it? It's like make except it's spell a-n-t, so it isn't make at all really.

1) Dru mentioned you should use './lib', since you're using lunix.

b) Try (for sheets and goggles) going into your othello/lib directory (which ant created) and just running "java com.codeforums.othello.Othello"

TheLinuxDuck
04-04-2002, 10:25 AM
I'm using linux? Weird.. I thought c:\java and the dos prompt were MS things.. (^=

::slapping self::

Let me try the sheet and goggle thing.

D'oh! Same error. ::sigh::

TheLinuxDuck
04-04-2002, 10:46 AM
Ok, so I'm a stoopeeed.

I didn't format the "-cp lib" addition in go.bat correctly. I put it at the end of the line.. this time, I put it in between "java" and the other stuff, and it worked.

Ok, so w00t and stuff.

kmj
04-04-2002, 10:46 AM
the line should read

java -cp lib com.codeforums.othello.Othello

or

java -cp ./lib com.codeforums.othello.Othello

TheLinuxDuck
04-04-2002, 11:07 AM
Yea.. heh.. I fingered that out after chatting with you in IM.

You might even go so far as to say that I am a "big fat headed dummy"... but, I suppose that is up to you.

Dru Lee Parsec
04-04-2002, 11:51 AM
Wow you guys have been busy while I was away last night.

Duck, sorry for my absence, let me make it up to you with what I hope are helpful answers. :)

"What is Ant. . . benefits".

Ant is a build tool similer to "make" however, unlike "make" it does not rely on a shell script for it to do complex builds. Since it's written in Java and it's not linked to a shell script it is a cross platform way to build a project. I thought this would be good since some of us are on Linux and some are on Windows. Also, it automatically figures out file dependencies so you doh't have to write a makefile that says "compile these first and then compile those".

JAVA_HOME: If you installed to C:\JDK1.4.0 then that should be your JAVA_HOME. Do not make the mistake of setting it to C:\JDK1.4.0\bin HOWEVER, your path should have C:\JDK1.4.0\bin added to it so you have access to javac, appletviewer, etc.

java.lang.NoClassDefFoundError: OK, first run ant and make sure it compiles. Check to see if you have a set of files with a *.class extension created under your lib directory. If so then you have three options to make go.bat run.

1. From the same directory that you ran "ant" type:
java -cp ./lib com.codeforums.othello.Othello

2. Set a new environment variable called CLASSPATH that points to the lib directory and then run it as
java com.codeforums.othello.Othello

3. Secret technique: Inside my build.xml file I have a "secret" target. Do this:

ant jar

then go to the newly created ./jar file and you'll see a file called othello.jar In that directory type

java -jar othello.jar

Let me know if these still don't work. Good luck Buddy.

TheLinuxDuck
04-04-2002, 12:57 PM
With some help from kmj, I was able to solve my dilemma.. (^= When I added the -cd lib into the go.bat, I added it at the end of the line... I then changed it, per kmj's comments, and put it between "java" and the other stuff, and it worked fine.

And, I will say that I think the images I did look stupid.

So, other than that. (^=

kmj
04-04-2002, 02:50 PM
that's -cp lib, duck. (c:


They look like oreo cookies to me; I get hungry when I run it.

Dru Lee Parsec
04-04-2002, 03:02 PM
But the important part is that it's up and running, and that you have ant working (ant == Another Neat Tool)! :)

So, I think the next step is to add a check to see if the button you clicked on is a valid button for the current player to click.

Then, once we know it's a valid button, we add the code that flips the appropriate tiles.

THEN, we add AI.

It's starting to become fun. And it's not that far from being done.

L-Duck, If you want to change the graphics then just put them in the images directory and use the same file names (black.jpg and white.jpg) When you recompile and re-jar they will show up.

I'll be on coderforums on and off throughout the day. Let me know if you have any more questions. Glad you're up and running. ;)

TheLinuxDuck
04-04-2002, 03:03 PM
Yea..yea..yea.. cp, cd.. it all ends up in the same place... err.. wait.. that's food.

Anyway, if anyone else thinks the images are stupid, let me know and I'll redo them.

TheLinuxDuck
04-04-2002, 03:05 PM
OK, so, is the player black or white? Should we put in a choice and let the player decide?

TheLinuxDuck
04-04-2002, 05:15 PM
Dru:

I made a modification to the source. I dunno if you want to keep it, or what. It's nothing mission critical, but I wanted to try to do something I knew I could do, to get my brain back into it a little bit..

I added a quit button to the button bar at the bottom. It basically calls a dispose on the main frame (which I had to pass into the OthelloMainpanel class).

If you don't want it in there, that's fine.. (^= But, if so, what do I do with the modified source?

Dru Lee Parsec
04-04-2002, 05:21 PM
OK, so, is the player black or white?

I set it up such that White has the first move. In the OthelloBrain class I've set the default value for currentPlayer = OthelloButton.WHITE; When a button is clicked the button sends it's x,y location back to the OthelloBrain class via the playerMoved method. All that method does right now is keep track of alternating colors.

To make the program test to see if the button clicked is a valid move I would do something like this:

In the OthelloButton look at the code where you add the action listener. I'd suggest doing something like this:

addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(brain.isValidMove(row,column){
int currentColor = brain.getCurrentPlayer();
int success = clickAction(currentColor);
if (success == 1) {
brain.playerMoved(row, column, currentColor);
}
}
}
});


You'd have to add the isValidMove(int row,int column) method to the OthelloBrain. The OthelloBrain knows which player has the current move, and it knows the row and column of the button that's trying to make a move. All it has to do is get access to the button array so it can check the status of the buttons around the button that was clicked.

One way to do that is to build a new OthelloBrain constructor that passes the ButtonArray that was created in OthelloMainPanel to the OthelloBrain. Or, you could just have a setArray method that builds the array and then just hands it to the OthelloBrain class.

So, adding these methods should get you pretty far into doing a valid move check.

Hope it helps.

DLP

P.S. L-Duck. You're new Avatar icon cracks me up. :D

Dru Lee Parsec
04-08-2002, 12:35 PM
BTW, just email me the source changes and I'll merge them and put them on the web site.

druleeparsec AT users DOT sourceforge DOT net

TheLinuxDuck
04-08-2002, 03:00 PM
Dru:

How do I go about emailing you just the source changes? Does ant do something funky or something that I need to know about?

Dru Lee Parsec
04-08-2002, 04:18 PM
If you want to zip up (or tar up) the src directory and send me that then that sould be fine. Everything under the src directory is the source code. ANT compiles it to class files and places it under the lib directory.

So all I need is the src directory and I'll compile it to make new class files.

Dru Lee Parsec
05-07-2002, 06:02 PM
bump

kmj
05-31-2002, 03:24 PM
..moving to our new Projects forum :)