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!

3
Contributors
9
Replies
18
Views
8 Years
Discussion Span
Last Post by tones1986

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;
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)
Removing from List -- arr.remove(index)

More detailed usage of arraylists can be found here.

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?

Edited by Ezzaral: n/a

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:

at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at CirclePanel.run(Animator.java:154)

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...

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.

Edited by Ezzaral: n/a

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!)

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

Attachments
``````import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.ArrayList;

public class Animator extends JFrame implements ActionListener
{
// variables here

private static CirclePanel leftPanel;  //custom JPanel below
private static LinesPanel rightPanel;
public Animator() {

leftPanel = new CirclePanel();
rightPanel = new LinesPanel();

this.pack();
}

public void actionPerformed(ActionEvent e) //FOR TOOLBARS
{
//stuff here
}

public static void main(String [] args)
{
JFrame f = new Animator();
f.setTitle("Animator");
f.setSize(1250, 750);
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);
}
}

/*********************************************************************************
* Cicle Panel Class
*
*
*
*
* *******************************************************************************/
class CirclePanel extends JPanel implements ActionListener, Runnable
{
JButton startCircle = new JButton("Start Circle");
JButton stopCircle = new JButton("Stop Circle");
JPanel circleDraw = new JPanel();

private boolean running = false;

public CirclePanel() {
JPanel circleMain = new JPanel(new GridLayout(1, 2));
circleMain.setPreferredSize(new Dimension(600,700));
JPanel circleOptions = new JPanel(new BorderLayout());
JPanel buttons = new JPanel(new GridLayout(1,1));
JPanel bottomPanel = new JPanel(new GridBagLayout());

circleDraw.setSize(300, 700);
circleDraw.setBackground(Color.BLACK);

JLabel bgColor = new JLabel("Background Color");
String[] colors = {"Red", "White", "Black", "Cyan"};
JList colorList = new JList(colors);
colorList.setVisibleRowCount(4);
colorList.setFixedCellWidth(150);

colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

numPoints = new JLabel("Number of points");

numPointsAmount = new JTextField("4", 10);

ButtonGroup group = new ButtonGroup();
JLabel speed = new JLabel("Speed");
slow.setSelected(true);

addItem(bottomPanel, numPoints, 0, 1, 1, 1, GridBagConstraints.WEST);
addItem(bottomPanel, numPointsAmount, 2, 1, 2, 1, GridBagConstraints.WEST);
addItem(bottomPanel, speed, 0, 3, 1, 1, GridBagConstraints.WEST);
addItem(bottomPanel, fast, 1, 3, 1, 1, GridBagConstraints.WEST);
addItem(bottomPanel, medium, 2, 3, 1, 1, GridBagConstraints.WEST);
addItem(bottomPanel, slow, 3, 3, 1, 1, GridBagConstraints.WEST);

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == startCircle)
startAnimation();
else if(e.getSource() == stopCircle)
stopAnimation();
}

public void startAnimation()
{
running = true;
runner.start();
}

public void stopAnimation()
{
running = false;
runner = null;
}

public void run()
{

Graphics g = circleDraw.getGraphics();
super.paintComponent(g);
circleDraw.setBackground(Color.BLACK);
g.fillRect(0,0,circleDraw.getWidth(), circleDraw.getHeight());

long sleepTime = 0;

if (fast.isSelected())
sleepTime = 500;
else if (medium.isSelected())
sleepTime = 2500;
else if (slow.isSelected())
sleepTime = 5000;

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

int numPoints = Integer.parseInt(numPointsAmount.getText());

double A = (double)( Math.PI * 2) / numPoints;
double theta = 0;
center.x = circleDraw.getWidth() / 2;
center.y = circleDraw.getHeight() / 2;

for(int i = 0; i < numPoints; i++)
{
nextPoint = new Point();
nextPoint.x = (int)(radius * Math.cos(theta) + center.x);
nextPoint.y = (int)(radius * Math.sin(theta) + center.y);
theta += A;
}
while(running)
{
g.setColor(Color.RED);
for (int i = 0; i  < numPoints; i++)
for (int j = 1; j < numPoints; j++)
{
{
g.drawLine(points.get(i).x, points.get(i).y, points.get(j).x, points.get(j).y);
}
try {
System.out.println("sleeping");
}
catch (InterruptedException ie)
{ //

}
}}
}

