•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,225 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 2,233 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: 1030 | Replies: 26
![]() |
•
•
•
•
I placed int variables in the draw method, and the paint method that are derived from the size of arrylist x. After draw is done being called, the size is 6. After the program moves on to paint, the size is 0. This is where the problem lies.
can you post your revised code? if you can't see it maybe we can
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
Consider the following for your KochComponent. All calculations are done outside of the paintComponent method (you don't need to override paint() at all). For each iteration, you need to add three intermediate points to each line segment to form the triangle portion.
java Syntax (Toggle Plain Text)
public class KochComponent extends JComponent { List<Point> points = null; /** create the initial triangle */ private void createTriangle() { points = new ArrayList<Point>(); int length = Math.min(getWidth(), getHeight())*2/3; int x1 = (getWidth()-length)/2; int y1 = length/2; points.add(new Point(x1, y1)); int x2 = x1+length; int y2 = y1; points.add(new Point(x2, y2)); int x3 = x1+(length/2); int y3 = y1+length; points.add(new Point(x3, y3)); } public void next() { numIterations++; List<Point> newPoints = new ArrayList<Point>(); // generate the new points for each line segment for(int i = 0; i<points.size(); i++) { Point p1 = points.get(i); Point p2 = i==points.size()-1 ? points.get(0) : points.get(i+1); newPoints.add(p1); newPoints.addAll(getKochPoints(p1, p2)); } points = newPoints; repaint(); } public void paintComponent(Graphics g) { // this is just to account for the fact that // the component has no initial width or height if(points==null) { createTriangle(); } Graphics2D g2 = (Graphics2D)g; // Use this code to draw the actual poly // Polygon koch = new Polygon(); // for (Point p : points){ // koch.addPoint(p.x, p.y); // } // g2.draw(koch); // This just draws unconnected points // for demo purposes for(Point p : points) { g2.drawRect(p.x, p.y, 1, 1); } } /** Generate the points to add for each segment * for each iteration. * I am just putting in the easy ones here. * You get to do the trickier one! */ private List<Point> getKochPoints(Point p1, Point p2) { List<Point> kochPoints = new ArrayList<Point>(); int dx = p2.x-p1.x; int dy = p2.y-p1.y; Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3); kochPoints.add(kp1); // you have to figure out how to generate // kp2 here (the top of the triangle) Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3); kochPoints.add(kp3); return kochPoints; } private int numIterations; }
Yes, java.awt.Point. It's part of the JDK, not a custom class.
So, after toying with the code you gave me, I've run into a problem. While I can supposedly get the next iteration to appear, all the others after that are incorrect.
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;
import java.awt.Point;
public class KochComponent3 extends JComponent {
ArrayList<Point> points = null;
/** create the initial triangle */
private void createTriangle() {
points = new ArrayList<Point>();
int length = Math.min(getWidth(), getHeight())*2/3;
int x1 = (getWidth()-length)/2;
int y1 = length/2;
points.add(new Point(x1, y1));
int x2 = x1+length;
int y2 = y1;
points.add(new Point(x2, y2));
int x3 = x1+(length/2);
int y3 = y1+length;
points.add(new Point(x3, y3));
}
public void next() {
numIterations++;
ArrayList<Point> newPoints = new ArrayList<Point>();
// generate the new points for each line segment
for(int i = 0; i<points.size(); i++) {
Point p1 = points.get(i);
Point p2 = i==points.size()-1 ?
points.get(0) : points.get(i+1);
newPoints.add(p1);
newPoints.addAll(getKochPoints(p1, p2));
}
points = newPoints;
repaint();
}
public void paintComponent(Graphics g)
{
if(points==null) {
createTriangle();
}
Graphics2D g2 = (Graphics2D)g;
Polygon koch = new Polygon();
for (Point p : points){
koch.addPoint(p.x, p.y);
}
g2.draw(koch);
}
/** Generate the points to add for each segment
* for each iteration.
* I am just putting in the easy ones here.
* You get to do the trickier one!
*/
private ArrayList<Point> getKochPoints(Point p1, Point p2) {
ArrayList<Point> kochPoints = new ArrayList<Point>();
int dx = p2.x-p1.x;
int dy = p2.y-p1.y;
double xtra = dx / 3;
double use = xtra * xtra;
int test = (int)(Math.sqrt(use-(use/36)));
int test2 = p1.y+dy/3-test;
Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3);
kochPoints.add(kp1);
if(p1.y == p2.y)
{
Point kp2 = new Point((p1.x+(dx/3)+((dx/3)/2)), ((p1.y+dy/3)-test));
kochPoints.add(kp2);
}
else if(p1.x > p1.y)
{
Point kp2 = new Point(p1.x, p1.y+(dy/3)+test+11);
kochPoints.add(kp2);
}
else
{
Point kp2 = new Point(p1.x+(dx), p1.y+(2*dy/3) + test+11);
kochPoints.add(kp2);
}
Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3);
kochPoints.add(kp3);
return kochPoints;
}
private int numIterations;
}![]() |
•
•
•
•
•
•
•
•
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