Reputation: 1
I want to modify my code so it will ask me how much arms I want my Star to have and then draw me the Star with the given number of arms. I don't know how to do it with Math.PI using triangles for example. Can you please help me?
This is what I have so far. Just a program drawing a Star without user input.
import java.awt.BasicStroke;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Path2D;
import java.util.Scanner;
public class App extends Frame
{
public static void main(String[] args)
{
Scanner czytnik = new Scanner(System.in);
System.out.println("Ile ramion ma mieć gwiazda?");
new App();
czytnik.close();
}
public App()
{
setSize(540, 380);
setVisible(true);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setStroke (new BasicStroke (15.0f));
Rectangle r = getBounds();
float width = 166;
float height = 166;
g2.translate( r.getWidth() / 2 - width/2, r.getHeight() / 2 - height/2);
Path2D star = new Path2D.Float ();
star.moveTo (width/5F, height-1);
star.lineTo (width/2F, 0);
star.lineTo (4*width/5F, height-1);
star.lineTo (0, 2*height/5F);
star.lineTo (width-1, 2*height/5F);
star.closePath ();
g2.draw (star);
g2.fill (star);
}
}
Upvotes: 0
Views: 537
Reputation: 1493
Imagine you have two circles, they have the same center point and one is larger than the other.
Actually I made a quick picture:
You could imagine the inner circle has a radius of 1, the outer circle has a radius of two. Those numbers could both be changeable in your code.
The star has 5 points, so both circles are divided into 5 sections by equally placing 5 points on the circle.
The inner circle is rotated half the distance of one of the divided sections.
Then lines are drawn connecting the points.
This is how I would start trying to make a star-creator class, but it is just a concept so you'll have to find the details for yourself.
Upvotes: 1