Brow Brow - 2 months ago 8
Java Question

Why doesn't both my Polygons have a border when i use setStroke?

I am drawing two stars using:

public void draw(Graphics2D g2) {
g2.drawPolygon(xCoordOfStar, yCoordOfStar, POINTS);
g2.setStroke(new BasicStroke(5));
}


and:

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Star star1 = new Star(100,200,300);
Star star2 = new Star(200,200,300);
star1.draw(g2);
star2.draw(g2);
}


In the other class.

For some reason unknown to me, only the bigger star (
star2
) gets a thicker border, while
star1
does not get any border. What am I doing wrong?

Answer

It's an ordering issue. Your second Polygon gets a border because your first Polygon called the g2.setStroke(new BasicStroke(5)); Comment out the first star code, the second star now also loses its border.

To fix it, you just need to rearrange the methods:

public void draw(Graphics2D g2) {
    g2.setStroke(new BasicStroke(5));
    g2.drawPolygon(xCoordOfStar, yCoordOfStar, POINTS);
}
Comments