Do you know about any "grid" java class?

I

i5513

Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.


I'd like, for example:

Grid grid= new Grid (640,480,3,4);
grid.BackGroundColor (RGB("red"));
grid.GridColor (RGB(blue));
grid.LineColor (RGB("yellow");

Where 640 is Grid's length, 480 is Grid's height.
3 is number of rows and 4 is number of column.
Three functions Color has obvious mission.

Once create Grid, I'd like:

grid.DisplayGrid ();
# Display Grid on screen
grid.Draw (3,4,3,5);
# Display a new line on Grid (from (3,4) to (3,5)), where start and
end are the center of (x,y).
grid.Remove (3,4,3,5);
# Remove line.

If I don't find this class, I'll develop it, but before I'd like to
know if I can derivate my class from others.

Thanks you!!
PD: (I'm a newbie in Java)
 
A

Andrew Thompson

i5513 said:
Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.

At first I though you might acheive this with a
GridLayout, but that made less sense when you
mention drawing lines from one grid to another.

You need either java.awt.Graphics or
java.awt.Graphics2D to acheive this
sort of rendering.

[ And no, I doubt anyone has developed a
class that you can extend that will save you
any work, but I could be utterly wrong. ]
 
M

Matthew Zimmer

Andrew said:
Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.


At first I though you might acheive this with a
GridLayout, but that made less sense when you
mention drawing lines from one grid to another.

You need either java.awt.Graphics or
java.awt.Graphics2D to acheive this
sort of rendering.

[ And no, I doubt anyone has developed a
class that you can extend that will save you
any work, but I could be utterly wrong. ]

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site

You could probably use GridLayout to control where and how many grid
objects you have. However, you'll have to create a custom Component in
order to draw the lines. What you can do is put your custom component
in each grid cell with nothing painted. Then, when you want to draw in
the grid between two cells, just paint half of the line between them in
each one from the midpoint to the edge. The math for that is pretty
simple since all you have to do is getSize on the component and then
calculate the midpoint from that.
 
A

Alex Hunsley

i5513 said:
Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.


I'd like, for example:

Grid grid= new Grid (640,480,3,4);
grid.BackGroundColor (RGB("red"));
grid.GridColor (RGB(blue));
grid.LineColor (RGB("yellow");

Where 640 is Grid's length, 480 is Grid's height.
3 is number of rows and 4 is number of column.
Three functions Color has obvious mission.
[snip]

Your chances of finding this particular thing already written are pretty
slim. Doesn't sound like a lot of work to get it written though.
I'd sublcass a JComponent (e.g. JPanel) and it give it the ability to
draw such things.
 
D

Danny Woods

Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.

One possible solution would be to use a JTable. You'd have to create a
custom CellRenderer, but this is fairly trivial. The model for the table
could have a Vector of Stacks: repainting would just amount to peeking at the
top of the Stack to get that particular tile to paint itself (this allows
you to stack tiles on a given location).

It's a bit different from the other suggestions, but the JTable takes care
of a lot of the hassle. Moving a tile would be a simple matter of popping
the stack at the given location, and pushing onto the stack at the target.

Just a thought.

Regards,

Danny.
 
I

i5513

Andrew Thompson said:
i5513 said:
Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.

At first I though you might acheive this with a
GridLayout, but that made less sense when you
mention drawing lines from one grid to another.

You need either java.awt.Graphics or
java.awt.Graphics2D to acheive this
sort of rendering.

[ And no, I doubt anyone has developed a
class that you can extend that will save you
any work, but I could be utterly wrong. ]

Thank you for your advice, I'll have to learn java.awt.Graphics.
 
I

i5513

Hi!, I'd like develop a game where a "grid" is used. I imagine this
class exists now, but I can't find it.


I'd like, for example:

Grid grid= new Grid (640,480,3,4);
grid.BackGroundColor (RGB("red"));
grid.GridColor (RGB(blue));
grid.LineColor (RGB("yellow");

Where 640 is Grid's length, 480 is Grid's height.
3 is number of rows and 4 is number of column.
Three functions Color has obvious mission.

Once create Grid, I'd like:

grid.DisplayGrid ();
# Display Grid on screen
grid.Draw (3,4,3,5);
# Display a new line on Grid (from (3,4) to (3,5)), where start and
end are the center of (x,y).
grid.Remove (3,4,3,5);
# Remove line.

If I don't find this class, I'll develop it, but before I'd like to
know if I can derivate my class from others.

Thanks you!!
PD: (I'm a newbie in Java)

When I will make my program you'll have my advertisement. I'll study
yours advices, thanks you.
 
T

Tim Tyler

Danny Woods said:
(e-mail address removed) (i5513) writes:

One possible solution would be to use a JTable. You'd have to create a
custom CellRenderer, but this is fairly trivial. The model for the table
could have a Vector of Stacks: repainting would just amount to peeking at the
top of the Stack to get that particular tile to paint itself (this allows
you to stack tiles on a given location).

Drawing lines from one cell to another would not be easy using this model.
 
D

Danny Woods

Drawing lines from one cell to another would not be easy using this model.

Hi Tim,

A glass pane does a good job here. I once wrote something that used a tile-
based map, and distances could be computed between two points by stretching
a red line between the cells. The line itself was drawn on the glass pane,
leaving the JTable and its contents completely visible.

Of course, the complexity of the implementation must be weighed up against the
needs of the application. It worked for me with the needs I had at the time.
Whether or not it's suitable for others is up to them to decide.

Regards,

Danny.
 
A

Andrew Thompson

Danny Woods said:
model.
....
A glass pane does a good job here.
.... The line itself was drawn on the glass pane,
leaving the JTable and its contents completely visible.

The glasspane was bound to come up eventually!

I was tempted to suggest it to implement the
first(?) possibility - the GridLayout..

[ The reasons I didn't are because;
a) I have never dealt with them ..and
b) They make Swing compulsory ]

...so many possibilities! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top