Hey guys,

I am trying to store a varying number of coordinate points in an array. So i thought of a list, and declaring this list as an arraylist using the Point class. Make sense so far? Or am i incorrect already?

In doing so i came up with this:

ArrayList<Point> points = new ArrayList<Point>();

Point center = new Point();

Point nextPoint = new Point();

Point prevPoint = new Point();


int numPoints = 4;

for(int i = 0; i < numPoints; i++)

{

points[i].x = r * cos(theta) + center.x;

points[i].y = r * sin(theta) + center.y;

theta += A; 

}

What am i doing wrong here in terms of the points.x/y

I am getting the error: must be of type array but it is resolved to arraylist<point>

So how can i make this work? I know it's something simple... all i want to be able to do is calculate a point, then store that point in the ArrayList... should be possible right?

Any help would be appreciated!

Thanks in advance

Recommended Answers

All 9 Replies

I guess i might have just sorted this out by myself, but i wanted confirmation that it would be correct in doing this:

I did:

ArrayList<Point> points[];// = new ArrayList<Point>();
	Point nextPoint = new Point();

	int numPoints = 4;
	for(int i = 0; i < numPoints; i++)
	  {
	  nextPoint.x = r * cos(theta) + center.x;
	  nextPoint.y = r * sin(theta) + center.y;
	  points[i].add(nextPoint);
	  theta += A;  //A was calculated earlier
	  }

This now gives no errors. I just wanted to check to see if this code is sound and should work as is?

Thanks again

the main problem is that you are assuming that the syntax for arraylists and arrays are the same. In fact, they are not. Here is a basic table I made that details the differences in syntax:

Arrays
Accessing Size = arr.length
Accessing Data -- arr[index]
Modifying Data -- arr[index] = val
Adding to List -- Not Applicable
Removing from List -- Not Applicable

ArrayLists
Accessing Size -- arr.size()
Accessing Data -- arr.get(index)
Modifying Data -- arr.set(index,val)
Adding to List -- arr.add(val) or arr.add(index,val)
Removing from List -- arr.remove(index)

More detailed usage of arraylists can be found here.

This should help you out in writing your code.

I'm curious why you chose to drop this thread: http://www.daniweb.com/forums/thread271568.html and start a new one when you're still working on the same thing?

Did you get the program to work without these extra Point collections and just want to modify it now?

Hey guys - i want to apologize for making another post, i considered it another problem, but should have realized that it was the same program i was working with. Sorry again.

My problem is sort of similar as to what i had before, but now its that i am getting that my array is out of bounds. Which, to me, makes no sense, because i have done Println's to find out what the data is, and the data is there, so my set() methods for the ArrayList must be incorrect. Here is my code and lets see if you can spot my mistake!

public void run()
    {
	Graphics g = circleDraw.getGraphics();
	ArrayList<Point> points = new ArrayList<Point>();
	Point center = new Point();
	Point currentPoint = new Point();
	Point nextPoint = new Point();
	
	int numPoints = 3, radius = 50;
	double A = (double)( Math.PI * 2) / numPoints;
	double theta = 0;
	center.x = getWidth() / 2;
	center.y = getHeight() / 2;
     
    

	for(int i = 0; i < numPoints; i++)
	  {
	  nextPoint.x = (int)(radius * Math.cos(theta) + center.x);
	  nextPoint.y = (int)(radius * Math.sin(theta) + center.y);
	  points.set(i, nextPoint);
	  theta += A;  
	  }
    while(running)
        {
    	currentPoint = points.get(0);	
    	nextPoint = points.get(1);
        
    	for (int i = 0; i  < numPoints; i++)
    		for (int j = 1; j < numPoints; j++)
    		{
    			{
    				g.setColor(Color.RED);
    				g.drawLine(points.get(i).x, points.get(i).y, points.get(j).x, points.get(j).y);
    			}
    		}
        //sleep here
        }

As you can see, what i am trying to do is draw from one point, to EVERY other point, then after that, go to the next line, then draw to the other lines, etc...

My problem isn't with drawing, its with getting/setting the Points into the ArrryList in the first place...

Here is my error:

Exception in thread "Thread-3" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at CirclePanel.run(Animator.java:154)
at java.lang.Thread.run(Unknown Source)

Line 154 is this line of code:

points.set(i, nextPoint);

Thanks again for all your help, and once again, im sorry for reposting on a new thread!

I think the main problem is that you aren't capitalizing on the dynamic nature of the ArrayList. If you declare an array of size 10, it creates 10 slots for you and you can assign values to each location accordingly. But in an arraylist it just creates a new index every time you need to add a value. So you can't access set a value at the 10th index if only 5 items are in the list. You CAN add the item to the list, and automatically the item will be added to slot number 6. You can only set slot 10 to your value if there are currently 10+ items in the list. Hopefully this makes sense...

Try using the add method instead of the set-at-index one.

You may also want to consider creating a new "nextPoint" object in your loop instead of re-adding the same reference to the list multiple times and changing the x and y value each iteration.

Good call Ezzaral, I didn't notice that

Hey guys - thank you for your input - adding the 'new Point' eahc time solved that problem i was having.

I have finished that section of work. I have to create something that looks like the old school windows screen saver... the lines tracing around the screen. To do this, i was to set up a new line class, and create my OWN queue class (not using the java built in queue completely) and also create nodes for each line element.

I think i have written something that is pretty solid (some of my helper methods for my LineQueue class are pretty pointless at the moment, but my main concern is this:

How do i go about drawing up to 10 lines on the screen at once, while still adding more, but erasing the oldest drawn lines. So to make it clearer:
1) the queue can store up to 10 lines
2) only 10 lines should be on the screen at once
3) when a new line is added, it should added in replacement of the oldest line...BUT
4) this old line needs to be removed from the visible graphics on the screen.

