aspose file tools*
The moose likes Beginning Java and the fly likes Making a class instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making a class instance "current"" Watch "Making a class instance "current"" New topic
Author

Making a class instance "current"

Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
hello,

This refers to a turn-based tile game with class "units". At a press of a button, a new "unit" is created at a specific point. I would like to be able to click a tile, select the unit (if there is one), and move the unit to wherever is clicked.(Ill worry about movement range later).
From research, i believe i need to use event handlers (eg mouseClick unit to select). As for making the unit "selected", i dont know... My classes are probably set up wrong to do this. my classes are as follows.
PlayerUnits

Hero




any ideas? i am thinking that clicking once loads the details of the unit eg location. the next click (assuming it is valid) transfers those details to the new clickable area, and deletes the original...
[ August 09, 2005: Message edited by: Seamus GalIagher ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
The class you shown are ok... What you need now is a class that will be the game. This class can hold the selected unit( a variable of the type unit that can be called "currentUnit" or "selectedUnit"). This class will propably also have a mouse listener(and other stuff too!!!). You will then be able to select a unit by assigning the variable(selectedUnit) or move it depending on the mouse event and/or the state of the game.
[ August 10, 2005: Message edited by: Jean-Sebastien Abella ]
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
hello again jean, and thanks for reply.

Your idea sounds good, and it makes sense. The problem is, although i have used java to make basic programs like a front for a shop or an artifical intelligence map generator,i have been away from java for a while, and im not 100% "thinking" java.
I have been looking at online games, that would be relavent to this probelm - eg chess (where you actively have to select a piece before moving). Im still trying to think of tutorials with this concept, as many tutorials are things like pong and space invaders and pacman, where you automatically control 1 unit the whole game!

thanks for your ideas,

Seamus
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
How about this.

- i make a nnew class
- it listens at any grid number for unitPresent (@Tiles)
- if a tile is clicked with a unitPresent, a value eg xyz goes from 0 to 1.
This value prevents another unit being selected, until a button(end unit move is clicked).
- the coordinates are calculated (row + column).
- every other tile that does not hold that unit gets green border
- any tile with green border that is clicked recieves duplicate unit information from the origonal tile.
- the origonal tiles information is reset.

i believe this will allow a unit to move anywhere on the board, both graphically and attribute wise. I can worry about legal moves after.
Is this a good method (in english) that could be coded in java?
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Your idea is ok! And I encourage you to try it!
You could also have the information of the row and column in the unit instead.
This way the movement of a unit will be easier. Also you could have the selected or not selected variable in the unit ( this way you could select more than 1 ). Then in the new class you need a List( probably an ArrayList) containing all the unit.

Keep on!
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
hi again,

this is my current status!
i made a new java program with just the basics - 4 references to a grid,4 jLabels ontop of references,1 grid position with a unit present,1 gird postion with no unit present, 2 unused grid positions,jlabels to show the results of clicking, mouseClicked events. Here is the part of my code.



this basically works, but only displays the results on jLabels. It is not a new class though - i have just added events. Also, it does not cover movement, only selection. What should i do next? Instead of displaying the information n jLables, do i code it so that a new instance of a new class is made(with string(name), int (some value), int(row), int(col))? And then save this class into an array? if so any hints?
[ August 10, 2005: Message edited by: Seamus GalIagher ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Jst to be sure is it possible to select different unit or it is always the same???

To answer right now how do you know if a unit is selected? I see that you have the variable currentUnit set to 1(unit Selected) or 0(unit not Selected) but do you know wich one! and where, if not better have a list of selected unit.

Some tips:
You should try make a function instead of copy pasting code.
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
IT works like this, on this small version.

- each tile has a value "unitPresent", default 0.
- i have coded that 1 tile has a "unitPresent", value 1.
- each tile is clickable.
- if "unitPresent" is 0, no information is displayed on jLabels(j1,j2,j3,j4)
- if "unitPresent" is 1, the information "eg name, attack" is displayed when clicked.
- if a tile with "unitPresent = 1" is clicked, an int,"currentUnit"(default 0), becomes value 1. This is how you know a unit is selected.
- clicking another tile with "unitPresent" is 1, when "currentUnit" is 1, prints out an error eg Unit already selected. The "information" from the origonal "unitPresent" is unchanged.
- a jButton "eut" can be clicked, to reset "currentUnit" to 0.(ie an "end unit turn" button".

So basically all it does is display the information of 1 unit at a time. I have changed code back and forth to make 2 tiles have a unit with different stats.

The tiles are t0 (position [0][0]), t1 (postion [0][1]), t2 (postion[1][0]) etc. When clicked to select a "unit", the unitName(string), attack(int),rr(the row number),cc(col number) is displayed on jLabels (j1,j2 etc)

you mentioned functions. i am a beginner at java and not 100% sure how to do this, in this scenario. Remember that this is only a quickly made dummy of my current work. All i am trying to do is to get selecting a unit, and moving it anywhere valid, to work.

my current idea is to have an oldX/oldY(original row + column),newX/newY(new row + col).
-Then the program loops through each tile until the newX/newY matched the current tile.
-Then all information is passed to newX/newY.
-All information at oldX/oldY is deleted. Make sense?
[ August 10, 2005: Message edited by: Seamus GalIagher ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
It does make sense!

By assuming that gridref is a 2d array of the type Tile I would change the currentUnit type to a Tile instead of an int, it would then be more easy to get the information on the selected unit. Oh and if nothing is selected the content would be null
currentUnit = null;


about the functions...
instead of doing 2 or more times
j1.setText(gridref[0][0].unitName);
j2.setText(""+gridref[0][0].attack);
j3.setText(""+gridref[0][0].rr);
j4.setText(""+gridref[0][0].cc);

you could have a function doing it:
void displayInfo( String name, String attack, String rr, String cc )
{
j1.setText(name);
j2.setText(attack);
j3.setText(rr);
j4.setText(cc);
}


This is just a simple function but it is worth it. A very very good practice is to never duplicate code.

Oh and on what do you click??? what class is the mouseListener or mouseAdapter???
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
hi again,

you asked what class the mouse things where at. Well i am a newbie using jbuilder, and every bit of code is on the first class that jbuilder made, called Frame1. (Apart from Tile which is a new class). I realise alot of tidying up is needed. For example, when looping through the grid, i have to manually type (cut and paste) the code at a particular line - i do not know how to make a class or whatever, that does

.

and call upon it when needed. An example of when it is needed would be when looking through the grid to find the newX and newY. are you able to explain?

secondly, thankyou for help with functions - i have to cut and paste alot. i understand the example you gave - it means i just type in keyword or two, instead of pasting alot of code. However, where do i put the function (in Frame1?), and how do i code it so that when currentUnit = 1, displayInfo is initiated.. I guess i need to somehow implement a class with an ActionListener, which listens for "currentUnit" to change to 1?
as for what i click on, above the "grid reference of tiles", are jLabels. I used the Jbuilder function os MouseClicked.

My "proper" map system (it just makes a map based on an array, coded VERY badly), has 4 layers (grids) currently.
1-references to class tile and its subclasses
2-jLabels that change images according to specific "1" attributes eg grass image
3-references to units
4- jLabels that change images according to specific "3" attributes eg unit image

essentially, i can create layers for objects (items) and scenery (trees/rocks). But lets worry about my small unit test! :-)
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
You can put the function anywhere in Frame1 as it is your only class!
Basically you try to have a structure like this in your java file
package xxx.yyy.zzz;

import ...;
import ...;

public class Frame1
{
Type memberName1;
Type memberName2;
...

returnType function1( Type param1, ...)
{
function2(...);
}
returnType function2( Type param1, ...)
{

}

...
}


where Type is something like int, String, Frame1 or even PlayerUnit

so you could put the function after the t0_mouseclickeevent(...)
And probably in jbuilder you could right click your class Frame1 and click add Function( I never used JBuilder but it should be ther...) and it would insert it somewhere legal!
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
ok!

i understand it a bit more. i deleted all the ".setText". I then made a new class called display, with your suggestions.


Now what do i type in here (ie the syntax), above "currentUnit++", in order for the text to be set?

Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
You call that function! since you are in the class Frame1 you can call any function define in Frame1.
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Wait!... You made a new Class!! no ... you dont have to... just add the function displayInfo() inside the class Frame1
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
hi!

i apoligize, i actually did know how to do what you wanted me to do, but i didnt know it was called a function. i got it to work, but


is not much less text than my method, especially when you have to write the function. However, i do see it will be useful in bigger test cases!

Thanks!
[ August 10, 2005: Message edited by: Seamus GalIagher ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
It's not a matter of size!
If in the future the display of the info is different let say you want to produce another window with the provided information you would just have to change 1 function instead of a couple of place in your code(and not being sure if you changed them all and making a couple of mistakes here in there...).
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
I stand corrected! :-)

You have been a great help so far. Your ideas basiclly do the same functions, but cut down alot of code, and makes it simplier for the future (which i understand now). Selecting a unit is 100% complete in this small dummy.
My next task i can figure out myself :-) - making surrounding grid tiles targets for moving. The main issue here will be "nullpointerexceptions", when i go off the grid eg [-1][0]. However , i had this problem before. I wasnt able to fix it by using call exceptions, but i did it by making each tile on the outside a "border tile", to prevent errors.

thanks again and you probably hear from me soon >.<
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
for nullpointerexception you can test it first...
if( object != null ){
object.function();
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Making a class instance "current"
 
Similar Threads
Multiple mouselisteners
Class Practice Help
ClassCastException - ArrayList binarySearch 1.4
Offsetting X/Y
Problem with ArrayList toArray method