Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

koch snowflake in JAVA

$
0
0
i have a program that recursively produces the koch snowflake on the click of a button. Im stuck at my last method.

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.awt.geom.GeneralPath;
import java.awt.Polygon;

public class KochComponent extends JComponent
{
   private int numIterations;

   public KochComponent()
   {
      numIterations = 1;
   }

   public void next()
   {
      numIterations++;
      repaint();
   }
   
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;

      int length = Math.min(getWidth(), getHeight()) * 2 / 3;

      int x1 = 10;
      int y1 = length / 2;
      int x2 = x1 + length;
      int y2 = y1;
      int x3 = x1 + (length / 2);
      int y3 = y1 + length;
      draw(g2, numIterations, x1, y1, x2, y2);
      draw(g2, numIterations, x2, y2, x3, y3);
      draw(g2, numIterations, x3, y3, x1, y1);
   }

   private void draw(Graphics2D g2, int iteration, 
      double x1, double y1, double x2, double y2)
   {
    /*********I dont know how to implement this method*****/
   }
}




component
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.awt.geom.GeneralPath;
import java.awt.Polygon;

public class KochComponent extends JComponent
{
   private int numIterations;

   public KochComponent()
   {
      numIterations = 1;
   }

   public void next()
   {
      numIterations++;
      repaint();
   }
   
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;

      int length = Math.min(getWidth(), getHeight()) * 2 / 3;

      int x1 = 10;
      int y1 = length / 2;
      int x2 = x1 + length;
      int y2 = y1;
      int x3 = x1 + (length / 2);
      int y3 = y1 + length;
      draw(g2, numIterations, x1, y1, x2, y2);
      draw(g2, numIterations, x2, y2, x3, y3);
      draw(g2, numIterations, x3, y3, x1, y1);
   }

   private void draw(Graphics2D g2, int iteration, double x1, double y1, double x2, double y2)
   {
      . . .
   }
}



main

import javax.swing.JFrame;

public class KochViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new KochFrame();
      frame.setTitle("KochViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>