Skip to content

Commit

Permalink
fixed rendering, added draggable point, added line that reacts to point
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristopher Guzman committed Dec 3, 2016
1 parent bbc8777 commit ebd302b
Show file tree
Hide file tree
Showing 7 changed files with 679 additions and 29 deletions.
475 changes: 475 additions & 0 deletions hs_err_pid3361.log

Large diffs are not rendered by default.

71 changes: 65 additions & 6 deletions src/main/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.SplitPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Controller {

Expand All @@ -30,6 +35,9 @@ public class Controller {
@FXML private Label offsetLabel;
@FXML private Label zoomLabel;


public static HashSet<Drawable> drawableObjects = new HashSet<>();

private double MIN_OFFSET = 0.000000000000000001; //10^-18
private double MAX_OFFSET = 0.0000000000000009; //9x10^-16
private BigDecimal maxAsBigDecimal = new BigDecimal("0.000000000000000001").setScale(18, BigDecimal.ROUND_HALF_EVEN);
Expand All @@ -39,8 +47,53 @@ public class Controller {
private DecimalFormat df = new DecimalFormat(".##");
private DecimalFormat offsetFormat = new DecimalFormat("0.0E0");


@FXML private void initialize() {

setSliderHandlers();
}

public void onSceneLoaded(Scene scene) {

setMouseEvents();

}

private void setMouseEvents() {

DragPoint point = new DragPoint(canvas, 300, 300);
Line line = new Line(canvas, 2, point);
Controller.update(canvas.getGraphicsContext2D());

canvas.setOnMousePressed((event) -> {

if(point.inBoundsOf(event.getX(), event.getY())) {
point.isDragging = true;
}

});

canvas.setOnMouseDragged((event) -> {

if(point.isDragging) {
point.setPosition(event.getX(), event.getY());
Controller.update(canvas.getGraphicsContext2D());
}

});

canvas.setOnMouseReleased((event) -> {

point.isDragging = false;

});

}

private void setSliderHandlers() {

GridStuff grid = new GridStuff(canvas, 0.1);

offsetLabel.setText("Offset: " + offsetFormat.format(0.00000000000000035));

offsetSlider.valueProperty().addListener(((observable, oldValue, newValue) -> {
Expand All @@ -57,16 +110,22 @@ public class Controller {
zoomSlider.valueProperty().addListener(((observable, oldValue, newValue) -> {

double val = newValue.doubleValue() / maxResolution;
grid.setZoom(val);
zoomLabel.setText("Zoom: " + df.format(val) + "x");

}));

GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(2);
gc.setStroke(Color.BLACK);
gc.moveTo(0,0);
gc.lineTo(500, 300);
gc.stroke();
}

public static void update(GraphicsContext gc) {

gc.clearRect(0, 0, gc.getCanvas().widthProperty().intValue(), gc.getCanvas().widthProperty().intValue());

for(Drawable item : drawableObjects) {

item.update();

}

}

Expand Down
64 changes: 64 additions & 0 deletions src/main/DragPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;

/**
* Created by kristopherguzman on 12/2/16.
*/
public class DragPoint implements Drawable {

public boolean isDragging = false;

private GraphicsContext gc;

private int screenWidth;
private double x;
private double y;
private int size = 20;

public double getX() { return x + (size / 2); }
public double getY() { return y + (size / 2); }


public DragPoint(Canvas canvas, double x, double y) {

this.gc = canvas.getGraphicsContext2D();
screenWidth = canvas.widthProperty().intValue();
this.x = x;
this.y = y;

Controller.drawableObjects.add(this);

}

public void setPosition(double x, double y) {

if(x > screenWidth - 25 || x < 25 || y > screenWidth - 25 || y < 25) {
return;
}

this.x = x - (size / 2);
this.y = y - (size / 2);

}

public boolean inBoundsOf(double mx, double my) {

//checks if point of mouse click is in bounds of drag point
System.out.println("px: " + x + " py: " + y + " mx: " + mx + " my: " + my);
return (mx > x && mx < x + size && my > y && my < y + size);

}

public void update() {

gc.setFill(Color.BLACK);
gc.fillOval(x, y, size, size);

}


}
8 changes: 8 additions & 0 deletions src/main/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main;

/**
* Created by kristopherguzman on 12/2/16.
*/
public interface Drawable {
void update();
}
41 changes: 27 additions & 14 deletions src/main/GridStuff.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class GridStuff{
int yellow = -1;
int white = 0;

int MAX_RESOLUTION = 100;

private GraphicsContext gc;

private double canvasWidth = 769;
private double canvasHeight = 769;

Expand All @@ -25,19 +29,14 @@ public class GridStuff{

GridStuff [][] map = new GridStuff[(int) n][(int) m];

//void draw();
//void setZoom(double z);

public GridStuff(Canvas canvas, double zoom){
GraphicsContext gc = canvas.getGraphicsContext2D();

for( int i=0; i < n; i++) {
for( int j=0; j < m; j++) {
public void draw() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//if(condition 1){
gc.setFill(Color.BLACK);
gc.fillRect(i*getWidth, j*getHeight, getWidth, getHeight);
gc.setFill(Color.INDIANRED);
gc.fillRect(i*getWidth, j*getHeight, getWidth - 2, getHeight - 2);
gc.setFill(Color.BLACK);
gc.fillRect(i * getWidth, j * getHeight, getWidth, getHeight);
gc.setFill(Color.INDIANRED);
gc.fillRect(i * getWidth, j * getHeight, getWidth - 2, getHeight - 2);
//}
/*if(condition 2){
gc.setFill(Color.BLACK);
Expand All @@ -51,7 +50,21 @@ public GridStuff(Canvas canvas, double zoom){
gc.setFill(Color.LIGHTGOLDENRODYELLOW);
gc.fillRect(i*getWidth, j*getHeight, getWidth - 2, getHeight - 2);
}*/
}
}
}
}
}

public void setZoom(double val) {

n -= 10 / val;
getHeight = canvasHeight / m;
getWidth = canvasWidth / n;
draw();

}

public GridStuff(Canvas canvas, double zoom){
this.gc = canvas.getGraphicsContext2D();
draw();
}
}
38 changes: 31 additions & 7 deletions src/main/Line.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
package main;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

/**
* Created by kristopherguzman on 12/2/16.
*/
public class Line {
public class Line implements Drawable {

private int start;
private int end;
private GraphicsContext gc;
private DragPoint p;
private int thickness;
private int screenWidth;

public void setStart();
public Line(Canvas canvas, int thickness, DragPoint p) {

public Line(Canvas canvas, int thickness, int from, int to) {
this.gc = canvas.getGraphicsContext2D();
this.thickness = thickness;
this.screenWidth = canvas.widthProperty().intValue();
this.p = p;

this.start = canvas.widthProperty().intValue() - start;
this.end = canvas.heightProperty().intValue() - end;
Controller.drawableObjects.add(this);
Controller.update(gc);

}

public void update() {

double py = screenWidth - p.getY();
double m = (py - screenWidth) / (p.getX() - screenWidth); // y2 - y1 / x2 - x2
double b = m * (0 - screenWidth) + screenWidth; // m(x - x1) = y intercept
double y = b; // y = mx + b
double x = 0;
y = screenWidth - y; //invert y coordinate

gc.beginPath();
gc.moveTo(screenWidth, 0);
gc.lineTo(x, y);
gc.setLineWidth(thickness);
gc.stroke();
gc.closePath();

}

}
11 changes: 9 additions & 2 deletions src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import javafx.scene.shape.Path;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Stack;

/**
* Created by kristopherguzman on 11/2/16.
*/
Expand All @@ -26,15 +29,19 @@ public static void main(String[] args) {

public void start(Stage primaryStage) throws Exception {


FXMLLoader loader = new FXMLLoader();
Parent root = FXMLLoader.load(getClass().getResource("../view/view.fxml"));
Parent root = loader.load(getClass().getResource("../view/view.fxml").openStream());
primaryStage.setTitle("Nonrobust CCW");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setResizable(false);

Controller controller = loader.getController();
controller.onSceneLoaded(scene);

primaryStage.show();

}


}

0 comments on commit ebd302b

Please sign in to comment.