Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: draw 4 different polygons.

1. ## draw 4 different polygons.

hi my problem is to draw 4 different polygons thanks in advance

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Polygons1 extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

//polygon 1 point 3

//polygon 2 point 4

//polygon 3 point 6

//polygon 4 point 12

int n = POLYGON.size() ;

int[] x = new int[n];
int[] y = new int[n];
int counter = 0;

for (POINTS pnt : POLYGON) {
x[counter] = pnt.getX();
y[counter++] = pnt.getY();

Polygon[] polygons = new Polygon[4];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(x, y, n);
g.setColor(Color.RED);
g.drawPolygon(polygons[i]);
}} }

public class POINTS {

private int ID;
private int X;
private int Y;

public POINTS(
int ID, int X, int Y) {
this.ID = ID;
this.X = X;
this.Y = Y;
}

public int getX() {
return X;
}

public int getY() {
return Y;
}

public int getID() {
return ID;
}
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Polygons");
frame.setSize(550, 550);

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();

frame.show();
}
}

2. ## Re: draw 4 different polygons.

Can you explain the problem you are having?
If you are getting any errors, please copy the full text and paste it here.

[code=java]
[/code]
to get highlighting and preserve formatting.

3. ## Re: draw 4 different polygons.

hello
my problem is how to draw polygons other than 4 <id> a arraylist containing all points of the polygons the code that I wrote I just draw a polygon with 25 points
example
polygon 1 ...... id = 1 .... points .. .....x {50,70,50}, y = {20,30,40};
polygon 2 ...... id = 2 .... points .... .. x {100,130,100,120}, y = {20,30,40,30};
polygon 3 ...... id = 3 .... points .... .. x {,,,,,,,,,,,,,,, }, y = {,,,,,,,,,,,,,,,,,,,,,};
polygon 4 ...... id = 4 .... points .... .. x {,,,,,,,,,,,,,,,,}, y = {,,,,,,,,,,,,,,,,,,,,,};

4. ## Re: draw 4 different polygons.

hi this is the code formatted I hope I was clear I thank you in advance

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Polygons2 extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

//polygon 1 point 3

//polygon 2 point 4

//polygon 3 point 6

//polygon 4 point 12

int n = POLYGON.size();

int[] x = new int[n];
int[] y = new int[n];
int counter = 0;

for (POINTS pnt : POLYGON) {
x[counter] = pnt.getX();
y[counter++] = pnt.getY();

Polygon[] polygons = new Polygon[4];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(x, y, n);
g.setColor(Color.RED);
g.drawPolygon(polygons[i]);
}
}
}

public class POINTS {

private int ID;
private int X;
private int Y;

public POINTS(
int ID, int X, int Y) {
this.ID = ID;
this.X = X;
this.Y = Y;
}

public int getX() {
return X;
}

public int getY() {
return Y;
}

public int getID() {
return ID;
}
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Polygons");
frame.setSize(550, 550);

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();

frame.show();
}
}

--- Update ---

hello
I'm working on this solution but can not figure out how to draw the polygons
that is how to pass the points to the polygons you asking for your help

Map<Integer,List<coordinates>> PolygonsByID =

for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<coordinates>();
PolygonsByID.put(n.getID(), PolygonsForID);
}

}

5. ## Re: draw 4 different polygons.

how to draw four different polygons
What happens if you call drawPolygon() for each polygon you want drawn?
I only see one call to drawPolygon() in the posted code. Because of the poor formatting I can't tell if there are any loops. There should not be more than one } on a line.

[code=java]
[/code]
to get highlighting and preserve formatting.

--- Update ---

The code you posted does not compile without errors. There is a class definition missing.

6. ## Re: draw 4 different polygons.

I'm sorry this is the correct code
I thank you in advance help me

```<
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Polygons2 extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

//polygon 1 point 3

//polygon 2 point 4

//polygon 3 point 6

//polygon 4 point 12

Map<Integer,List<POINTS>> PolygonsByID =

for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<POINTS>();
PolygonsByID.put(n.getID(), PolygonsForID);
}

}
}

public class POINTS {

private int ID;
private int X;
private int Y;

public POINTS(
int ID, int X, int Y) {
this.ID = ID;
this.X = X;
this.Y = Y;
}

public int getX() {
return X;
}

public int getY() {
return Y;
}

public int getID() {
return ID;
}
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Polygons2");
frame.setSize(550, 550);

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();

frame.show();
}
}
>```

7. ## Re: draw 4 different polygons.

Where is the call to the draw method for drawing the polygon?

If you want to draw 4 different polygons, where are the 4 containers that hold the points for the polygons? It looks like all the points are added to just one container, not to 4 different containers.

8. ## Re: draw 4 different polygons.

hello
container is one where all the points then group the different polygons based on their <id> and eventually draw polygons can be such a thing or are on the wrong track
I'm working on this code but I can not move the points to the polygons and then draw them as you can continue asking for your help and thank you in advance

```<
Map<Integer,List<POINTS>> PolygonsByID =
for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<POINTS>();
PolygonsByID.put(n.getID(), PolygonsForID);
}

//At this point I would like to draw the polygons
//in PolygonsForID but do not know how

}
>```

9. ## Re: draw 4 different polygons.

can not move the points to the polygons
Please explain what the problem is.
Where does the code call the drawPolygon() method to draw a polygon?
There are two versions of the method. You need to write some code that uses one of them to draw a polygon(). When you can draw a polygon then you need to work on separating the points that are in the POLYGON container into 4 separate places that can be used to draw the 4 polygons you want to draw.

10. ## Re: draw 4 different polygons.

hello
the question is
1) how do I count the number of points for each polygon;
2) How do I switch points to polygons

```<
Map<Integer,List<POINTS>> PolygonsByID =
for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<POINTS>();
PolygonsByID.put(n.getID(), PolygonsForID);
}

Polygon[] polygons = new Polygon[4];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(new int[] {.........},new int[] {.............}, .......);
g.setColor(Color.RED);
g.fillPolygon(polygons[i]);

}
}
>```

11. ## Re: draw 4 different polygons.

How do I count the number of points for each polygon;
How many points are there for each polygon? How do you find those points in the POLYGON collection? Count them as they are added to the arrays used to build the Polygon object and use that count when you create the Polygon object. Then reset the count to 0 and start over filling the arrays for the next Polygon object.

How do I switch points to polygons
The Polygon class's constructor takes arrays of points to create the Polygon object.

12. ## Re: draw 4 different polygons.

hello
I'm working on this solution but I error
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 50, Size: 1

```<
Map<Integer,List<POINTS>> PolygonsByID =
for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<POINTS>();
PolygonsByID.put(n.getID(), PolygonsForID);
}

int num = PolygonsForID.size();
int[] x =  (int[]) PolygonsForID.get(n.getX());
int[] y = (int[]) PolygonsForID.get(n.getY());

Polygon[] polygons = new Polygon[4];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(new int[] {x[i]},new int[] {y[i]}, num);
g.setColor(Color.RED);
g.fillPolygon(polygons[i]);

}
}
>```

13. ## Re: draw 4 different polygons.

What line did the error happen on?

Before trying to write any more code, you need to stop and do some design work on what the code needs to do. The posted code does not do what I think the program needs to do.

Given that the points for the 4 polygons are in a collection: POLYGON, work out the steps to get the points for each ploygon out of that collection and into 4 separate Polygon objects.

Make a list of the steps (in pseudo code) the program needs to do to create the 4 Polygon objects.
When the logic for that is done, then write the code to do it.

14. ## Re: draw 4 different polygons.

hello
asking for your help I do not understand do wrong I'm working on this code can not spend points to individual polygons could get some help I thank you in advance.

```<
Map<Integer,List<POINTS>> PolygonsByID =
for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<POINTS>();
PolygonsByID.put(n.getID(), PolygonsForID);
Collections.sort(PolygonsForID);
}

}
for(Map.Entry<Integer,List<POINTS>> count : PolygonsByID.entrySet()) {
int np =  count.getValue().size();
//System.out.println("polygon : " + count.getKey() + " points : " + np);

int[] x = new int[25];
int[] y = new int[25];
int counter = 0;

for (POINTS pnt : POLYGON) {
x[counter] = pnt.getX();
y[counter] = pnt.getY();
counter++;
}
Polygon[] polygons = new Polygon[4];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(x, y, np);
g.setColor(Color.RED);
g.drawPolygon(polygons[i]);
}
}
>```

15. ## Re: draw 4 different polygons.

What are the steps to make 4 Polygons from the points contained in the POLYGON container?
Here is one way it could be done:

Define array to hold Polygons
Initialize ptr to the above array to 0
initialize polygon id number
begin loop
get next point from POLYGON
is this point in current polygon?
yes - save point's x and y values
no- create new Polygon object with above saved points; increment polygon id number and array ptr
end loop

Now the array holds 4 Polygon objects that can be looped through to be drawn

16. ## Re: draw 4 different polygons.

hello
I'm sorry but I do not know how I solve this problem you can see some examples thank you in advance.

17. ## Re: draw 4 different polygons.

I just posted pseudo code for one way to solve your problem.
Look at the lines of pseudo code, one at a time and if you don't understand how to do that step, please ask a question about that step.
The first step: define an array - there is an example in the posted code:
` Polygon[] polygons = new Polygon[4];`
What about the rest of the steps?

18. ## Re: draw 4 different polygons.

hello
I'm sorry but I do not understand what code I have to watch.

19. ## Re: draw 4 different polygons.

Many of the steps the program needs to do were listed in post#15

20. ## Re: draw 4 different polygons.

hello
I followed the steps # 15 but the problem is that I can not understand
I can get some help or some example thank you in advance

21. ## Re: draw 4 different polygons.

Which step(s) are you having problems with?
Post the new code you have
and post the questions you have about the listed steps.

22. ## Re: draw 4 different polygons.

hello
this is the code and I get this error I can get some help I thank you in advance.

```<

for (POINTS n : POLYGON) {
List PolygonsForID = PolygonsByID.get(n.getID());
if (PolygonsForID == null) {
PolygonsForID = new ArrayList<POINTS>();
PolygonsByID.put(n.getID(), PolygonsForID);
Collections.sort(PolygonsForID);
}

}
for(Map.Entry<Integer,List<POINTS>> count : PolygonsByID.entrySet()) {
int np =  count.getValue().size();
System.out.println("polygon : " + count.getKey() + " points : " + np);
int num_poly = 0;
Polygon[] polygons = new Polygon[4];
for (POINTS pnt : POLYGON) {
polygons[count.getKey()] = new Polygon(new int[] {pnt.getX()},new int[] {pnt.getY()},np);
num_poly ++;
g.setColor(Color.RED);
g.drawPolygon(polygons[count.getKey()]);
}
}

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: npoints > xpoints.length || npoints > ypoints.length

>```

23. ## Re: draw 4 different polygons.

The posted code does NOT follow the steps listed in post #15.
The first step was: Define array to hold Polygons
The code to do that was shown in post #17.

In the code in post #22, the array is NOT created first. It is deep inside some loops.

If you are not going to follow the steps I listed in post #15, Can you make a list of the steps you want to follow. Writing code BEFORE you have a good design is a waste of time.

24. ## Re: draw 4 different polygons.

hello
the procedure described in post # 15. is correct but I can not write the code