Dibujo figuras 2D

Aqui les dejo un ejemplo de como dibujar figuras en 2D:


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class FLRectas extends JFrame{

/** Creates a new instance of FLRectas */
public FLRectas() {
super("Dibujo figuras 2D");
getContentPane().setBackground(Color.WHITE);
setSize(400,400);
setFocusable(true);
setVisible(true);
setResizable(false);
}
public void paint(Graphics g){

super.paint(g);
int puntosX[] = {55,67,109,73,83,55,27,37,1,43};
int puntosY[] = {0,36,36,54,96,72,96,54,36,36};

Graphics2D gd2 = (Graphics2D)g;
GeneralPath estrella = new GeneralPath(); //crear objeto GeneralPath

estrella.moveTo(puntosX[0],puntosY[0]); //Coordenadad inicial

//Crear la estrella pero esto no la dibuja
for(int i=1;i < puntosX.length;i++){
estrella.lineTo(puntosX[i],puntosY[i]);
}
estrella.closePath();//cerrar la figura
gd2.translate(200,200);//trasladar el origen
for(int j=1;j < = 20;j++){
gd2.rotate(Math.PI/10.0); //girar las coordenadas
gd2.setColor(new Color(((int) (Math.random()*256) ),((int) (Math.random()*256)),((int) (Math.random()*256))));
gd2.fill(estrella);
}
}
public static void main(String[] args) {
FLRectas pint=new FLRectas();
pint.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

0 comentarios: