Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
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;
/**
 * Class CanvasAndPixels demonstrates drawing directly into windows in JavaFX.
 *
 * @author Jeffrey Bergamini for CS 12J, jeffrey.bergamini@cabrillo.edu
 */
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 {
			// two portions of the point
			int x, y;

			// default constructor does nothing
			public Point() {
			}

			// constructor that takes two arguments
			public Point(int nx, int ny) {
				x = nx;
				y = ny;
			}
		}

		Group root = new Group(); // Serves as root of scene graph
		Scene scene = new Scene(root, width, height, Color.WHITE);
		Canvas canvas = new Canvas(width, height);
		root.getChildren().add(canvas); // Canvas is only node in this scene
										// graph

		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();

		// GraphicsContext graphics = canvas.getGraphicsContext2D(); // Like
		// Graphics2D

		// PixelWriter pw = graphics.getPixelWriter(); // For direct pixel
		// manipulation
		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..
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900