Draw figures in Java

2

How can I draw a star in java? The part that confuses me is that of the coordinates in g.drawline because I do not understand in what order they are written and how to give me an idea of the correct coordinates to draw a star.

This is what I have 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);
}
}
    
asked by Stephanie B Bautista 07.02.2017 в 00:45
source

3 answers

3

Hello, although you are using the drawLine method, another alternative would be to use drawPolygon which is a method to which you pass directly the coordinates and number of points.

Example to obtain the coordinates you can base a plan like the following image:

public class MainWindow extends JFrame {


    public MainWindow()
    {
        setBounds(0, 0, 300, 300);
        setBackground(new Color(200,0,180));
        setVisible(true);
    }

    public void paint(Graphics g)
    {
        int[] coordenadasX={130,150,210,160,180,130,80,100,50,110};
        int[] coordenadasY={20,90,90,130,200,160,200,130,90,90};
        int nPuntos=10;

        g.drawPolygon(coordenadasX, coordenadasY, nPuntos);
    }

    public static void main(String[] args) {
        MainWindow ventana=new MainWindow();
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

For more information about the drawPolygon method link

    
answered by 07.02.2017 / 01:50
source
2

Top-left side is the point (0,0), that is, the starting point

  

I do not understand what order they are written in?

to answer your question I will quote a example:

  

g.drawLine (iniX, iniY, finX, finY);

     

g.drawLine (400,100,100,200);

     

to draw a finite line you need 2 points therefore:

     

p1 = (400,100) and p2 = (100,200)

then that instruction would draw something like this:

    
answered by 07.02.2017 в 01:05
0

I recommend you do a polygon by programming and then paint it:

Polygon poligono = new Polygon();

//Numero de puntas del poligono
int puntas = 5;
//Distancia de la punta hasta centro del poligono
double radioExterior = 40;
//Distancia del valle interior hasta el centro
double radioInterior = 20;
//Coordenadas en el frame para del centro del poligono
double xCentro = getWidth()/2, yCentro = getHeight()/2;
//Inclinacion inicial si no queremos que la punta este en angulo 0
double inclinacionPrimeraPunta = Math.PI;//Punta hacia arriba

//Calculamos los puntos por trigonometria
double anguloEntreVertices = Math.PI/puntas;
for(int i = 0; i < puntas*2; i++){
    //Vamos alternando entre los radios de punta y valle
    double r = (i%2 == 0)?radioExterior:radioInterior;
    double a = i*anguloEntreVertices + inclinacionPrimeraPunta;
    //Añadimos el nuevo punto al poligono
    poligono.addPoint(
        //Coordenada X
        (int)(r*Math.sin(a) + xCentro),
        //Coordenada Y
        (int)(r*Math.cos(a) + yCentro));
}

//Pintamos el poligono
g.drawPolygon(poligono);

So you can easily change the size, position, tilt, make copies, etc ... Imagine calculating by hand the points for 5 stars of 8 points 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 moving star.

To see it better I suggest you clean the screen at the beginning of the method with fillRect or the above will be painted.

I attached the output of adding this code to yours:

    
answered by 07.02.2017 в 09:05