Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class MandelbrotGenerator here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- import javafx.scene.input.KeyCode;
- import javafx.animation.Animation;
- public class MandelbrotGenerator extends Application
- {
- // intstance variables
- static final int screenWidth = 900;
- static final int screenHeight = 450;
- static final int scale = 0;
- static final int maxIterations = 255;
- @Override
- public void start(Stage stage)
- {
- Canvas canvas = new Canvas(screenWidth, screenHeight);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- Timeline tl = new Timeline(new KeyFrame(Duration.millis(100), e -> run(gc)));
- tl.setCycleCount(Timeline.INDEFINITE);
- canvas.setFocusTraversable(true);
- // handle mouse and key events
- canvas.setOnKeyPressed(e ->
- {
- if(e.getCode() == KeyCode.W)
- {
- // do something
- }
- });
- canvas.setOnMouseClicked(e ->
- {
- // do something
- });
- stage.setTitle("Mandelbrot Set Generator");
- stage.setScene(new Scene(new StackPane(canvas)));
- stage.show();
- tl.play();
- }
- private void run(GraphicsContext gc)
- {
- // color for background
- gc.setFill(Color.BLACK);
- gc.fillRect(0, 0, screenWidth, screenHeight);
- for (int row = 0; row < screenWidth; row++) {
- for (int col = 0; col < screenHeight; col++) {
- checkPoint(row, col, gc);
- }
- }
- }
- //determinees number of iterations, calls colorPoint if in set
- public static void checkPoint(int x, int y, GraphicsContext gc) {
- int numIterations = 0;
- int previousSum = 0;
- int c = getDistanceSquared(x, y);
- while (numIterations < maxIterations && previousSum < 4) {
- previousSum = zFuntionOutput(c, previousSum);
- }
- System.out.println("here");
- if (numIterations < maxIterations)
- colorPoint(x, y, numIterations, gc);
- }
- //draw and assign color
- public static void colorPoint(int x, int y, double iterations, GraphicsContext gc) {
- iterations = 1 / (iterations + 1);
- gc.setFill(new Color(iterations, 1 - iterations, iterations, 1));
- gc.fillRect(x, y, 1, 1);
- }
- public static int zFuntionOutput(int c, int previousSum) {
- return previousSum * previousSum + c;
- }
- //
- public static int getDistanceSquared(int x, int y) {
- return translateX(x) * translateX(x) + translateY(y) * translateY(y);
- }
- /*
- @param: takes the X value fromt the top left coordinate system
- @postcondition: returns the X coordinate on the cartesian cooridnate system
- */
- public static int translateX(int x) {
- if (x < screenWidth / 2) {
- return x - screenWidth / 2;
- }
- else if (x > screenWidth / 2) {
- return x / 2;
- }
- else {
- return 0;
- }
- }
- /*
- @param: takes the Y value fromt the top left coordinate system
- @postcondition: returns the Y coordinate on the cartesian cooridnate system
- */
- public static int translateY(int y) {
- if (y < screenHeight / 2) {
- return y + screenHeight / 2;
- }
- else if (y > screenHeight / 2) {
- return -y / 2;
- }
- else {
- return 0;
- }
- }
- // run program
- public static void main(String[] args)
- {
- Application.launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement