ArrayList -> Polygon?

Hi,

in my game the user clicks with the mouse to position the player; each position is stored in an ArrayList:private List <Ellipse2D>playerAllCoordinates =new ArrayList<Ellipse2D>();

Now I need these coordinates for collision detection; for this I am using the polygon class.

Is it possible to construct a polygon using the x and y coordinates stored in this ArrayList (or a similar ArrayList so that I can paint a polygon using the stored coordinates?)?

[560 byte] By [SFLa] at [2007-11-27 2:57:29]
# 1

From the API doco

Polygon

public Polygon(int[] xpoints,

int[] ypoints,

int npoints)

Constructs and initializes a Polygon from the specified parameters.

Parameters:

xpoints - an array of x coordinates

ypoints - an array of y coordinates

npoints - the total number of points in the Polygon

Throws:

So the answer is ... if you have the correct number of [x,y] coordinates in your ArrayList, then yes you can do it.

abillconsla at 2007-7-12 3:36:02 > top of Java-index,Desktop,Core GUI APIs...
# 2
Is one array sufficient or do I need 2 arrays, the second storing the vertices? How could I store the vertices?
SFLa at 2007-7-12 3:36:02 > top of Java-index,Desktop,Core GUI APIs...
# 3
The Polygon can be any number of vertices ... it depends on the number of [x,y] coordinates you have - that's what the third 'int' parameter specifies. So if each ArrayList contains six pairs of [x,y] coordinates, than each ArrayList will represent one Polygon. Or am I missing
abillconsla at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 4

Ok.... I am really uncertain in this topic... I am asking about this ArrayList because I need to remove points from a polygon... that's why I thought I can store the coordinates in an ArrayList, adding and removing coordinates to / from this ArrayList and finally drawing the polygon. I noticed that the polygon class don't have a "removePoint" method :/

SFLa at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 5
This is the first time that I read that you want the coordinates back out. I have not tried it, but perhaps the getPathIterator will do what you want.
abillconsla at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 6

I now tried something like this:

private List playerPolygonCoordinates = new ArrayList();

private Polygon playerPolygon = new Polygon();

private Point2D playerCurrentPosition;

(...)

public void mouseClicked(MouseEvent evt) {

playerCurrentPosition = new Point2D.Double(evt.getX(), evt.getY());

playerPolygonCoordinates.add(playerCurrentPosition);

(...)

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

for(int i=1 ; i < playerPolygonCoordinates.size(); i++) {

playerPolygon = (Polygon) playerPolygonCoordinates.get(i);

g2.draw(playerPolygon);

}

(...)

...with every mouse click I am adding points to the array list and can then draw the polygon... however, this doesn't work... to remove a coordinate from the polygon I then would just remove one coordinate pair from the array list and redraw the whole polygon.. hmmm....

SFLa at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 7

private Point playerCurrentPosition;

/* ... */

public void mouseClicked(MouseEvent evt) {

playerCurrentPosition = new Point(evt.getX(), evt.getY());

playerPolygonCoordinates.add(playerCurrentPosition);

/* ... */

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int size = playerPolygonCoordinates.size();

int[] x = new int[size];

int[] y = new int[size];

for (int i=1 ; i < size; i++) {

Point point = ((Point) playerPolygonCoordinates.get(i));

x[i] = point.getX();

y[i] = point.getY();

}

g.drawPolygon(x,y,size);

}

Message was edited by:

abillconsl

abillconsla at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 8

Thanks, it works :)

How could I now construct a polygon from the playerPolygonCoordinates list (something like playerPolygon = (Polygon) playerPolygonCoordinates.get(i); // not working :(

) so that I can later define an area like playerArea = new Area(playerPolygon);

?

One more question.. I think it's not possible: could I draw the polygon without drawing the last line segment between first and final pairs of coordinates?

Message was edited by:

SFL

SFLa at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 9

You are welcome, but that last post of your confused me.

First you said it's working and then immediately precede to say it does not work and display a line of code that I had hoped you would have displaced/replaced with the code I gave you.

As to the other questions regarding not closing the polygon ... look at drawPolyline instead - this will do what I think you want.

abillconsla at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 10
no, everything is fine... I played with various approaches and thought that I can, using your code, manage to put all coordinates into a Polygon variable... I already abandoned it... newbie thoughts :)Thanks!
SFLa at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 11
Ok ... well ... you're welcome ... I suppose ...
abillconsla at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...
# 12
Solved, using GeneralPath instead.
SFLa at 2007-7-12 3:36:03 > top of Java-index,Desktop,Core GUI APIs...