0

The fractal is called Vicsek fractal .When I set midpoint's x and y values to = firstDot's x and y values / 3, i get the fractal, but my fractal is very small, and off to the side. However, when I set it to equal firstDot's values * 3/2, what is suppose to be, only the square prints, and not the fractal.

package fractals;

import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
*
* @author Joshua
*/
public class SquarePanel extends JPanel {

static final int[] xPoints = new int[]{270, 270, 810, 810};//Square x coordiantes
static final int[] yPoints = new int[]{92, 632, 632, 92};//Square y coordinates
int firstDotx = 270 + (int) (Math.random() * ((810 - 270) + 1)); 
int firstDoty = 92 + (int) (Math.random() * ((632 - 92) + 1));

public final static int iterations = 100000;

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.black);
    g2.drawPolygon(xPoints, yPoints, 4);
    g2.drawRect(firstDotx, firstDoty, 0, 0);
    for (int i = 0; i <= iterations; i++) {
        int midpointX;
        int midpointY;
        int Vertex = (int) (Math.random() * 5 + 1);
        switch (Vertex) { 
            case 1: //top left corner
                g2.setColor(Color.blue);
                midpointX = (firstDotx + 270) / 3;
                midpointY = (firstDoty + 92) / 3;
                g2.drawRect(midpointX, midpointY, 0, 0);
                firstDotx = midpointX;
                firstDoty = midpointY;
                break;
            case 2://bottom left
                g2.setColor(Color.blue);
                midpointX = (firstDotx + 270) / 3;
                midpointY = (firstDoty + 632) / 3;
                g2.drawRect(midpointX, midpointY, 0, 0);
                firstDotx = midpointX;
                firstDoty = midpointY;
                break;
            case 3://bottom right
                g2.setColor(Color.blue);
                midpointX = (firstDotx + 810) / 3;
                midpointY = (firstDoty + 632) / 3;
                g2.drawRect(midpointX, midpointY, 0, 0);
                firstDotx = midpointX;
                firstDoty = midpointY;
                break;
            case 4://bottom left
            g2.setColor(Color.blue);
                midpointX = (firstDotx + 810) / 3;
                midpointY = (firstDoty + 92) / 3;
                g2.drawRect(midpointX, midpointY, 0, 0);
                firstDotx = midpointX;
                firstDoty = midpointY;
                break;
                case 5:
                g2.setColor(Color.red);
                midpointX = (firstDotx + 540) / 3;
                midpointY = (firstDoty + 384) / 3;
                g2.drawRect(midpointX, midpointY, 0, 0);
                firstDotx = midpointX;
                firstDoty = midpointY;
                break;
            default:
                break;
            
        }
    }
}
}

This is the class file for the Frame, im planning to add a GUI, with buttons, but I havn't gotten there yet.

package fractals;

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
*
* @author Joshua
*/
public class SqaureFrame {
 public static void main(String[] args) {
    JFrame Sframe = new JFrame("Vicsek Fractal");
    Sframe.setSize(1080, 768);
    Sframe.setLocation(300, 200);
    Sframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
    SquarePanel Vicsek = new SquarePanel();
    JButton Square = new JButton("Click for the Vicsek Fractal");
    Sframe.add(Vicsek);
    Sframe.setVisible(true);
}
}

1 Answers1

1

Well thanks to everyone who looked at it, but I managed to solve it. I noticed that the size of my fractal was the same size as a quarter of the square, so I took the coordinates of each of the vertices in the square (the "+270" for example) and doubled them. My fractal now prints correctly.

For instance:

midpointY = (firstDoty + 92*2) / 3