public void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
{
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
}
}

/*********************************************************************************
* Lines Panel Class
*
*
*
*
* *******************************************************************************/
class LinesPanel extends JPanel implements ActionListener, Runnable
{
JButton startLines = new JButton("Start Lines");
JButton stopLines = new JButton("Stop Lines");
JPanel lineDraw = new JPanel();
JComboBox dx1, dy1, dx2, dy2, color;

private boolean running = false;

public LinesPanel() {
JPanel lineMain = new JPanel(new BorderLayout());
lineMain.setPreferredSize(new Dimension(600,700));
JPanel lineOptions = new JPanel(new GridBagLayout());

lineDraw.setPreferredSize(new Dimension(300,700));
lineDraw.setBackground(Color.BLACK);

String[] values = {"2", "4", "6", "8"};
String[] colors = {"orange/blue", "green/yellow", "white/red"};
dx1 = new JComboBox(values);
dx1.setPreferredSize(new Dimension(100,25));
dx2 = new JComboBox(values);
dx2.setPreferredSize(new Dimension(100,25));
dy1 = new JComboBox(values);
dy1.setPreferredSize(new Dimension(100,25));
dy2 = new JComboBox(values);
dy2.setPreferredSize(new Dimension(100,25));
color = new JComboBox(colors);
color.setPreferredSize(new Dimension(100,25));

JLabel dx1Label = new JLabel("DX1");
JLabel dx2Label = new JLabel("Dx2");
JLabel dy1Label = new JLabel("Dy1");
JLabel dy2Label = new JLabel("Dy2");
JLabel colorLabel = new JLabel("Color");

addItem(lineOptions, startLines, 0, 0, 1, 1, GridBagConstraints.WEST);
addItem(lineOptions, stopLines, 1, 0, 1, 1, GridBagConstraints.WEST);
addItem(lineOptions, dx1Label, 2, 0, 1, 1, GridBagConstraints.EAST);
addItem(lineOptions, dy1Label, 4, 0, 1, 1, GridBagConstraints.EAST);
addItem(lineOptions, dx2Label, 0, 1, 1, 1, GridBagConstraints.EAST);
addItem(lineOptions, dy2Label, 2, 1, 1, 1, GridBagConstraints.EAST);
addItem(lineOptions, colorLabel, 4, 1, 1, 1, GridBagConstraints.EAST);
addItem(lineOptions, dx2, 1, 1, 1, 1, GridBagConstraints.WEST);
addItem(lineOptions, dx1, 3, 0, 1, 1, GridBagConstraints.WEST);
addItem(lineOptions, dy2, 3, 1, 1, 1, GridBagConstraints.WEST);
addItem(lineOptions, dy1, 5, 0, 1, 1, GridBagConstraints.WEST);
addItem(lineOptions, color, 5, 1, 1, 1, GridBagConstraints.WEST);

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == startLines)
startAnimation();
else if(e.getSource() == stopLines)
stopAnimation();
}

public void startAnimation()
{
running = true;
runner.start();
}

public void stopAnimation()
{
running = false;
runner = null;
}

public void run()
{
LineQueue firstLine = new LineQueue();
Graphics gc = lineDraw.getGraphics();
super.paintComponent(gc);
lineDraw.setBackground(Color.BLACK);
gc.fillRect(0,0,lineDraw.getWidth(), lineDraw.getHeight());

lineNode newLine = new lineNode(10,10,10,100);

while(running)
{
gc.setColor(Color.red);
for (int i = 0; i <= 100; i++)
{
gc.drawLine(firstLine.theArray[i].x1, firstLine.theArray[i].y1, firstLine.theArray[i].x2, firstLine.theArray[i].y2);
newLine = new lineNode(firstLine.theArray[i].x1 + 10, firstLine.theArray[i].y1, firstLine.theArray[i].x2 + 10, firstLine.theArray[i].y2);
System.out.println("STUFF!");
}
}
}

public void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
{
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.anchor = align;
gc.fill = GridBagConstraints.EAST;
}

public class LineQueue implements Queue
{
lineNode[] theArray;
lineNode newLineNode;
lineNode tail;

int currentSize = 0,``````

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);

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 {
} 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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.