My solution for 4 (which i havent written out yet - i will wait for your input / suggestions) was to simply set my draw color to black (the background color) and simple redraw the oldest line to match the background, therefore, 'erasing' from view.

Is there a better way about this? Or should i spend the time coding it this way now? Any suggestions or comments about my current LineClass and lineNode stuff would be awesome, and greatly appreciated. (As for the drawCircle stuff, i have that working enough for now, so please ignore that section to save yourself some time!)

Thanks again for your help in advance!

(I have posted my entire .java for review - thanks!)
Animator.java

Hey guys - alright i have got to the point where i have create my linequeue and linenodes correctly. (at least it seems to work!).

My problem lies with the printing step of my line queue. What i want to have happen is that i add a line to the queue (a lineNode) then i will print out the entire queue, then clear the screen. I will then add another line to the queue, if the queue is full, replace the oldest line in queue (else just continue to fill the queue till full)...then reprint the queue with the new line (without having the old print queue on the screen at the same time).

Does this make any sense? if not, let me know and i will try to clarify it for you. If it does, please read my small snippets of code below - maybe you will see my problem.

Within void run()

public void run() {
...get data from GUI
Graphics g = lineDraw.getGraphics();
    Dimension d = lineDraw.getSize();
    
	lineNode first = new lineNode(d, g, lineColor1, 10 + cx1, 10+ cy1, 10 + cx2, 100 + cy2);
	lineNode second = new lineNode(d, g, lineColor2, 250 + cx1*2, 250 + cy1*2, 450 + cx2*2, 200 + cy2*2);
	
	firstLine.addLine(first);		
	secondLine.addLine(second);

	while(running)
	{
		
	
		g.setColor(lineColor1);	
		g.drawLine(firstLine.theArray[firstLine.currentIndex - 1].x1, firstLine.theArray[firstLine.currentIndex - 1].y1, firstLine.theArray[firstLine.currentIndex - 1].x2, firstLine.theArray[firstLine.currentIndex - 1].y2);
		firstLine.addLine(new lineNode(d, g, lineColor1, firstLine.theArray[firstLine.currentIndex - 1].x1 + cx1, firstLine.theArray[firstLine.currentIndex - 1].y1 + cy1, firstLine.theArray[firstLine.currentIndex - 1].x2 + cx2, firstLine.theArray[firstLine.currentIndex - 1].y2 + cy2));
 
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		g.clearRect(0,0,(int)d.getWidth(), (int)d.getHeight());
		g.setColor(Color.black);
		g.fillRect(0,0, (int)d.getWidth(), (int)d.getHeight());
	
	}
  }

This currently prints only a single line, but it does move from the top corner to the bottom (each old line gets erased, then a new one drawn) and it continues to get drawn even past the edge of my drawing area (a problem to solve easily later)

I will show you the addLine() method:

public void addLine(lineNode x)
    {
    	if (currentIndex == 0 || currentIndex <  LINES_MAX)
    	{
    		theArray[currentIndex] = x;
    		currentIndex++;
    		currentSize++;
    	}
    	else if (currentIndex == LINES_MAX)
    	{
    		currentIndex = 0;
    		theArray[currentIndex] = x;
    		currentIndex++;
    	}
    	
    }

currentIndex is set to 0 when the lineQueue is created, as is currentSize. LINES_MAX is set to 10. So if i am thinking correctly, my program should do something like:
draw 1 line, erase screen
draw 2 lines, erase screen
..
..draw 10 lines, erase screen
draw 10 lines, (the oldest line being replaced with newest lineNode), erase screen
then contine as previous until user stops animation

Is this possible? Im sure it is, im just missing something important.

My thinking is maybe my for () loop is incorrect? Or the value inside the theArray[VALUE] is incorrect?

Any help would be greatly appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.