•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 430,123 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,322 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1061 | Replies: 26
![]() |
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:
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);
}
} your vertex variable is holding a number too high, i am trying to figure out why
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
vertex is actually the right number,
found it,
your trying to draw with the new number of points before making the points, put your paintComponent(g); at the end of the method
found it,
your trying to draw with the new number of points before making the points, put your paintComponent(g); at the end of the method
Last edited by sciwizeh : Jul 20th, 2008 at 8:54 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
ok... the problem is right, i think, but the solution causes the same problem
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
well, i would re-arrange the two paint methods, to avoid using the arrays before making them.
i don't know whether it would make a difference to the returned array, but i would also try adding a call to trimToSize() call before toArray()
another thing you could do to avoid the casting to array type, you could look into the toArray(T[] a) method, but that isn't necessary
i don't know whether it would make a difference to the returned array, but i would also try adding a call to trimToSize() call before toArray()
another thing you could do to avoid the casting to array type, you could look into the toArray(T[] a) method, but that isn't necessary
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
that's weird.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
What algorithm is necessary to generate the points for the Koch Snowflake?
Are you omitting lines in the KochSnowflake?
Also, by the way I would not rely on the paint method or paintComponent method to have code in it without some kind of flag.
You could put a global-scoped boolean data type in your program and place an if statement that uses that boolean and code your graphics within it.
The reason for this? Everytime the window is resized your paint method will be called, along with paintComponent for JComponent.
As long as you understand the algorithm for generating this Koch Snowflake, you can code around the problem or start from scratch without much of a hassle, but I will try to look for the problem.
Sciwizeh was correct about the vertex getting more points than the existing points in the arrays. It may have been due to the JComponent being painted before you were capable of clicking the button (which I believe was stated before in a slightly different way).
Having some kind of flag may save you some trouble, but I'm not sure how much trouble it'll save you since your code mainly depends on the algorithm that produces the snowflake.
Are you omitting lines in the KochSnowflake?
Also, by the way I would not rely on the paint method or paintComponent method to have code in it without some kind of flag.
You could put a global-scoped boolean data type in your program and place an if statement that uses that boolean and code your graphics within it.
The reason for this? Everytime the window is resized your paint method will be called, along with paintComponent for JComponent.
As long as you understand the algorithm for generating this Koch Snowflake, you can code around the problem or start from scratch without much of a hassle, but I will try to look for the problem.
Sciwizeh was correct about the vertex getting more points than the existing points in the arrays. It may have been due to the JComponent being painted before you were capable of clicking the button (which I believe was stated before in a slightly different way).
Having some kind of flag may save you some trouble, but I'm not sure how much trouble it'll save you since your code mainly depends on the algorithm that produces the snowflake.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Koch Curve in Python (Python)
Other Threads in the Java Forum
- Previous Thread: compiler as the final year project
- Next Thread: Drag n Drop in same JList


Linear Mode