I need to write a program that draws the next iteration if a koch snowflake, when a button is clicked. It begins with a equilateral triangle. The program uses an ArrayList, Polygon, and GeneralPath, as I found these imports in the starter code. I think I may also be supposed to use recursion, but I'm unsure of where to do so, or how it would even help.
Thus far my code draws the initial triangle, and displays the button for drawing the enxt iteration. When clicked, I get an error message, stating that npoints > xpoints.length or ypoints.length. These are all constructors for the polygon, xpoints and ypoints are arrays of the x and y values, and npoints is the total number of points. As you can see my draw method adds the points to arraylists, and these lists are later converter to arrays, as the constructor for a polygon requires them to be. I believe my issue must be there. Either with the conversion, or the arraylist itself.
My Code:
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
{
public KochComponent()
{
numIterations = 1;
x = new ArrayList<Integer>();
y = new ArrayList<Integer>();
}
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)
{
x.add((int)x1);
x.add((int)x2);
y.add((int)y1);
y.add((int)y2);
double n = 3 * Math.pow(4, iteration-1);
vertex = (int)n;
}
public void paint(Graphics g)
{
paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
super.paint(g2);
Object[] obj = x.toArray();
Object[] obj2 = y.toArray();
xPoints = new int[obj.length];
yPoints = new int[obj2.length];
for (int k = 0; k < obj.length; k++)
{
xPoints[k] = ((Integer)obj[k]).intValue();
}
for (int l = 0; l < obj2.length; l++)
{
yPoints[l] = ((Integer)obj2[l]).intValue();
}
Polygon koch = new Polygon(xPoints, yPoints, vertex);
path = new GeneralPath(koch);
path.moveTo(xPoints[0], yPoints[0]);
for (int i = 1; i < xPoints.length; i++)
{
path.lineTo(xPoints[i], yPoints[i]);
}
path.closePath();
g2.draw(path);
}
private int numIterations;
private ArrayList<Integer> x;
private ArrayList<Integer> y;
private GeneralPath path;
private int vertex;
private int[] xPoints;
private int[] yPoints;
}
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class KochFrame extends JFrame
{
public KochFrame()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("KochViewer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(makeNextButton());
component = new KochComponent();
component.setPreferredSize(new Dimension(
COMPONENT_WIDTH, COMPONENT_HEIGHT));
panel.add(component);
add(panel);
}
private JButton makeNextButton()
{
JButton button = new JButton("Next");
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
component.next();
}
}
button.addActionListener(new ButtonListener());
return button;
}
private static final int FRAME_WIDTH = 360;
private static final int FRAME_HEIGHT = 500;
private static final int COMPONENT_WIDTH = 300;
private static final int COMPONENT_HEIGHT = 400;
private KochComponent component;
}
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);
}
}