Register new account
Edit account
Search

Ancient Domains Of Mystery, forum overview / General / Jan, a lil question about Java

Online users ( Unknown)
Application object not working properly at the moment, no clue who is online...

* Numbers in parentheses are the number of minutes since the user last loaded a page. Logged-in users time out after 40 minutes (unless they manually log out), lurkers and anonymous posters after 20.

Stas
Registered user
The Dark Lord Melkor


Last page view:

7659 days, 4 hours, 13 minutes and 41 seconds ago.
Posted on Monday, April 15, 2002 at 07:04 (GMT -5)

how can I write a file, which contains a single class? and vice-versa: open a file, which contains the class, and then make a class from the file?


"Do not meddle in the affairs of wizards, for they are subtle and quick to anger"
Lord of The Rings

"Do not meddle in the affairs of cats, for they are subtle and will anger the cat lord"

Portrait
Jan Erik
Administrator

Last page view:

4 days, 23 hours, 2 minutes and 34 seconds ago.
Posted on Monday, April 15, 2002 at 20:57 (GMT -5)

Not quite sure I got that...

I'll try anyway:

To write a java file containing a class just use the regular:
import <whatever you need>;

public class <classname>
{
     // methods and variables
}


To make a class file out of it just compile it with the javac program...

If you where refering on how to make use of a seperate class file in another prgram just make sure the class files are in the same folder and you can use it to either create a new object based on the class:

<classname> MyObject = new <classname>();

or use any static methods from that class like this:

<classname>.<some static method>();
Of corse if you want to use a number of classes in a project you ought to make a package...

I'm pretty sure I missed the target completely here, so try to make it a little more clear what you mean and I'll try to answer better :)


Jan Erik Mydland
HoF admin
Stas
Registered user
The Dark Lord Melkor


Last page view:

7659 days, 4 hours, 13 minutes and 41 seconds ago.
Posted on Tuesday, April 16, 2002 at 00:55 (GMT -5)

yes:
I have a class GameWorld. I make an instance of it (GameWorld theGameWorld = new GameWorld()). How do I save the class theGameWorld on a disc? in C/C++, there is a 'fwrite' function, but in Java it seems to accept only character array as argument... (ie how do I write a data file?)
and how do I read the data file? open the file, and then assign it's data to the class object...

I'm not sure whether this was clear enough :)


"Do not meddle in the affairs of wizards, for they are subtle and quick to anger"
Lord of The Rings

"Do not meddle in the affairs of cats, for they are subtle and will anger the cat lord"

Portrait
Jan Erik
Administrator

Last page view:

4 days, 23 hours, 2 minutes and 34 seconds ago.
Posted on Tuesday, April 16, 2002 at 18:07 (GMT -5)

Ah, ok, in Java that is called Serializing.

Providing your class meet scertain criterea, it can't among other things contain sub-objects that are not themselves serializable (Image objects for example) (read the doc for java.io.Serializable for details) and implement the java.io.Serializable interface you can read and write Java objects to file and network streams using java.io.ObjectInputStream and java.io.ObjectOutputStream.

You can for example save your GameWorld object to disk with the following code (provided it's Serializable (you'll get a NotSerializableException if not)):
try
{
	GameWorld theGameWorld = new GameWorld();

	/* some code that manipulate the object (no point in saving
	a newly created "blank" object after all) */

	FileOutputStream ostream = new FileOutputStream("t.tmp");
	ObjectOutputStream p = new ObjectOutputStream(ostream);

	p.writeObject(theGameWorld);

	p.flush();
	ostream.close();
}
catch(IOException e)
{
	e.printStackTrace();
}
catch(ClassNotFoundException e)
{
	e.printStackTrace();
}

You can then restore the object at a later date using this code:
try
{
	FileInputStream istream = new FileInputStream("t.tmp");
	ObjectInputStream p = new ObjectInputStream(istream);

	GameWorld theGameWorld = (GameWorld)p.readObject();

	istream.close();
}
catch(IOException e)
{
	e.printStackTrace();
}
catch(ClassNotFoundException e)
{
	e.printStackTrace();
}

Note that this saves quite a bit of meta-data about the object as well as it's data fields, so it takes up more space than having the object just save the raw data (not a major problem though, look at the resulting file to see what I mean), but it's a lot more conveient seeng as you don't have to write your own save() and load() (or whatever) methods for the class.

If you do want to save just bits of data rather than the entire object you can pretty much re-use the code abowe, just use a different Stream object and use writeInteger() readInteger() or whatever...

Hope that covers it.
Jan Erik Mydland
HoF admin

[Edited 1 time, last edit on 4/16/2002 at 18:09 (GMT -5) by Jan Erik]
Stas
Registered user
The Dark Lord Melkor


Last page view:

7659 days, 4 hours, 13 minutes and 41 seconds ago.
Posted on Wednesday, April 17, 2002 at 00:48 (GMT -5)

thanks. :)

hm, I have a three-dimensional array of classes (BigMap[][][] bigMap;), and each BigMap class contains a three-dimensional array of classes (MapSquare[][][] mapSquare;), and then initializes them... I tried with integers like 100 (BigMap[100][100][100] (!!)), but my PC almost died for 5 mins, so maybe for a moment I use smaller maps ;)

why using three-dimensional maps? the game will be thus very realistic (and heavy :), and some things can be more easily done... althought the vision code will be a mess (probably) :)


"Do not meddle in the affairs of wizards, for they are subtle and quick to anger"
Lord of The Rings

"Do not meddle in the affairs of cats, for they are subtle and will anger the cat lord"


Color mixer:
Red: Green: Blue: HTML color code: result:      
Your Name: Check to login:

Your Message:


Read the
formating help
Are you a spambot? Yes No Maybe Huh?
Create poll? Yes No   What is this?
Poll question: