• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Java Newbie needs guidance

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone this is my first post in this forum.

I would like to create a very simple random terrain generator - a top down view.

1.the user inputs size into a gui (4x4 or 8x8)
2.a new window opens with the appropriate grid size.
3.the java program randomizes the position of 2 tile types
a) grass (color green) b) sea (color blue)

I have google searched, am reading java books, have been given code, and have not found much source code on the web ( 1 or 2 maybe).

however, any code i have found, and used, does not work - many errors appear. here is an example of code ive been given (which doesnt work)

"public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.BLACK);
for (int i=0;i<squarecount-1;i++){
g.drawLine(i*getSize().width/squarecount,0,i*getSize().width/squarecount,getSize().height);
g.drawLine(0,i*getSize().height/squarecount,getSize().width,i*getSize().height/squarecount);
}
}
"

i have both "jbuilder foundation x", and the latest "eclipse" software. I am used to jbuilder.

My request is part of a much much larger project eg a 128x128 square tiled random map generator that uses AI to create realistic cities, based on a randomly generated terrain. As such, i am not asking for the solution of my problem, I am asking (pleading lol) for the solution to an extremely basic prototype for the terrain map. Even though i have 8 months to do this, im at my wits end!

Thankyou for reading this post and i hope you can help me in anyway you can!
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you need to break down what you want into smaller packages.

What were you expecting from the programs you downloaded and what did you receive?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mister,

First, a bit of business: you may not have read our naming policy on the way in. It reqiures that you use a full, real (sounding) first and last name for your display name. Titles like "Mr." aren't enough. You can change your display name here.

Now, as to your question: there's nothing overtly wrong with the paint() method you've shown; the only problem is that it's a single method that's not shown together with the class it would have to be embedded in. The paint method would have to be a part of a subclass of java.awt.Component, and that class would have to define an integer variable named "squarecount".

Does this help? Were you trying to compile this method all by itself? If so, then I think you're trying to put the cart before the horse -- don't try to write a program like this before you get a feel for the fundamentals of the Java language. There are plenty of excellent books for Java beginners, or you could look at Sun's online Java tutorial -- If I were you, I'd work through all the trails under "Trails Covering the Basics" and then look at your problem again.
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
firstly id like to thank the posters for reading and replying to my posts (Ernest have you noticed my new name :-) ).

Ok, helpful adivce has been given to me, and ill work through the tutorials today. I have used java before e.g. to produce a simple program related to a customer purchasing goods, but this was a while back. I guess im now a rusty newbie!

I really dont want to come across as somebody who wants a quick fix and requires an immediate answer - ive scheduled 8 months to complete my project, and currently have requirements, specifications and a paper-based prototype. I think what i am really lacking at the moment is a strong knowledge of Java and grapics. These tutorials will no doubt be a help.

If anyone else has the time, any other help would be useful. For example, what keywords should i look for - to find tutorials and the such. Examples of keywords ive typed are "java terrain generator" and "java tile based 2d".
Alot of the findings are of 3d fractal landscapes. (My final product will hopefully use a simple top-down tile based system with perlin noise).

An example of simple help i could be given could be as follows:
-type of java file to open with new project eg awt
-syntax used within jbuilder for tiles (ie a single phrase that can be looked up and studied)
-the best way to "identify tiles" eg assign a color (0-255)
-Do i first identify the whole grid, followed by identifying the 2 tile types?
-Do i have to identify the "blank" tiles in the 8x8 grid.

a simple theory is to give each blank tile in the 8x8 grid a name eg A1/A2, and name "green tile" G, and "blue tile" B, and to code so that A1/A2 has either G or B. Im not sure if i actually place a new tile on the old tile, or simply change the background color as appropriate, or assign a similar sized art file.

(when i produce an 8x8 grid that randomly places two tiles eg green and blue, my next step is to have 8 different tile types, that uses AI (some might say CSP) to realisticly place these tiles. eg blue will not be found touching brown - This is my 2nd step as such, and am not working on it with java until the first is done. From what i have seen, tile edges are assigned numbers - 1/2/3/4)

so overall any "keywords" that will allow me to look in the right direction would be great. Or any tried and tested methods i could look into. Please note, i do read books :-)
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a simple demostration program, to give you a few ideas. I use the JButton class to represent your 'Tile' concept. The GridLayout class to represent the grid. You will need to work on the random placement of tiles.
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nigel browne, Thankyou very much for your time and help. ITs the sort of thing im looking for - a simple example that will allow me (with effort) to essentially create what i am after.

im off to work now but when i get back ill definately have a good look at it. Hopefully whenever i have made my 2d terrain generator i can find a good place to post it, to help other people.

Thanks again, and ill keep this thread updated with my successes and failures (and questions lol)
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nigel Browne, i typed out your example word for word using jbuilder.

To do this i made a "new project", then a "new file".
The code was then inserted into the blank page.

In the code viewer, there are no errors (ie no exclaimation marks, underlines etc)

However, when i click "run", i have to choose a "Runtime configuration".
This consists of
"type:application"
"Main class i have to choose)"
"VM parameters blank)"
"Application parameters blank)"

Now, to run the application, i have to choose "Main class".
Whatever class i have chosen, i get errors. These are the errors.

1)java - Could not find main class, program will exit. (java.lang.NoClassDefFoundError: java)

2)java.applet - Could not find main class, program will exit.
(java.lang.NoClassDefFoundError: java/applet)

these repeats for java.awt/javaxswing.. in fact anything i pick comes up with the same error.

Now, if i change "type" from "application" to "Applet" i get these errors
for java/java.applet/java.awt/java.awt.event/javaxswing etc

"the class java/java.applet.java.awt/etc does not exist."

have you any ideas on how to get the code to work?

the project is saved as .jpx, and the file .java.
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i got it figured - instead of "new project" i go to "new" "application".

this code, found at the end -

"gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gd.setTitle("Grid Demo");
gd.pack();
gd.setVisible(true);"

is underlined and isnt recognised ("cannot resolve symbol").
however if i delete all of it, it works.

The window opens to size 0 so i have to manually enlarge it. theres also no title.

Anyway, i am very confident that i can rework it to make it work in jbuilder, so again i thank you for help. It should be easy for me now to create a large grid.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This was added to JFrame in the 1.3? release of java(I think)
Do you know which JDK your Jbuilder compiler is using?
[ October 15, 2004: Message edited by: Nigel Browne ]
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have the latest release for everything - i downloaded latest versions from the sites.

does this code (+comment) do the same task?

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyway heres a simple yes or no question.

Using the method of buttons for tiles (or panels), is it possible for me to:

1) Set the background as a picture (eg. grass)
2) Set on top of a "grass" colored tile another picture eg a black house
3) Assign different values to the tiles eg a 1 - 10 rating.

These 3 i am happy enough to plod along and do myself through trial and error, but if it isnt possible to do these 3 things, id best know now :-)
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William White:
i have the latest release for everything - i downloaded latest versions from the sites.

does this code (+comment) do the same task?

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);



You need to add a WindowListener to the JFrame to catch the WindowEvent. An example follows
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William White:
anyway heres a simple yes or no question.

Using the method of buttons for tiles (or panels), is it possible for me to:

1) Set the background as a picture (eg. grass)
2) Set on top of a "grass" colored tile another picture eg a black house
3) Assign different values to the tiles eg a 1 - 10 rating.

These 3 i am happy enough to plod along and do myself through trial and error, but if it isnt possible to do these 3 things, id best know now :-)



Question 1, Yes you can set an Icon as the image
Question 2, Sort of, If you set the background of a JButton to green you can add an Icon of a house. You are only allowed one Icon per button.
Question 3, Extend the JButton class and add any extra functionality you think necessary
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William, I've been reading this thread and it's interesting. I'm not trying to do the same sort of thing as you William, but I find that it helps me to watch you go through your problem solving. It's always good to see how other people attack a situation. Good luck and please keep updating your progress.

Nigel, you�re helping more than William here, thanks.
 
William White
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey chris, im glad that something im trying to do is interesting! I agree when you say Nigel is more helpful than me, but since im the newbie asking for help and hes the expert, that should be the way :-)

I have a tile grid set up and know exactly what to do (its all on paper).
My question about placing an image eg of a house, on an image of terrain eg grass, i think can be solved easy enough - i basically create two different images (1 with a house and one without) and display the appropriate one.

unfortunately, i think ive gone down the wrong path for my project. Basically, one of the components of a random city that the program will create is a network of roads. As far as i can tell, i will not be able to create a road network the size of the map, and then place it on top of the terrain + city. The only way to do this with my tile method, that i can see, is to create ALOT of images the size of a tile. Each image will have a road in it, but going at different angles. i would then have to make constraints so that the roads effectively join - Something in the line of tile number/tile edge number/image number etc. It seems a very long and boring process and im sure there is an alternative method that i cant find at the moment. I shall carry on browsing i guess :-) Anything i seem to find though is 3d fractals and perlin noise, which isnt 100% suitable, as graphics are not a big issue for the project.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic