0% found this document useful (0 votes)
5 views

Random Shape Generator

java

Uploaded by

xennyokonti
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Random Shape Generator

java

Uploaded by

xennyokonti
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import javafx.application.

Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ShapeGeneratorApp extends Application {

private static final int WINDOW_WIDTH = 800;


private static final int WINDOW_HEIGHT = 600;
private static final int SHAPE_COUNT = 20;

private Pane canvas;


private Shape selectedShape;
private List<Shape> shapes = new ArrayList<>();
private ColorPicker shapeColorPicker;
private ColorPicker backgroundColorPicker;

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Shape Generator App");

// Initialize the root layout


BorderPane root = new BorderPane();

// Create the canvas


canvas = new Pane();
canvas.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT);
root.setCenter(canvas);

// Create toolbars
ToolBar toolBar = new ToolBar();
shapeColorPicker = new ColorPicker(Color.BLACK);
shapeColorPicker.setOnAction(e ->
changeShapeColor(shapeColorPicker.getValue()));
backgroundColorPicker = new ColorPicker(Color.WHITE);
backgroundColorPicker.setOnAction(e ->
changeBackgroundColor(backgroundColorPicker.getValue()));
toolBar.getItems().addAll(new ColorPicker(Color.BLACK), shapeColorPicker,
new ColorPicker(Color.WHITE), backgroundColorPicker);

// Create a menu bar


MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
MenuItem clearCanvasItem = new MenuItem("Clear Canvas");
clearCanvasItem.setOnAction(e -> clearCanvas());
MenuItem exitItem = new MenuItem("Exit");
exitItem.setOnAction(e -> primaryStage.close());

menuFile.getItems().addAll(clearCanvasItem, exitItem);
menuBar.getMenus().add(menuFile);

root.setTop(menuBar);
root.setBottom(toolBar);

// Generate random shapes


generateRandomShapes();

// Set up event handling


canvas.setOnMouseClicked(this::selectShape);

// Set up the scene


Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
primaryStage.setScene(scene);
primaryStage.show();
}

private void generateRandomShapes() {


Random random = new Random();

for (int i = 0; i < SHAPE_COUNT; i++) {


Shape shape;
double x = random.nextDouble() * WINDOW_WIDTH;
double y = random.nextDouble() * WINDOW_HEIGHT;

switch (random.nextInt(3)) {
case 0:
shape = new Circle(x, y, random.nextDouble() * 50 + 10,
randomColor(random));
break;
case 1:
shape = new Rectangle(x, y, random.nextDouble() * 50 + 10,
random.nextDouble() * 50 + 10, randomColor(random));
break;
case 2:
shape = new Ellipse(x, y, random.nextDouble() * 50 + 10,
random.nextDouble() * 30 + 10, randomColor(random));
break;
default:
shape = new Circle(x, y, random.nextDouble() * 50 + 10,
randomColor(random));
break;
}

shapes.add(shape);
canvas.getChildren().add(shape);
}
}

private Color randomColor(Random random) {


return Color.rgb(random.nextInt(256), random.nextInt(256),
random.nextInt(256));
}

private void selectShape(MouseEvent event) {


Shape clickedShape = (Shape) event.getTarget();

if (selectedShape != null) {
selectedShape.setStroke(null);
}

selectedShape = clickedShape;
selectedShape.setStroke(Color.BLUE);
}

private void changeShapeColor(Color color) {


if (selectedShape != null) {
if (selectedShape instanceof Circle) {
((Circle) selectedShape).setFill(color);
} else if (selectedShape instanceof Rectangle) {
((Rectangle) selectedShape).setFill(color);
} else if (selectedShape instanceof Ellipse) {
((Ellipse) selectedShape).setFill(color);
}
}
}

private void changeBackgroundColor(Color color) {


canvas.setStyle("-fx-background-color: #" + color.toString().substring(2,
8));
}

private void clearCanvas() {


canvas.getChildren().clear();
shapes.clear();
generateRandomShapes();
}
}

You might also like