This site requires JavaScript, please enable it in your browser!
Greenfoot back
PlanetUlme03
PlanetUlme03 wrote ...

2018/3/10

How to make a Triangle?

PlanetUlme03 PlanetUlme03

2018/3/10

#
drawPolygon(int xPoints, int yPoints, int nPoints) i found this, but i dont knew how to use it. Could you help me?
Super_Hippo Super_Hippo

2018/3/10

#
1
2
3
4
5
6
7
8
9
10
public MyWorld()
{
    super(300, 300, 1);
    GreenfootImage img = new GreenfootImage(300, 300);
    img.setColor(Color.WHITE);
    img.fill();
    img.setColor(Color.BLACK);
    img.drawPolygon(new int[] {100, 100, 200}, new int[] {100, 200, 100}, 3);
    setBackground(img);
}
This creates a triangle with corners at (100|100), (100|200) and (200|100).
danpost danpost

2018/3/10

#
PlanetUlme03 wrote...
1
2
3
drawPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
i found this, but i dont knew how to use it. Could you help me?
For a triangle 'nPoints' would be '3' and 'xPoints' and 'yPoints' would both be int arrays of length 3. Let us say you had an image of size 100x100. To draw a triangle that goes from bottom-left corner to bottom-right corner to top-left corner (back to bottom-left corner), then you have this:
1
2
3
4
GreenfootImage image = new GreenfootImage(100, 100);
int[] xPoints = new int[] { 0, 99, 0 };
int[] yPoints = new int[] { 99, 99, 0 };
image.drawPolygon(xPoints, yPoints, 3);
You need to login to post a reply.