Hoy escribiré sobre la implementación recursiva de los copos de nieve de Koch de nivel K (el valor K requiere que ingreses manualmente). En cuanto a lo que son los copos de nieve de Koch, por favor Baidu.
Primero, pensemos en cómo escribir este programa. Cuando cuente = 0, debería ser un triángulo. Estos tres puntos se determinan desde el principio, y se desarrollarán cambios futuros en función de estos tres puntos. Cuando no es 0, debe calcular los 9 puntos en relación con este triángulo, a saber, los dos puntos en cada borde y el tercer vértice del triángulo correspondiente a él.
Primero, agregue un panel en Jframe, y necesitamos dibujar una imagen en este panel.
Echemos un vistazo a esta foto nuevamente. Esta imagen introduce el proceso de calcular los otros tres puntos a través de dos puntos.
Ahora comience a dibujar en el panel:
La clase estática showPanel extiende jpanel {int number = 0; public void setNumber (int number) {this.number = number; repintado (); } public void PaintComponent (Graphics g) {super.PaintComponent (g); // dibujar un panel simple int aparte = (int) (math.min ((int) getWidth (), (int) getheight ())*0.8); int high = (int) (lado*math.cos (math.toradians (30))); Punto p1 = nuevo punto (getWidth () / 2, 10); Punto p2 = nuevo punto (getwidth () / 2 - lado / 2, 10 + alto); Punto p3 = nuevo punto (getWidth () / 2 + lado / 2, 10 + alto); Playkochsnowflake (G, número, P1, P2); Playkochsnowflake (G, número, P2, P3); Playkochsnowflake (G, número, P3, P1); }Ahora comience a escribir funciones recursivas.
public static void playkochsnowflake (Graphics G, int número, punto p1, punto p2) {if (number == 0) {g.drawline (p1.x, p1.y, p2.x, p2.y); } else {int deltax = p2.x - p1.x; int deltay = p2.y - p1.y; Punto x = nuevo punto (p1.x + deltax / 3, p1.y + deltay / 3); Punto y = nuevo punto (p1.x + deltax * 2/3, p1.y + deltay * 2 /3); Punto y = nuevo punto (p1.x + deltax * 2 /3); Punto z = nuevo punto ((int) ((p1.x + p2.x) / 2 + math.sin (math.toradians (60)) * (p1.y - p2.y) / 3), (int) ((p1.y + p2.y) / 2 + math.sin (math.toradians (60)) * (p2.x - p1.x) / 3)); Playkochsnowflake (G, número - 1, p1, x); Playkochsnowflake (G, número - 1, x, z); Playkochsnowflake (G, número - 1, z, y); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, y, p2); }}Luego agregue un jTA JTextfield al panel principal. Los datos en los que ingresan se transmitirán al número. Así que agrega un oyente. Si ya hay entrada de datos, llame a la función setNumber () en ella para establecer la variable de número.
jta.addactionListener (new ActionListener () {public void ActionPerformed (ActionEvent Arg0) {Spl.SetNumber (Integer.ParseInt (jta.gettext ()));}}); Por lo tanto, en general se ha completado, y el resto es la configuración de formulario de respuesta corta.
Aquí hay un código Java completo:
import java.awt.borderlayut; import java.awt.flowlayout; import java.awt.graphics; import java.awt.point; import java.awt.event.actionEvent; import java.awt.event.actionListener; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jTextfield; Public Class Snowflake extiende Jframe {private JTextfield JTA = new JTextField (5); showPanel privado Spl = new ShowPanel (); La clase estática showPanel extiende jpanel {int number = 0; public void setNumber (int number) {this.number = number; repintado (); } public void PaintComponent (Graphics g) {super.PaintComponent (g); // dibujar un panel simple int aparte = (int) (math.min ((int) getWidth (), (int) getheight ())*0.8); int high = (int) (lado*math.cos (math.toradians (30))); Punto p1 = nuevo punto (getWidth () / 2, 10); Punto p2 = nuevo punto (getwidth () / 2 - lado / 2, 10 + alto); Punto p3 = nuevo punto (getWidth () / 2 + lado / 2, 10 + alto); Playkochsnowflake (G, número, P1, P2); Playkochsnowflake (G, número, P2, P3); Playkochsnowflake (G, número, P3, P1); } public static void playkochsnowflake (gráficos g, int número, punto p1, punto p2) {if (number == 0) {g.drawline (p1.x, p1.y, p2.x, p2.y); } else {int deltax = p2.x - p1.x; int deltay = p2.y - p1.y; Punto x = nuevo punto (p1.x + deltax / 3, p1.y + deltay / 3); Punto y = nuevo punto (p1.x + deltax * 2/3, p1.y + deltay * 2 /3); Punto y = nuevo punto (p1.x + deltax * 2 /3); Punto z = nuevo punto ((int) ((p1.x + p2.x) / 2 + math.sin (math.toradians (60)) * (p1.y - p2.y) / 3), (int) ((p1.y + p2.y) / 2 + math.sin (math.toradians (60)) * (p2.x - p1.x) / 3)); Playkochsnowflake (G, número - 1, p1, x); Playkochsnowflake (G, número - 1, x, z); Playkochsnowflake (G, número - 1, z, y); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, z); Playkochsnowflake (G, número - 1, y, p2); }}} public Snowflake () {JPanel Panel = new JPanel (); panel.setLayout (new FlowLayout ()); panel.add (new JLabel ("Ingrese el número")); panel.add (JTA); Agregar (SPL, BorderLayout.Center); Agregar (panel, borderLayout.South); jta.addactionListener (new ActionListener () {public void ActionPerformed (ActionEvent Arg0) {Spl.SetNumber (Integer.ParseInt (jta.gettext ()));}}); } public static void main (string args []) {Snowflake Snowflake = new Snowflake (); copo de nieve.setsize (300, 300); copo de nieve.settitle ("copo de nieve"); Snowflake.SetLocationRelativeto (NULL); Snowflake.setVisible (verdadero); }}Imagen de reproducción:
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.