Sierpinski垫片有点

时间:2015-07-22 18:43:30

标签: java swing awt

我的Java课程分配有问题。我需要

  

实施Sierpinski垫圈。不要使用三角形或递归。这个   是只使用点的混沌版本。

以下是我实施的内容。我得到的只是前三个点:x,y和z。 while循环中存在一个问题(或问题)。任何有关如何处理此问题的建议将不胜感激。谢谢,我被卡住了!

SierpinskiGasket.java

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class SierpinskiGasket extends JFrame{

    //Class constructor
    public SierpinskiGasket(){
        Container c = getContentPane();
        JPanel jp = new JPanel();
        jp.setBackground(Color.white);
        c.add(jp);
        setTitle("Sierpinski Gasket");
        setSize(new Dimension(400,400));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        }

    public void paint(Graphics g){
        super.paint(g);
        int count = 0,
            vert  = 0;  
        Random rndm = new Random();

        //Create the three vertices of the triangle
        Point x=new Point(200,50),
              y=new Point(350,350),
              z=new Point(50, 350),
              current=x, target = null;


        if(count==0){ //Draw the three points of the vertices
            g.drawLine(x.x,x.y,x.x,x.y); //Top of the triangle
            g.drawLine(y.x,y.y,y.x,y.y); //Right of triangle
            g.drawLine(z.x,z.y,z.x,z.y); //Left of triangle

        }else{
            //The loop uses a random number to chose one of the three vertices of the triangle.
            //It then makes that point the target point
            while(count<1000){
                vert = rndm.nextInt(3);
                //Switch statement assigns one of the vertices to the target
                switch(vert){
                case 0: target=x; break;
                case 1: target=y; break;
                case 2: target=z; break;
                }   
                //Calculates the mid point between the current and the target
                current = midpoint(current, target);
                //Draws the point calculates in midpoint()
                g.drawLine(current.x, current.y, current.x, current.y);
                //Repaint
                repaint();
                //Increase the count
                count++;
            }

        }
    }
    /**
     * Calculates the midpoint between the two points
     * @param c
     * @param t
     * @return The midpoint
     */
    public Point midpoint(Point c, Point t){

            return new Point((Math.round((c.x+t.x)/2)), 
                              Math.round(((c.y+t.y)/2)));
        }
}

Gasket.java

public class Gasket {

    public static void main(String[] args) {

        new SierpinskiGasket();
    }

}

1 个答案:

答案 0 :(得分:1)

你的while循环根本就没有执行。这是您修复和清理的代码(不要过度评论您的代码):

public class SierpinskiGasket extends JFrame {

    public static void main(String[] args) {    
        new SierpinskiGasket();
    }

    // Class constructor
    public SierpinskiGasket() {
        Container c = getContentPane();
        JPanel jp = new JPanel();
        jp.setBackground(Color.white);
        c.add(jp);
        setTitle("Sierpinski Gasket");
        setSize(new Dimension(400, 400));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        Random rndm = new Random();

        Point pt0 = new Point(200, 50);
        Point pt1 = new Point(350, 350);
        Point pt2 = new Point(50, 350);
        Point current = pt0;

        g.setColor(Color.RED);

        drawPoint(g, pt0);
        drawPoint(g, pt1);
        drawPoint(g, pt2);

        Point[] pts = { pt0,pt1,pt2 };
        for (int i = 0 ; i < 10000 ; i++) {
            current = midpoint(current, pts[rndm.nextInt(3)]);
            drawPoint(g, current);
        }
    }

    private static void drawPoint(Graphics g, Point p) {
        g.drawLine(p.x, p.y, p.x, p.y);
    }

    /**
     * Calculates the midpoint between the two points
     * 
     * @param c
     * @param t
     * @return The midpoint
     */
    public Point midpoint(Point c, Point t) {    
        return new Point((Math.round((c.x + t.x) / 2)),
                Math.round(((c.y + t.y) / 2)));     
    }
}

你的count变量是本地的,所以总是重新初始化为0.我没有保留它,因为它使用了一个奇怪的递归,而你的循环就足够了。最后,paintComponent永远不应该致电repaint

除此之外,你的算法是正确的:)