How can I draw a star in java? The part that confuses me is the coordinates in g.drawline because I don't understand what order they are written in and how to get an idea of the correct coordinates to draw a star.
This is what I take from my code.
import java.awt.*;
import javax.swing.*;
class Pantalla extends Frame{
public static void main(String[]args)
{
Pantalla p=new Pantalla();
JOptionPane.showMessageDialog(null,"bienvenido sistema de algo");
}
public Pantalla()
{
this.setSize(1600,900);
setBackground(new Color(200,0,180));
this.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(new Color(255,0,0));
g.drawString("Hola",400,100);
g.drawLine(400,100,800,100);
g.drawLine(500,150,450,200);
g.drawLine(500,150,550,200);
g.drawLine(350,100,200,250);
}
}
Hello, although you are using the drawLine method, another alternative would be to use drawPolygon, which is a method to which you directly pass the coordinates and number of points.
Example to obtain the coordinates you can base a plane like the following image:
For more information on the drawPolygon method https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#drawPolygon-int:A-int:A-int-
Upper-left side is the point (0,0), that is, the initial point
To answer your question I will cite an example:
then that instruction will draw something like this:
I recommend you to make a polygon by programming and then paint it:
So you can easily change the size, position, tilt, make copies, etc... Imagine calculating by hand the points for 5 8-pointed stars for example... crazy.
As the center of the polygon is now (with the coordinates relative to the width and height of the frame) you can try to change the size of the window and you will see a nice star moving.
To see it better I suggest you clean the screen at the beginning of the method with
fillRect
or the previous will be painted.I attach the output of adding this code to yours: