西尔宾斯基垫片带点

  • 本文关键字: 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();
    }
}

不执行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

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

相关内容

  • 没有找到相关文章

最新更新