Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created March 29, 2021 22:02
Show Gist options
  • Save jananpatel2002/8672eaca4e27ce7a7ed2624a2adb4a70 to your computer and use it in GitHub Desktop.
Save jananpatel2002/8672eaca4e27ce7a7ed2624a2adb4a70 to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 3/29/2021
* Course Number: CSC 112
* Course Name: Computer Science
* Problem Number: 8
* Email:jkpatel2001@student.stcc.edu
* Short Description of the Problem: Making an Etch-A-Sketch
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.control.TextArea;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
public class EtchASketch extends Application {
private Stage primaryStage;
private SketchPane sketchPane;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.sketchPane = new SketchPane();
this.sketchPane.setId("sketchy");
AboutPane aboutPane = new AboutPane();
aboutPane.getStyleClass().add("doBackGround");
aboutPane.setId("aboutPane");
OperationsPane opsPane = new OperationsPane();
opsPane.setId("opsPane");
opsPane.getStyleClass().add("doBackGround");
BorderPane bp = new BorderPane();
bp.setCenter(sketchPane);
bp.setBottom(aboutPane);
bp.setRight(opsPane);
Scene scene = new Scene(bp, 700, 500);
primaryStage.setTitle("Etch-A-Sketch");
primaryStage.setScene(scene);
scene.getStylesheets().add(EtchASketch.class.getResource("EtchASketch.css").toExternalForm());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
class AboutPane extends HBox {
public AboutPane() {
this.setSpacing(10);
this.setAlignment(Pos.CENTER);
this.getChildren().addAll(new Label("Etch-A-Sketch by Janan Patel"));
}
}
class OperationsPane extends VBox {
public OperationsPane() {
this.setSpacing(20);
this.setPrefWidth(120);
this.setAlignment(Pos.CENTER);
var btnViewLines = new Button("View Lines");
btnViewLines.setPrefWidth(100);
btnViewLines.setOnAction(e -> sketchPane.viewListOfNodes());
var btnCloseApp = new Button("Close");
btnCloseApp.setPrefWidth(100);
btnCloseApp.setOnAction(e -> System.exit(0));
var btnOpenApp = new Button("Open");
btnOpenApp.setPrefWidth(100);
btnOpenApp.setOnAction(e -> {
try {
sketchPane.open();
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
} catch (NullPointerException e1) {
System.out.println("Please pick a file in order to get your results.");
System.out.println("You can also draw a new picture and save it in order to open it later :).");
}
});
var btnSaveApp = new Button("Save");
btnSaveApp.setPrefWidth(100);
btnSaveApp.setOnAction(e -> {
try {
sketchPane.save();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
var btnClearApp = new Button("Clear");
btnClearApp.setPrefWidth(100);
btnClearApp.setOnAction(e -> sketchPane.clear());
this.getChildren().addAll(btnViewLines, btnOpenApp, btnSaveApp, btnClearApp, btnCloseApp);
}
}
class SketchPane extends Pane {
private double X;
private double Y;
public SketchPane() {
this.setOnMousePressed(e -> {
this.X = (int) e.getX();
this.Y = (int) e.getY();
});
this.setOnMouseDragged(e -> {
Line firstline = new Line(this.X, this.Y, e.getX(), e.getY());
this.getChildren().add(firstline);
this.X = e.getX();
this.Y = e.getY();
});
Rectangle clipRect = new Rectangle(this.getWidth(), this.getHeight());
clipRect.heightProperty().bind(this.heightProperty());
clipRect.widthProperty().bind(this.widthProperty());
this.setClip(clipRect);
}
public void viewListOfNodes() {
BorderPane bp = new BorderPane();
TextArea ta = new TextArea();
ta.setEditable(false);
bp.setCenter(ta);
Stage dialog = new Stage();
dialog.setTitle("Line End Points");
Scene scene = new Scene(bp, 300, 300);
dialog.setScene(scene);
int i = 0;
for (Node n : this.getChildren()) {
Line line = (Line) n;
i++;
ta.appendText(String.format("%5d: (%.0f, %.0f) - (%.0f, %.0f)\n", i, line.getStartX(), line.getStartY(),
line.getEndX(), line.getEndY()));
}
dialog.initOwner(primaryStage);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.showAndWait();
}
public void clear() {
this.getChildren().clear();
}
public void save() throws FileNotFoundException {
FileChooser fileSave = new FileChooser();
fileSave.getExtensionFilters().add(new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt"));
File thisFile = null;
thisFile = fileSave.showSaveDialog(null);
File file = new File(String.valueOf(thisFile));
PrintWriter outPutPrinted = null;
outPutPrinted = new PrintWriter(file);
for (Node n : this.getChildren()) {
Line line = (Line) n;
outPutPrinted.append(String.format("%.0f,%.0f,%.0f,%.0f", line.getStartX(), line.getStartY(),
line.getEndX(), line.getEndY())).println();
}
outPutPrinted.close();
}
public void open() throws FileNotFoundException {
clear();
FileChooser chooseFile = new FileChooser();
chooseFile.getExtensionFilters().add(new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt"));
File fileSelected = chooseFile.showOpenDialog(null);
@SuppressWarnings("resource")
Scanner scan = new Scanner(fileSelected);
do {
EtchASketch(scan.nextLine());
} while (scan.hasNext());
}
// regex for the 2 coordinates
public void EtchASketch(String compnumbers) throws RuntimeException {
String regex = "^(\\d+)\\,(\\d+)\\,(\\d+),(\\d+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(compnumbers);
if (matcher.find()) {
Line line = new Line(Double.parseDouble(matcher.group(1).toString()),
Double.parseDouble(matcher.group(2)), Double.parseDouble(matcher.group(3)),
Double.parseDouble(matcher.group(4)));
this.getChildren().add(line);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment