# [java] Vectors in Java?

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have read a lot of about collision detection between circles. Found some good tutorials and read most of the informations. Well the problem is that in the tutorials source codes have some operants that wont work. Code is always speaking about Vector vectorA or Vector gravityVector but what are those? Do I find Vector class from Java api or should I do my own Vector class?

##### Share on other sites
You would have to create your own vector class, which basically holds x and y variables representing the direction and magnitude of the vector (or x, y, z for 3d) and has methods for normalizing the vectors and such. You can look at the gamedev articles on vectors here.

##### Share on other sites
That, or either use a vector class distributed by your engine of choice - if you're using one, that is ^_^

There are some open source math libs for Java on the internet... I'm sure you'll find something good on sourceforge. I mean that because there's really no reason to 'reinvent' the wheel - there's already plenty of code available.

Son Of Cain

##### Share on other sites
Personally I use the classes supplied with LWJGL, but there is in fact a library called javax.vecmath which is more extensive and does what you want.
Clickety-Click
Edit: I'm no big proponent of reinventing the wheel, but in the case of Vectors (or Matrices, for that matter), due to their importance in 3d, it might be a good experience for you to learn about them, and it might be fastest to learn by coding some Vector classes.

##### Share on other sites
Warning: There is a Vector class in the Java API, but it's not what you're looking for. It more closely resembles the stl::vector found in C++ (that is, a dynamic array). For mathematical vectors, you'll have to roll your own or use a seperate library, I believe.

##### Share on other sites
Thanks bros.

Found some interesting pages about Vector classes. Basically building my own Vector class shouldnt be so hard or take plenty of time.

Well its cool now and I have again gained some more skills in game programming ;)

##### Share on other sites
hey guys!

my question fo rtoday seem to be kinda related to this, so here it goes:

the constructor for JTable takes only (Object[][] rowData, String[]columnNames) or (Vector rowData, Vector columnNames) as arguments.

my problem is...

im trying to build a table for a txt file in disc.
I open the file, and for every line in the file I want to make a row in the table.

the file is of the form:

thing11,thing12,thing13,thing14
thing21,thing22,thing23,thing24

so the table should display:

thing11|thing12|thing13|thing14
thing21|thing22|thing23|thing24

so, in plain text, what I do is:
- open the entire file and coping it over to a DefaultListModel type var.
- get every element from the list (which corresponds to every line in the file) and split it in a String[] so I can get the values in an indexeable manner.
- then i copy the String[] onto a Vector (named row), and this one holds the values for every column, for only 1 row.

so far so good.

- then i made a second Vector, to hold all the rows. (named data)
- then for every line in the file, I add an element in the tableData Vector.

then I build the table: JTable table = new JTable(data, columnNames);

it all works swiftly...
except...
as Vectors are actually POINTERS, not variables, the data Vector will show the same things in every element, which reflects the last change in the object which the rowValues Vector point at, menaing: the last line of the freaking file.

so if my damn file has 12 lines, the table WILL show perfectly on screen, but will contains 12 times the last line in the file.

ive tried to substitute the row Vector into a number of other kinds of indexed variables, like arrays, lists, objects, etc... and it works to the point that the data Vector manages to be filled with contents. However, for a type mismnatch or some other stupid reason, I get a fatal exception when trying to build the table, for the constructor gets unable to split the elements in data into the same number of columns given by the columnNames Vector.

do you guys think of a workaround for this? like: i tried to use a String() with the contents for every cell separated by commas, as it shows like that when i convert the tableData Vector into String(), and i still get the same exception when building the table. the only way to build the table nicely is using those vectors, but then, how to make every line of the table reflect a line from the file?

heres the chunk of code:
(please pay attention to the stuff I write as output (System.ou.println()) and the pasted actual output from the command prompt when i run the routine)

Vector columnNames = new Vector();    columnNames.add("Data");    columnNames.add("Designer");    columnNames.add("Livro");    columnNames.add("Ano-Modelo");    columnNames.add("UPC");    columnNames.add("Folha");        String[] splittedData = {""};        System.out.println(allLines.getSize()+" linhas");        int counter = 0;    int counter2 = 0;    Vector data = new Vector();        Vector row = new Vector();    row.add("Data");    row.add("Designer");    row.add("Livro");    row.add("Ano-Modelo");    row.add("UPC");    row.add("Folha");     for (counter=0; counter < allLines.getSize();counter++)    {    	splittedData = allLines.elementAt(counter).toString().split(",");     	for(counter2=0;counter2<6;counter2++) //get only the last elements of splittedData		{    					switch(counter2)			{				case 0: row.setElementAt((splittedData[(splittedData.length-1)-counter2]).toString(),counter2); break;				case 1: row.setElementAt((splittedData[(splittedData.length-1)-counter2]).toString(),counter2); break;				case 2: row.setElementAt((splittedData[(splittedData.length-1)-counter2]).toString(),counter2); break;				case 3: row.setElementAt((splittedData[(splittedData.length-1)-counter2]).toString(),counter2); break;				case 4: row.setElementAt((splittedData[(splittedData.length-1)-counter2]).toString(),counter2); break;				case 5: row.setElementAt((splittedData[(splittedData.length-1)-counter2]).toString(),counter2); break;			}		}    	data.add(row);    	System.out.println(counter+" "+data.elementAt(counter));    }    //TODO: setup table        //JTable table = new JTable(data, columnNames);        for (counter=0;counter < data.size();counter++)    {    	System.out.println("data contents: "+counter+" "+data.elementAt(counter));    }------------------- cut here 8< ---------------------goddamn output:2 linhas0 [29/11/2005, 08, 1A2B, 5A, Camila Paschini, 2005]1 [30/11/2004, 1.1, 1A1A, 54, Alberto Gil, 2006]data contents: 0 [30/11/2004, 1.1, 1A1A, 54, Alberto Gil, 2006]data contents: 1 [30/11/2004, 1.1, 1A1A, 54, Alberto Gil, 2006]

##### Share on other sites
Quote:
 Original post by NairbWarning: There is a Vector class in the Java API, but it's not what you're looking for. It more closely resembles the stl::vector found in C++ (that is, a dynamic array). For mathematical vectors, you'll have to roll your own or use a seperate library, I believe.

The "Vector" they reference is probably more along the "Point" or "Point2D" classes available in Java. Both hold and x/y value pair, and Point2D has built-in distance functions and the like. For a 3D point, you'd have to define your own class (likely called "Point3D" :) or something like that.

##### Share on other sites
I'm wondering: If I want to use vectors for movement of objects in a Java game (esp. on a cell-phone game), what would I do for decimal numbers? I've made a 2D vector class before, for a game I made in C++/OpenGL, but for that I used floats. Here, I can't just remake the same class so easily. Everything that is drawn on
the screen with "DrawImage" is positioned with integers. Whenever I look on the web for anything about fixed point math (a concept that I don't really understand, honestly, but its supposed to be the answer to this problem) they talk about using the << (bit shift) operator. But that's from the C language, that's not usefull to me right now.

oh and, Elehesie? You'll get better luck getting help on that by posting in a whole new thread.

Edit: Basically, my question is: How do I do bit-shifting in Java, but if you have any other advice to impart that you think I might find usefull, please tell me! I need all the advice I can get! :)

[Edited by - EGD Eric on December 11, 2005 10:40:42 AM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634782
• Total Posts
3019267
×