import javafx.application.Application;
import java.awt.event.*;
import javafx.stage.Stage;
import java.util.Random;
import java.awt.*;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.PixelWriter;
import javafx.scene.paint.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class chaos extends Application {
int width = 600;
int height = 600;
public int rand0to2() {
return (int) (Math.random() * 100) % 3;
}
private double prevX;
private double prevY;
public void start(Stage primaryStage) {
class Point {
int x, y;
public Point() {
}
public Point(int nx, int ny) {
x = nx;
y = ny;
}
}
Group root = new Group();
Scene scene = new Scene(root, width, height, Color.WHITE);
Canvas canvas = new Canvas(width, height);
root.getChildren().add(canvas);
Point[] triangle = new Point[3];
Point fp;
Point cp = new Point();
triangle[0] = new Point(width / 2, 0);
triangle[1] = new Point(0, height);
triangle[2] = new Point(width, height);
fp = new Point((int) (Math.random() * width), (int) (Math.random() * height / 2));
double R = Math.random();
double G = Math.random();
double B = Math.random();
double O = Math.random();
Color col = new Color(R, G, B, O);
GraphicsContext graphics = canvas.getGraphicsContext2D();
PixelWriter pw = graphics.getPixelWriter();
canvas.addMouseListener (new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
for (int i = 0; i < 10000; i++) {
int ra = rand0to2();
int x = triangle[ra].x;
int y = triangle[ra].y;
cp.x = (x + cp.x) / 2;
cp.y = (y + cp.y) / 2;
pw.setColor(cp.x, cp.y, col);
}
} else if (e.getButton() == MouseEvent.BUTTON3) {
graphics.clearRect(0, 0,width, height);
}
}
});
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
}
What I have tried:
I tried to make it so that the primary click would run 10000 iterations everytime i click, and the secondary click will just clear the canvas.
I have looked for help through google on stackoverflow, java api , youtube and other places..