Note: 25 years with Java, 2.5 hours (almost) with JavaFX.
I want to be able to highlight all cells of a GridPane that the mouse is dragged across - i.e. everything that intersects with the rectangle cornered by the click point and the current drag point. I can do that if all children are 1x1, but with mixed sizes I'm having no joy.
For example, if the top row has 1 1 column cell (A) and 1 2 column cell (B) and the bottom has 1 2 column cell (C) and 1 1 column cell (D), if I click in A and drag down into C I can highlight both. However, I cannot figure out when I drag into the right half of C so that B should be highlighted.
Sample Board:
Apologies for the HSCE - it's a bit long but I felt stripping it down would reduce readability.
import java.util.*;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.geometry.*;
public class AddTestSSCE extends Application
{
ArrayList<StackPane> sPanes = new ArrayList<StackPane>(); // selected panes
GridPane theGrid;
Node startNode = null;
int col0, col1;
int row0, row1;
#Override
public void start(Stage stage) {
theGrid = new GridPane();
ColumnConstraints col = new ColumnConstraints(300);
theGrid.getColumnConstraints().addAll(col, col, col);
RowConstraints row = new RowConstraints(200);
theGrid.getRowConstraints().addAll(row, row, row);
addGridPane();
theGrid.getStyleClass().add("bg-grid");
Scene scene = new Scene(theGrid, 1024, 768);
scene.getStylesheets().add("addtestssce.css");
stage.setScene(scene);
stage.show();
}
public void addGridPane() {
theGrid.setHgap(10);
theGrid.setVgap(10);
theGrid.setPadding(new Insets(0, 10, 0, 10));
StackPane theSP = sPAdd(new Label("A"));
theGrid.add(theSP, 0, 0, 1, 1);
theSP = sPAdd(new Label("B"));
theGrid.add(theSP, 1, 0, 2, 1);
theSP = sPAdd(new Label("C"));
theGrid.add(theSP, 0, 1, 2, 1);
theSP = sPAdd(new Label("D"));
theGrid.add(theSP, 2, 1, 1, 1);
theGrid.addEventFilter(MouseEvent.MOUSE_PRESSED, //Creating the mouse event handler
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
System.out.println("We're Moving!!");
startNode = (Node)e.getTarget();
sPanes.add((StackPane)startNode);
col0 = GridPane.getColumnIndex(startNode).intValue();
row0 = GridPane.getRowIndex(startNode).intValue();
System.out.printf("Starting at %d %d\n", col0, row0);
}
});
theGrid.addEventFilter(MouseEvent.MOUSE_DRAGGED, //Creating the mouse event handler
new EventHandler<MouseEvent>() {
Node lastNode = null;
#Override
public void handle(MouseEvent e) {
Node target = (Node)e.getTarget();
double xLoc = e.getX();
double yLoc = e.getY();
Bounds bs = target.localToScene(target.getBoundsInLocal());
Node moveTarget;
if( bs.contains(xLoc, yLoc) )
{
moveTarget = target;
}
else
{
moveTarget = getContainingNode((int)xLoc, (int)yLoc);
}
if( moveTarget != null && lastNode != moveTarget )
{
col1 = GridPane.getColumnIndex(moveTarget).intValue();
row1 = GridPane.getRowIndex(moveTarget).intValue();
doHighlighting();
lastNode = moveTarget;
}
}
});
}
void doHighlighting()
{
int c0, c1, r0, r1;
c0 = col0 > col1 ? col1 : col0;
c1 = !(col0 > col1) ? col1 : col0;
r0 = row0 > row1 ? row1 : row0;
r1 = !(row0 > row1) ? row1 : row0;
Rectangle2D rec1 = new Rectangle2D(c0, r0, c1-c0+1, r1-r0+1);
System.out.printf("Box: %d %d %d %d\n", c0, c1, r0, r1);
List<Node> nodes = theGrid.getChildren();
for( Node node : nodes )
{
StackPane sp = (StackPane)node;
unhighlight(sp);
int col = GridPane.getColumnIndex(sp).intValue();
int row = GridPane.getRowIndex(sp).intValue();
if( occupiesCell(sp, rec1) )
{
highlight(sp);
}
}
}
boolean occupiesCell(Node node, Rectangle2D r1)
{
boolean result = false;
int col = GridPane.getColumnIndex(node).intValue();
int row = GridPane.getRowIndex(node).intValue();
int wid = GridPane.getColumnSpan(node).intValue();
int hei = GridPane.getRowSpan(node).intValue();
Rectangle2D r2 = new Rectangle2D( col, row, wid, hei);
return r2.intersects(r1);
}
void unhighlight(Node node)
{
if( !(node instanceof StackPane) )
{
return;
}
StackPane label = (StackPane)node;
List<String> cList = label.getStyleClass();
cList.remove("b2");
cList.add("b1");
}
void highlight(Node node)
{
if( !(node instanceof StackPane) )
{
return;
}
StackPane label = (StackPane)node;
List<String> cList = label.getStyleClass();
cList.remove("b1");
cList.add("b2");
}
private Node getContainingNode(int xLoc, int yLoc)
{
Node tgt = null;
for( Node node : theGrid.getChildren() )
{
Bounds boundsInScene = node.localToScene(node.getBoundsInLocal());
if( boundsInScene.contains(xLoc, yLoc) )
{
return node;
}
}
return tgt;
}
private StackPane sPAdd(Label label)
{
StackPane gPPane = new StackPane();
gPPane.getChildren().add(label);
gPPane.getStyleClass().addAll("b1", "grid-element");
GridPane.setFillHeight(gPPane, true);
GridPane.setFillWidth(gPPane, true);
return gPPane;
}
public static void main(String[] args)
{
launch();
}
}
.bg-grid {
-fx-background-color: slategrey;
}
.grid-element {
-fx-border-width: 10;
-fx-border-color: rgb(225, 128, 217);
-fx-background-color: rgb(247, 146, 146);
-fx-font: 36 arial;
}
.b1 {
-fx-text-base-color: white;
-fx-border-color: rgb(225, 128, 217);
}
.b2 {
-fx-text-base-color: lightgray;
-fx-border-color: rgb(233, 228, 86);
}
After a bit of back and forth with #james_d and #slaw, finally came up with a working solution.
import java.util.*;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.css.PseudoClass;
public class AddTestSSCE extends Application
{
private static final PseudoClass highlight = PseudoClass.getPseudoClass("highlight");
ArrayList<StackPane> sPanes = new ArrayList<StackPane>(); // selected panes
GridPane theGrid;
Node startNode = null;
int x0, y0, x1, y1;
#Override
public void start(Stage stage) {
theGrid = new GridPane();
ColumnConstraints col = new ColumnConstraints(300);
theGrid.getColumnConstraints().addAll(col, col, col);
RowConstraints row = new RowConstraints(200);
theGrid.getRowConstraints().addAll(row, row, row);
addGridPane();
theGrid.getStyleClass().add("bg-grid");
Scene scene = new Scene(theGrid, 1024, 768);
scene.getStylesheets().add("addtestssce.css");
stage.setScene(scene);
stage.show();
}
public void addGridPane() {
theGrid.setHgap(10);
theGrid.setVgap(10);
theGrid.setPadding(new Insets(0, 10, 0, 10));
StackPane theSP = sPAdd(new Label("A"));
theGrid.add(theSP, 0, 0, 1, 1);
theSP = sPAdd(new Label("B"));
theGrid.add(theSP, 1, 0, 2, 1);
theSP = sPAdd(new Label("C"));
theGrid.add(theSP, 0, 1, 2, 1);
theSP = sPAdd(new Label("D"));
theGrid.add(theSP, 2, 1, 1, 1);
theGrid.addEventFilter(MouseEvent.MOUSE_PRESSED, //Creating the mouse event handler
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
System.out.println("We're Moving!!");
startNode = (Node)e.getTarget();
sPanes.add((StackPane)startNode);
x0 = x1 = (int)e.getX();
y0 = y1 = (int)e.getY();
doHighlighting();
System.out.printf("Starting at %d %d\n", x0, y0);
}
});
theGrid.addEventFilter(MouseEvent.MOUSE_DRAGGED, //Creating the mouse event handler
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
Node target = (Node)e.getTarget();
x1 = (int)e.getX();
y1 = (int)e.getY();
Bounds bs = target.localToScene(target.getBoundsInLocal());
Node moveTarget;
if( bs.contains(x1, y1) )
{
moveTarget = target;
}
else
{
moveTarget = getContainingNode( x1, y1);
}
if( moveTarget != null )
{
doHighlighting();
}
}
});
}
void doHighlighting()
{
int c0, c1, r0, r1;
c0 = x0 > x1 ? x1 : x0;
c1 = !(x0 > x1) ? x1 : x0;
r0 = y0 > y1 ? y1 : y0;
r1 = !(y0 > y1) ? y1 : y0;
Bounds dragged = new BoundingBox(c0, r0, c1-c0+1, r1-r0+1);
for (Node child : theGrid.getChildren())
{
child.pseudoClassStateChanged(highlight, dragged.intersects(child.getBoundsInParent()));
}
}
private Node getContainingNode(int xLoc, int yLoc)
{
Node tgt = null;
for( Node node : theGrid.getChildren() )
{
Bounds boundsInScene = node.localToScene(node.getBoundsInLocal());
if( boundsInScene.contains(xLoc, yLoc) )
{
return node;
}
}
return tgt;
}
private StackPane sPAdd(Label label)
{
StackPane gPPane = new StackPane();
gPPane.getChildren().add(label);
gPPane.getStyleClass().addAll("b1", "grid-element");
GridPane.setFillHeight(gPPane, true);
GridPane.setFillWidth(gPPane, true);
return gPPane;
}
public static void main(String[] args)
{
launch();
}
}
and the CSS:
.bg-grid {
-fx-background-color: slategrey;
}
.grid-element {
-fx-border-width: 10;
-fx-border-color: rgb(225, 128, 217);
-fx-background-color: rgb(247, 146, 146);
-fx-font: 36 arial;
}
.grid-element:highlight {
-fx-text-base-color: lightgray;
-fx-border-color: rgb(233, 228, 86);
}
Related
I've created a calculator in JavaFX but one thing frustrates me - it seems like the scene or the primary stage has slightly rounded corners, and these are visible behind the square corners of the buttons.
Screenshot of the problem
I've played around with the background radius of the scene but can't work it out. Any ideas where this radius (if that's what it is) is set?
Here is my code and CSS:
.root
{
-fx-font-size: 12pt;
}
.label
{
-fx-background-color: #999999;
-fx-text-fill: #f9f9f9;
-fx-font-weight: 500;
}
.label-main
{
-fx-background-color: #999999;
-fx-text-fill: #f9f9f9;
-fx-font-size: 35pt;
-fx-font-weight: 500;
}
.button
{
-fx-font-weight: lighter;
-fx-border-style: solid;
-fx-border-color: gray;
-fx-background-color: #E0E0E0;
-fx-border-width: 0 1 1 0;
-fx-background-insets: 0,1,1;
-fx-background-radius: 0;
}
.del-clear
{
-fx-font-weight: lighter;
-fx-font-size: 9pt;
-fx-border-style: solid;
-fx-border-color: gray;
-fx-background-color: #E0E0E0;
-fx-border-width: 0 1 1 0;
-fx-background-insets: 0,1,1;
-fx-background-radius: 0;
}
.button-bigger
{
-fx-font-size: 14pt;
}
.button:pressed
{
-fx-background-color: #B2B2B2;
}
.button-special
{
-fx-background-color: #EA9747;
-fx-text-fill: white;
-fx-border-width: 0 0 1 0;
}
.zero-point
{
-fx-border-width: 0 1 0 0;
}
.button-equals
{
-fx-border-width: 0 0 0 0;
}
package calculator;
import java.util.ArrayList;
import java.util.List;
import javafx.application.*;
import static javafx.application.Application.launch;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.*;
import javafx.stage.*;
public class UserInterface extends Application {
private static int appWidth = 215; //230;
private static int appHeight = 295; //300;
private static int numRows = 7;
private static int numCols = 8;
private List<Button> numButList = new ArrayList();
private List<Button> functButtons = new ArrayList();
private List<Button> compButtons = new ArrayList();
private Calculator calc;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
//Screen s
Label screen2 = new Label("0");
screen2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
screen2.setAlignment(Pos.CENTER_RIGHT);
screen2.getStyleClass().add("label-main");
Label screen1 = new Label("");
screen1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
screen1.setAlignment(Pos.CENTER_RIGHT);
GridPane screenBox = new GridPane();
screenBox.getStyleClass().add("innerGrid");
screenBox.setPrefSize(UserInterface.appWidth, (UserInterface.appHeight / UserInterface.numRows) * 2);
//Set the row constraints
for (int i = 0; i < 4; i++) {
RowConstraints r = new RowConstraints();
r.setVgrow(Priority.ALWAYS);
r.setFillHeight(true);
r.setPercentHeight(25);
screenBox.getRowConstraints().add(r);
}
//Set the column constraints
ColumnConstraints c = new ColumnConstraints();
c.setHgrow(Priority.ALWAYS);
c.setPercentWidth(100);
screenBox.getColumnConstraints().add(c);
//screenBox.setGridLinesVisible(true);
screenBox.add(screen1, 0, 0, 1, 1);
screenBox.add(screen2, 0, 1, 1, 3);
//Create calc and pass the screen1 to it
this.calc = new Calculator(screen1, screen2);
//Create 0-9 buttons
for (int i = 0; i < 10; i++) {
Button tempButton = new Button(Integer.toString(i));
if (i == 0) {
tempButton.getStyleClass().add("zero-point");
}
final String tempVar = Integer.toString(i);
tempButton.setOnAction(e -> this.calc.setUserInput(tempVar));
tempButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(tempButton, Priority.ALWAYS);
VBox.setVgrow(tempButton, Priority.ALWAYS);
numButList.add(tempButton);
}
//Point button
//point button
Button point = new Button(".");
String tepointVarmpVar = ".";
point.setOnAction(e -> this.calc.setUserInput(tepointVarmpVar));
point.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(point, Priority.ALWAYS);
VBox.setVgrow(point, Priority.ALWAYS);
point.getStyleClass().add("button-bigger");
point.getStyleClass().add("zero-point");
//Equals, plus, minus, multiply, and divide buttons
Button equals = new Button("=");
this.compButtons.add(equals);
equals.setOnAction(e -> this.calc.calculate());
Button plus = new Button("+");
this.compButtons.add(plus);
String plusVar = "+";
plus.setOnAction(e -> this.calc.setUserInput(plusVar));
Button minus = new Button("-");
String minusVar = "-";
minus.setOnAction(e -> this.calc.setUserInput(minusVar));
this.compButtons.add(minus);
minus.setStyle("fx-font-size: 16");
Button multiply = new Button("x");
String xVar = "*";
multiply.setOnAction(e -> this.calc.setUserInput(xVar));
this.compButtons.add(multiply);
Button divide = new Button("/");
this.compButtons.add(divide);
String divideVar = "/";
divide.setOnAction(e -> this.calc.setUserInput(divideVar));
//Add button configs
for (int i = 0; i < 5; i++) {
this.compButtons.get(i).setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
this.compButtons.get(i).getStyleClass().add("button-special");
HBox.setHgrow(this.compButtons.get(i), Priority.ALWAYS);
VBox.setVgrow(this.compButtons.get(i), Priority.ALWAYS);
}
equals.getStyleClass().add("button-equals");
for (int i = 0; i < 4; i++) {
this.compButtons.get(i).getStyleClass().add("button-bigger");
}
//Clear and delete buttons
Button clear = new Button("CLR");
clear.setOnAction(e -> this.calc.clearUserInput());
clear.getStyleClass().add("del-clear");
this.functButtons.add(clear);
Button del = new Button("DEL");
del.setOnAction(e -> this.calc.backSpace());
del.getStyleClass().add("del-clear");
this.functButtons.add(del);
for (int i = 0; i < 2; i++) {
this.functButtons.get(i).setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(this.functButtons.get(i), Priority.ALWAYS);
VBox.setVgrow(this.functButtons.get(i), Priority.ALWAYS);
}
//Create GridPane
GridPane grid = new GridPane();
//Add nodes to grid pane
grid.add(numButList.get(0), 0, 6, 4, 1);
grid.add(point, 4, 6, 2, 1);
grid.add(numButList.get(1), 0, 5, 2, 1);
grid.add(numButList.get(2), 2, 5, 2, 1);
grid.add(numButList.get(3), 4, 5, 2, 1);
grid.add(numButList.get(4), 0, 4, 2, 1);
grid.add(numButList.get(5), 2, 4, 2, 1);
grid.add(numButList.get(6), 4, 4, 2, 1);
grid.add(numButList.get(7), 0, 3, 2, 1);
grid.add(numButList.get(8), 2, 3, 2, 1);
grid.add(numButList.get(9), 4, 3, 2, 1);
grid.add(equals, 6, 6, 2, 1);
grid.add(plus, 6, 5, 2, 1);
grid.add(minus, 6, 4, 2, 1);
grid.add(multiply, 6, 3, 2, 1);
grid.add(divide, 6, 2, 2, 1);
grid.add(clear, 0, 2, 3, 1);
grid.add(del, 3, 2, 3, 1);
grid.add(screenBox, 0, 0, 8, 2);
// Set row and column constraints
for (int rowIndex = 0; rowIndex < UserInterface.numRows; rowIndex++) {
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS);
rc.setFillHeight(true);
rc.setPercentHeight(UserInterface.appHeight / UserInterface.numRows);
grid.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < UserInterface.numCols; colIndex++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setHgrow(Priority.ALWAYS);
cc.setFillWidth(true);
cc.setPercentWidth(UserInterface.appWidth / UserInterface.numCols);
grid.getColumnConstraints().add(cc);
}
// Create the scene and the stage
Scene scene = new Scene(grid, appWidth, appHeight);
scene.getStylesheets().add("Simple.css");
primaryStage.setScene(scene);
//primaryStage.setTitle("Calculator");
primaryStage.setResizable(false);
primaryStage.show();
}
}
package calculator;
import java.util.ArrayList;
import java.util.List;
public class StringCalculator {
public StringCalculator() {
}
public static double calcString(final String str2) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str2.length()) ? str2.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') {
nextChar();
}
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str2.length()) {
throw new RuntimeException("Unexpected: " + (char) ch);
}
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) {
x += parseTerm(); // addition
} else if (eat('-')) {
x -= parseTerm(); // subtraction
} else {
return x;
}
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*')) {
x *= parseFactor(); // multiplication
} else if (eat('/')) {
x /= parseFactor(); // division
} else {
return x;
}
}
}
double parseFactor() {
if (eat('+')) {
return parseFactor(); // unary plus
}
if (eat('-')) {
return -parseFactor(); // unary minus
}
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') {
nextChar();
}
x = Double.parseDouble(str2.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') {
nextChar();
}
String func = str2.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) {
x = Math.sqrt(x);
} else if (func.equals("sin")) {
x = Math.sin(Math.toRadians(x));
} else if (func.equals("cos")) {
x = Math.cos(Math.toRadians(x));
} else if (func.equals("tan")) {
x = Math.tan(Math.toRadians(x));
} else {
throw new RuntimeException("Unknown function: " + func);
}
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^')) {
x = Math.pow(x, parseFactor()); // exponentiation
}
return x;
}
}.parse();
}
}
package application;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
private static Canvas canvas;
private static int clickCount = 0;
private static int x1 = 0;
private static int y1 = 0;
static ArrayList<Circle> circleArr = new ArrayList<Circle>();
private Timeline timeline;
#Override
public void start(Stage primaryStage) {
try {
primaryStage.setTitle("Graphics Demo App");
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 800, 500);
canvas = new Canvas(800, 475);
canvas.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (clickCount % 2 == 0) {
x1 = x;
y1 = y;
} else {
int x2 = x;
int y2 = y;
int r = (int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
int x_corner = x1 - r;
int y_corner = y1 - r;
Circle c = new Circle(x_corner, y_corner, r);
circleArr.add(c);
c.drawOn(canvas);
}
clickCount++;
}
});
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
root.setCenter(canvas);
Button circleButton = new Button("Circle");
circleButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
// System.out.println("Circle");
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.AQUAMARINE);
Random rand = new Random();
int x = rand.nextInt((int) canvas.getWidth());
int y = rand.nextInt((int) canvas.getHeight());
Circle c = new Circle(x, y, 50);
c.drawOn(canvas);
// gc.strokeOval(x, y, 200, 200);
}
});
Button squareButton = new Button("Square");
squareButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
// System.out.println("square");
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.AQUAMARINE);
Random rand = new Random();
int x = rand.nextInt((int) canvas.getWidth());
int y = rand.nextInt((int) canvas.getHeight());
int w = rand.nextInt((int) canvas.getWidth());
int h = rand.nextInt((int) canvas.getHeight());
Square s = new Square(x, y, w, h);
s.drawOn(canvas);
// gc.strokeOval(x, y, 200, 200);
}
});
Button stepButton = new Button("Step");
stepButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
step();
}
});
Button playButton = new Button("Play");
playButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
timeline = new Timeline(new KeyFrame(Duration.millis(100), ae -> step()));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
});
Button stopButton = new Button("Stop");
stopButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
if (timeline != null) {
timeline.stop();
}
}
});
FlowPane buttonsPane = new FlowPane();
buttonsPane.setAlignment(Pos.CENTER);
buttonsPane.getChildren().add(circleButton);
buttonsPane.getChildren().add(squareButton);
Button saveButton = new Button("Save");
Button restoreButton = new Button("Restore");
restoreButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
restore();
}
});
buttonsPane.getChildren().add(saveButton);
buttonsPane.getChildren().add(restoreButton);
buttonsPane.getChildren().add(stepButton);
buttonsPane.getChildren().add(playButton);
buttonsPane.getChildren().add(stopButton);
root.setBottom(buttonsPane);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void step() {
clearScreen();
for (Circle c : circleArr) {
c.step(canvas);
}
}
private static void clearScreen() {
canvas.getGraphicsContext2D().fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
}
public static void main(String[] args) {
launch(args);
}
public static void restore() {
try {
// Setting up to read the text file
FileReader reader = new FileReader("shapes.txt");
BufferedReader bufferReader = new BufferedReader(reader);
// Read the first line in the file
String line = bufferReader.readLine();
while (line != null && line.trim().length() > 0) {
String[] parts = line.split(":");
String left = parts[0];
String right = parts[1];
if (left.equals("C")) {
String[] components = right.trim().split(" ");
int x = Integer.parseInt(components[0]);
int y = Integer.parseInt(components[1]);
int r = Integer.parseInt(components[2]);
Circle c = new Circle(x, y, r);
c.drawOn(canvas);
} else if (left.equals("S")) {
System.out.println("This is a square: " + right);
}
line = bufferReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is the circle code :-
package application;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Circle {
int x;
int y;
int radius;
int x_direction = 1;
int y_direction = 1;
int dx = 11, dy = 7;
public Circle(){
}
public Circle(int x, int y, int r){
this.x = x;
this.y = y;
this.radius = r;
}
public void print(){
System.out.println("Circle: " + x + " " + y + " " + radius);
}
public void drawOn(Canvas canvas){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.AQUAMARINE);
gc.strokeOval(x, y, 2*radius, 2*radius);
}
public void step(Canvas canvas){
x += 5 * x_direction;
y += 5 * y_direction;
if (x + radius * 2 > canvas.getWidth()){
x_direction = x_direction * -1;
}
if (y + radius * 2 > canvas.getHeight()){
y_direction = y_direction * -1;
}
if ((x - radius + dx < 0) || (x + radius + dx > canvas.getWidth()))
dx = -dx;
if ((y - radius + dy < 0) || (y + radius + dy > canvas.getHeight()))
dy = -dy;
// Move the circle.
x += dx;
y += dy;
drawOn(canvas);
}
Here is what I came up with. As you didn't say your problem, I made an example. I didn't use a time line, I just used another thread that continually updates.
package helloworld;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Matt on 17/08/16.
*/
public class AnimatedScene extends Application{
List<AnimatedCircle> circles = new ArrayList<>();
Canvas custom;
boolean quit = false;
#Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
custom = new Canvas(600, 600);
GraphicsContext gc = custom.getGraphicsContext2D();
createBackground(gc);
root.getChildren().add(custom);
circles.add(new AnimatedCircle(50, 50));
circles.add(new AnimatedCircle(50, 100));
circles.add(new AnimatedCircle(50, 150));
circles.add(new AnimatedCircle(50, 200));
for(AnimatedCircle circle: circles) {
root.getChildren().add(circle.shape);
}
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, evt->{
quit=true;
});
startMainLoop();
}
private void startMainLoop(){
new Thread(()->{
Thread.currentThread().setName("bounce loop");
long time = System.currentTimeMillis();
int step = (int)(1000.0/60.0);
while(!(Thread.interrupted()||quit)){
synchronized (circles) {
for(int i = 0; i<circles.size(); i++){
for(int j = i+1; j<circles.size(); j++){
AnimatedCircle a = circles.get(i);
AnimatedCircle b = circles.get(j);
double dx = b.cx - a.cx;
double dy = b.cy - a.cy;
double distance = Math.sqrt(dx*dx + dy*dy);
if(distance<a.radius + b.radius){
//collision
b.vx = dx/distance;
b.vy = dy/distance;
a.vx = -dx/distance;
a.vy = dy/distance;
}
}
}
for (AnimatedCircle circle : circles) {
circle.update();
}
}
Platform.runLater(()->{
sync();
});
try{
long nt = System.currentTimeMillis();
long wait = step - (nt - time);
if(wait>=0) {
Thread.sleep(wait);
time = nt+wait;
} else{
time = nt;
}
} catch(InterruptedException exc){
//just exit.
}
}
}).start();
}
void sync(){
synchronized (circles){
GraphicsContext gc = custom.getGraphicsContext2D();
createBackground(gc);
for(AnimatedCircle circle: circles){
circle.sync();
gc.setFill(new Color(0, 0, 0, 0.2));
gc.fillOval(circle.cx - circle.radius-15, circle.cy - circle.radius-5, 50, 50);
}
}
}
void createBackground(GraphicsContext gc){
gc.setFill(Color.RED);
RadialGradient gradient = new RadialGradient(
0, 0, 300, 300, 300, false,
CycleMethod.NO_CYCLE,
new Stop(0.0, Color.RED), new Stop(1.0, Color.DARKRED));
gc.setFill(gradient);
gc.fillRect(100, 100, 400, 400);
gc.setFill(new Color(1, 0.2, 0.2, 1));
gc.fillPolygon(
new double[]{ 0, 100, 100, 0},
new double[]{ 0, 100, 500, 600},
4
);
gc.setFill(new Color(1, 0.25, 0.25, 1));
gc.fillPolygon(
new double[]{ 0, 100, 500, 600},
new double[]{ 0, 100, 100, 0},
4
);
gc.setFill(new Color(0.5, 0., 0., 1));
gc.fillPolygon(
new double[]{ 500, 600, 600, 500},
new double[]{ 100, 0, 600, 500},
4
);
gc.setFill(new Color(0.8, 0., 0., 1));
gc.fillPolygon(
new double[]{ 0, 100, 500, 600},
new double[]{ 600, 500, 500, 600},
4
);
}
public static void main(String[] args){
launch(args);
}
}
class AnimatedCircle{
public Ellipse shape;
RadialGradient gradient;
double vx;
double vy;
double cx, cy;
double speed = 5;
double radius = 25;
public AnimatedCircle(double x, double y){
cx = x;
cy = y;
vx = 2*(Math.random() - 0.5);
vy = Math.sqrt(1 - vx*vx);
if(Math.random()>=0.5){
vy = -vy;
}
shape = new Ellipse(cx, cy, radius, radius);
final String label = x + ", " + y;
shape.addEventHandler(MouseEvent.MOUSE_CLICKED, evt->{
System.out.println(label);
});
gradient = new RadialGradient(0, 0, 0.75, 0.6, 1, true, CycleMethod.NO_CYCLE, new Stop(0.0, Color.WHITE), new Stop(1.0, Color.BLACK));
shape.setFill(gradient);
}
void update(){
double nx = cx + vx*speed;
if(nx + shape.getRadiusX()>600 || nx - shape.getRadiusX()<0){
vx = -vx;
nx = cx;
}
double ny = cy + vy*speed;
if(ny + shape.getRadiusY()>600 || ny - shape.getRadiusY()<0 ){
vy = -vy;
ny = cy;
}
cy = ny;
cx = nx;
}
public void sync(){
shape.setCenterX(cx);
shape.setCenterY(cy);
}
}
Things to note:
In createBackground, I completely repaint the background. In your clear method you fill everything with the last paint. That doesn't necessarily clear your background.
I used two different methods to draw the balls. 1) I use ellipses that are added to the scene, then I update their position. 2) I draw the shadows using the graphics canvas.
Otherwise, if you can clean up your question a bit, and actually explain what is wrong somebody might be able to fix your code.
I wrote a Snake Game , when it hit it's tail , it will be Game Over and throw an alert. I set a Button in stage of my alert for new game , and i want to use that to run my cod again at first. is there any method or something...
It's my alert class
class alart {
public static void Game_over_alart(int dom) {
dom = (dom - 9) * 10 ;
Label sdom = new Label(Integer.toString(dom));
Stage window = new Stage();
window.setTitle("Game_Over");
Label gameOver = new Label("Game Over");
Label scor = new Label("SCORE : ");
Label best = new Label("BEST : ");
Text newgame = new Text("NEW GAME");
HBox hbox1 = new HBox(2, scor , sdom );
HBox hbox2 = new HBox(2, best);
VBox vbox = new VBox(2, hbox1, hbox2);
gameOver.setStyle(""
+ "-fx-alignment: top;"
+ "-fx-font-size: 40px;"
+ "-fx-font-style: italic;"
+ "-fx-font-weight: bold;"
+ "-fx-font-family: fantasy;"
+ "-fx-text-fill: lightgrey ;");
// + "-fx-background-color: gray");
DropShadow d = new DropShadow(5, Color.BLACK);
ScaleTransition st=new ScaleTransition(Duration.millis(100),newgame);
st.setToX(1.1);
st.setToY(1.1);
st.setFromX(1);
st.setFromY(1);
//st.setAutoReverse(true);
// st.setCycleCount(Animation.INDEFINITE);
gameOver.setEffect(d);
BorderPane b = new BorderPane();
b.setTop(gameOver);
b.setCenter(vbox);
b.setBottom(newgame);
gameOver.setAlignment(Pos.CENTER);
Scene s = new Scene(b, 400, 200);
window.setScene(s);
window.show();
window.setResizable(false);
newgame.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
}
});
newgame.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event1) {
st.play();
}
});
newgame.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event1) {
st.setFromX(1.1);
st.setFromY(1.1);
st.setToX(1);
st.setToY(1);
}
});
}
}
and it's the whole of my code
/**
* Created by Nadia on 12/31/2015.
*/
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.animation.ScaleTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
import sun.jvm.hotspot.debugger.cdbg.Sym;
import javax.swing.text.StyledEditorKit;
import java.util.ArrayList;
public class Main_Snake extends Application{
Canvas canvas = new Canvas(399, 599);
Rectangle round = new Rectangle(0 ,0 , 400 , 600 );
B_Part snake = new Snake();
B_Part apple = new Apple();
B_Part mane = new Mane();
#Override
public void start(Stage primaryStage) throws Exception {
round.setStroke(Color.BLACK);
round.setFill(Color.WHITE);
StackPane ss = new StackPane();
ss.getChildren().addAll(round, canvas);
BorderPane b = new BorderPane();
b.setBottom(ss);
Scene scene = new Scene(b, 410, 700);
primaryStage.setScene(scene);
primaryStage.setTitle ( "Snake" );
primaryStage.show();
primaryStage.setResizable(false);
play();
}
public void play() {
AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
#Override
public void handle(long now) {
if (now - lastUpdate >= 20_000_000) { // payin avordane sor#
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
try {
for ( int i = 0 ; i < 5 ; i++)
mane.Move(canvas.getScene());
} catch (Game_Over_Exception e) {
}finally {
mane.Drow(canvas);
}
try {
apple.Move(canvas.getScene());
}catch (Exception e) {
}finally {
apple.Drow(canvas);
}
try {
snake.Move(canvas.getScene());
} catch (Game_Over_Exception e) {
stop();
alart.Game_over_alart(snake.X.size());
}finally {
snake.Drow(canvas); // har bar mar rasm mishe bad az move va ye sib ba X,Y khodesh rasm mishe tu tabe move dar morede tabe Point hast
}
lastUpdate = now; // sor#
}
}
};
timer.start();
}
}
abstract class B_Part {
boolean goNorth = true, goSouth = false, goWest = false, goEast = false; ///////////////////////////////////////////////////////
static int GM = 0 ;
int Mx , My ;
static ArrayList<Integer> Mane_x = new ArrayList<>();
static ArrayList<Integer> Mane_y = new ArrayList<>();
static int x, y ; // marbut be apple
static int j = 0;
// int gm_ov = 0; // vase game over shodan
static ArrayList<Integer> X = new ArrayList<>();
static ArrayList<Integer> Y = new ArrayList<>();
static int Domx1 =400 , Domy1 =390 ;
static int Domx2 =400, Domy2 =400 ;
abstract public void Drow(Canvas canvas);
abstract public void Move(Scene scene)throws Game_Over_Exception;
void Point() {
if (X.get(0) == x && Y.get(0) == y)
j = 0;
}
void Game_Over() {
for (int i = 1 ; i < X.size() ; i ++) { // inke mokhtasate sare mar tu mokhtasate tanesh hast ya na
if (X.get(0).equals(X.get(i)) && Y.get(0).equals(Y.get(i))) {
GM = 1;
}
}
for (int i = 0 ; i < Mane_x.size() ; i ++) { // inke mokhtasate sare mar be mane hast ya na
if (X.get(0).equals(Mane_x.get(i)) && Y.get(0).equals(Mane_y.get(i))) {
GM = 1;
}
}
}
}
class Apple extends B_Part {
#Override
public void Move(Scene scene) {
if (j == 0) { // ye sib bede ke ru mar nabashe ( rasmesh tu rasme )
do {
x = (int) ( Math.random() * 390 + 1 );
y = (int) ( Math.random() * 590 + 1 );
} while (X.indexOf(x) != -1 && Y.get(X.indexOf(x)) == y || x % 10 != 0 || y % 10 != 0);
/*
inja aval chek kardam tu araylist x hast ya na ag bud sharte aval ok hala sharte do ke tu Y ham mibinim tu hamun shomare khune
y barabare y mast ag bud pas ina bar ham montabeghan va sharte dovom ham ok . 2 sharte akhar ham vase ine ke mare ma faghat mazrab
haye 10 and pas ta vaghti in se shart bargharare jahayie ke ma nemikhaym va hey jaye dg mide
*/
j = 1;
}
}
#Override
public void Drow(Canvas canvas) {
DropShadow dd = new DropShadow(20,Color.RED);
GraphicsContext a = canvas.getGraphicsContext2D();
a.setFill(Color.RED);
a.setEffect(dd);
a.fillRect( x , y , 9 ,9);
a.setEffect(null);
}
}
class Snake extends B_Part {
Snake() { //cunstructor
X.add(400);
Y.add(300);
X.add(400);
Y.add(310);
X.add(400);
Y.add(320);
X.add(400);
Y.add(330);
X.add(400);
Y.add(340);
X.add(400);
Y.add(350);
X.add(400);
Y.add(360);
X.add(400);
Y.add(370);
X.add(400);
Y.add(380);
}
#Override
public void Drow(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
// keshidane mar (body yeki ezafe tar az adade morabaA mide)
for (int i = X.size() - 1; i >= 0; i--) {
gc.fillRect(X.get(i), Y.get(i), 9, 9);
gc.setStroke(Color.WHITE);
}
}
#Override
public void Move(Scene scene) throws Game_Over_Exception {
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent small) {
switch (small.getText()) {
case "W":
if (!goSouth) {
goNorth = true;
goSouth = false;
goWest = false;
goEast = false;
}
break;
case "w":
if (!goSouth) {
goNorth = true;
goSouth = false;
goWest = false;
goEast = false;
}
break;
case "S":
if (!goNorth) {
goSouth = true;
goNorth = false;
goWest = false;
goEast = false;
}
break;
case "s":
if (!goNorth) {
goSouth = true;
goNorth = false;
goWest = false;
goEast = false;
}
break;
case "A":
if (!goEast) {
goWest = true;
goEast = false;
goSouth = false;
goNorth = false;
}
break;
case "a":
if (!goEast) {
goWest = true;
goEast = false;
goSouth = false;
goNorth = false;
}
break;
case "D":
if (!goWest) {
goEast = true;
goWest = false;
goSouth = false;
goNorth = false;
}
break;
case "d":
if (!goWest) {
goEast = true;
goWest = false;
goSouth = false;
goNorth = false;
}
break;
}
}
});
/* scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent e) {
switch (e.getCode()) {
case UP:
if (!goSouth) {
goNorth = true;
goSouth = false;
goWest = false;
goEast = false;
}
break;
case DOWN:
if (!goNorth) {
goSouth = true;
goNorth = false;
goWest = false;
goEast = false;
}
break;
case LEFT:
if (!goEast) {
goWest = true;
goEast = false;
goSouth = false;
goNorth = false;
}
break;
case RIGHT:
if (!goWest) {
goEast = true;
goWest = false;
goSouth = false;
goNorth = false;
}
break;
}
}
});
*/
Domx1 = X.get(X.size() - 1);
Domy1 = Y.get(Y.size() - 1);
for (int z = X.size() - 1; z > 0; z--) {
X.remove(z);
X.add(z, X.get(z - 1));
Y.remove(z);
Y.add(z, Y.get(z - 1));
}
if (goNorth) {
Y.add(0, Y.get(0) - 10);
Y.remove(1);
}
if (goSouth) {
Y.add(0, Y.get(0) + 10);
Y.remove(1);
}
if (goEast) {
X.add(0, X.get(0) + 10);
X.remove(1);
}
if (goWest) {
X.add(0, X.get(0) - 10);
X.remove(1);
}
Point(); // emtiaz gerefte
if (j == 0) {
X.add(Domx1);
Y.add(Domy1);
X.add(Domx2);
Y.add(Domy2);
Domx2 = Domx1;
Domy2 = Domy1;
System.out.println();
System.out.println("size : "+Mane_x.size() );
System.out.print(" clear : " );
Mane_x.clear();
Mane_y.clear();
System.out.println("size : "+Mane_x.size() );
}
Game_Over();
if ( GM == 1 ) {
throw new Game_Over_Exception("Game Over");
}
if (X.get(0) > 390) {
X.remove(0);
X.add(0, 0);
}
if (X.get(0) < 0) {
X.remove(0);
X.add(0, 400);
}
if (Y.get(0) > 590) {
Y.remove(0);
Y.add(0, 0);
}
if (Y.get(0) < 0) {
Y.remove(0);
Y.add(0, 600);
}
}
}
class Mane extends B_Part{
#Override
public void Drow(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
for ( int i = 0 ; i < Mane_x.size() ; i++) {
gc.setFill(Color.GRAY);
gc.fillRect(Mane_x.get(i), Mane_y.get(i), 9, 9);
}
//System.out.println(Mane_x.get(i)+" "+Mane_y.get(i));}
}
#Override
public void Move(Scene scene) {
if (j == 0) { // ye sib bede ke ru mar nabashe ( rasmesh tu rasme )
do {
Mx = (int) (Math.random() * 390 + 1);
My = (int) (Math.random() * 590 + 1);
} while (Mx == x && My == y ||
Mane_x.indexOf(Mx) != -1 && Mane_y.get(Mane_x.indexOf(Mx)) == My ||
X.indexOf(Mx) != -1 && Y.get(X.indexOf(Mx)) == My ||
Mx % 10 != 0 || My % 10 != 0
/* Mx == X.get(0) + 10 && My == Y.get(0) || // yeki joloye saresh nayofte (vaghti rast mire)
Mx == X.get(0) - 10 && My == Y.get(0) || // ( vaghti chap mire)
Mx == X.get(0) && My == Y.get(0) + 10 || // yek ta jolo tar az saresh nayofte vaghtti payi
Mx == X.get(0) && My == Y.get(0) - 10*/); // vaghti bala
//sharte chek kardane in ke har maneye random rooye mar ya maneye ghablia nayofte ( sib hamintor )
Mane_x.add(Mx);
Mane_y.add(My);
}
}
}
class Game_Over_Exception extends Exception{
public Game_Over_Exception (String s){
super(s);
}
}
Hand over a restart method to the alert handler. You can do that directly or via interface, e. g.:
public interface GameManager {
public void restart();
}
Your main class must implement that interface:
public class Main_Snake extends Application implements GameManager {
public void restart() {
// TODO: reset playfield & restart game
}
public void play() {
...
try {
snake.Move(canvas.getScene());
} catch (Game_Over_Exception e) {
stop();
alart.Game_over_alart(snake.X.size(), Main_Snake.this);
}
...
}
}
And in your alart class you invoke that method:
class alart {
public static void Game_over_alart(int dom, GameManager gameManager) {
...
newgame.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
// TODO: close dialog
// restart game
gameManager.restart();
}
});
...
}
}
I'm having trouble getting the Hit button to work in my BlackJack program. I tried programming the action for the hit button in the setOnAction block as well as lambda, but I get an error about the variables not being final. So, I tried it this way, but I don't think my variables are carrying over. It's probably something very simple. Please help if you can. Thanks!
import java.util.Arrays;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class BlackJackGame extends Application {
public String btHitY;
public int NumberPlayerCards;
public int NumberDealerCards;
public int NUMBER_OF_CARDS;
public int PlayerCards[];
public int DealerCards[];
public Image imagesP[];
public Image imagesD[];
public int deck[];
public String URLBase;
public GridPane pane;
public BlackJackGame() {
this.btHitY = " ";
this.btHitY = new String();
}
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//Create array deck, suit string, and rank string
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int NUMBER_OF_CARDS = 4;
String URLBase = "http://www.cs.armstrong.edu/liang/common/image/card/";
//Initialize the cards
for (int i = 0; i < deck.length; i++)
deck[i] = i;
//Shuffle the cards
for (int i = 0; i < deck.length; i++) {
//Generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
int NumberPlayerCards = 2;
int NumberDealerCards = 2;
int[] PlayerCards = new int[50];
int[] DealerCards = new int[50];
//Display the first cards
for (int i = 0; i < 8; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);
}
for (int i = 0; i < NumberPlayerCards; i++)
PlayerCards[i] = deck[i * 2];
for (int i = 0; i < NumberDealerCards; i++)
DealerCards[i] = deck[i * 2 + 1];
// Create a pane to hold the image views
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(5, 5, 5, 5));
pane.setHgap(5);
pane.setVgap(5);
Image[] imagesP = new Image[50];
Image[] imagesD = new Image[50];
for (int i = 0; i < NumberPlayerCards; i++) {
int cardForPrint = PlayerCards[i] + 1;
System.out.println(URLBase + cardForPrint + ".png");
imagesP[i] = new Image(URLBase + cardForPrint + ".png");
}
for (int i = 0; i < NumberDealerCards; i++) {
int cardForPrint = DealerCards[i] + 1;
System.out.println(URLBase + cardForPrint + ".png");
imagesD[i] = new Image(URLBase + cardForPrint + ".png");
}
//rotate flag image to cover dealer card
Image flag = new Image("http://www.cs.armstrong.edu/liang/common/image/us.gif");
ImageView imageFlag = new ImageView(flag);
imageFlag.setRotate(90);
imageFlag.setFitHeight(75);
imageFlag.setFitWidth(95);
pane.add(new Label("Player Cards"), 0, 0);
pane.add(new ImageView(imagesP[0]), 1, 0);
pane.add((imageFlag), 1, 1);
pane.add(new Label("Dealer Cards"), 0, 1);
pane.add(new ImageView(imagesP[1]), 2, 0);
pane.add(new ImageView(imagesD[1]), 2, 1);
Button btHit = new Button("Hit");
Button btStay = new Button("Stay");
pane.add(btHit, 1, 2);
pane.add(btStay, 2, 2);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 1200, 700);
primaryStage.setTitle("Black Jack"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
HitHandlerClass handlerHit = new HitHandlerClass();
btHitY = " ";
btHit.setOnAction(handlerHit);
/* if (btHitY.equals("Hit")); {
NumberPlayerCards = NumberPlayerCards + 1;
NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;
PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];
for (int j = 0; j < NumberPlayerCards; j++){
System.out.println(PlayerCards[j]);
}
System.out.println(NumberPlayerCards);
int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;
imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");
pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);
btHitY = " ";
primaryStage.show();
} */
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
* #param args
*/
public static void main(String[] args) {
launch(args);
}
class HitHandlerClass implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
NumberPlayerCards = NumberPlayerCards + 1;
NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;
PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];
for (int j = 0; j < NumberPlayerCards; j++){
System.out.println(PlayerCards[j]);
}
System.out.println(NumberPlayerCards);
int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;
imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");
pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);
btHitY = " ";
}
}
}
You declare this global
public int deck[];
and then local
int[] deck = new int[52];
So the global never gets initialized and gives you a Nullpointer Exception.
Solution:
deck = new int[52];
Same for the other variables.
public class BlackJackGame extends Application {
//snip
public int NUMBER_OF_CARDS;
public int deck[];
//snip
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//Create array deck, suit string, and rank string
int[] deck = new int[52];
int NUMBER_OF_CARDS = 4;
//snip
You've declared deck[] and NUMBER_OF_CARDS at a higher scope, then redeclared them later, at a lower scope.
To fix these issues, simply remove the type declaration in the start method.
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//Create array deck, suit string, and rank string
deck = new int[52];
NUMBER_OF_CARDS = 4;
//snip
As a side note, I would like to recommend you read up a little bit on Code conventions for Java/Javafx, as they help other programmers read your code by providing a standard style.
I tried to create a spring like behavior with JavaFX by creating draggable circles. When I drag one circle, the others should follow and simulate elasticity.
I created a boilerplate template which contains 3 circles, they are draggable via mouse. The animation is running, of course all appears still because currently speed is 0. All I need to know is how to calculate the angle and the speed of the attached particles.
It would be great if someone could help me.
Here's the code:
import java.util.ArrayList;
import java.util.List;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;
public class PhysicsTest extends Application {
List<Particle> particles = new ArrayList<>();
List<Spring> springs = new ArrayList<>();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Group root = new Group();
// create particles
Particle pRed = new Particle(Color.RED, 100, 100);
Particle pBlue = new Particle(Color.BLUE, 400, 200);
Particle pGreen = new Particle(Color.GREEN, 100, 300);
// red -> blue
Line lineRedBlue = new Line(100, 100, 500, 500);
lineRedBlue.setStroke(Color.BLACK);
lineRedBlue.setStrokeWidth(5);
// green -> blue
Line lineGreenBlue = new Line(100, 100, 500, 500);
lineGreenBlue.setStroke(Color.BLACK);
lineGreenBlue.setStrokeWidth(5);
// line binding
// line 1 -> 2
lineRedBlue.startXProperty().bind(pRed.centerXProperty());
lineRedBlue.startYProperty().bind(pRed.centerYProperty());
lineRedBlue.endXProperty().bind(pBlue.centerXProperty());
lineRedBlue.endYProperty().bind(pBlue.centerYProperty());
// line 3 -> 2
lineGreenBlue.startXProperty().bind(pGreen.centerXProperty());
lineGreenBlue.startYProperty().bind(pGreen.centerYProperty());
lineGreenBlue.endXProperty().bind(pBlue.centerXProperty());
lineGreenBlue.endYProperty().bind(pBlue.centerYProperty());
MouseGestures mg = new MouseGestures();
mg.makeDraggable(pRed);
mg.makeDraggable(pBlue);
mg.makeDraggable(pGreen);
root.getChildren().addAll(pRed, pBlue, pGreen, lineRedBlue, lineGreenBlue);
// add to list
particles.add( pRed);
particles.add( pBlue);
particles.add( pGreen);
// add springs
Spring s1 = new Spring( pRed, pBlue, 10, 0.5);
springs.add( s1);
Spring s2 = new Spring( pGreen, pBlue, 10, 0.5);
springs.add( s2);
primaryStage.setScene(new Scene(root, 1024, 768));
primaryStage.show();
// animate
startAnimation();
}
private void startAnimation() {
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
// move particles
for (Particle p : particles) {
if (!p.selected) {
p.move();
}
}
// apply springs
for (Spring s : springs) {
s.update();
}
// move particles to new location
for (Particle p : particles) {
p.updateLocation();
}
}
};
timer.start();
}
/**
* The spring constraint and calculation. Updates particle
*/
public class Spring {
Particle p1;
Particle p2;
double length; // length it tries to obtain
double strength; // how quickly it tries to reach that length
public Spring( Particle p1, Particle p2, double length, double strength) {
this.p1 = p1;
this.p2 = p2;
this.length = length;
this.strength = strength;
}
public void update() {
double dx = p1.getCenterX() - p2.getCenterX();
double dy = p1.getCenterY() - p2.getCenterY();
double dist = Math.hypot(dx, dy);
double theta = Math.atan2(dy, dx);
double force = (length - dist) * strength;
// System.out.println( dist + ", " + Math.toDegrees( theta) + ", " + force);
// what's supposed to happen here?
p1.angle = ... // <===
p1.speed = ... // <===
p2.angle = ... // <===
p2.speed = ... // <===
}
}
/**
* The particle itself
*/
public class Particle extends Circle {
double x;
double y;
double angle = 0.0;
double speed = 0.0;
double mass = 1;
boolean selected = false;
public Particle(Color color, double x, double y) {
super(x, y, 50);
this.x = x;
this.y = y;
setFill(color.deriveColor(1, 1, 1, 0.5));
setStroke(color);
setStrokeWidth(2);
setStrokeType(StrokeType.OUTSIDE);
}
public void move() {
x += Math.sin( angle) * speed;
y += Math.cos( angle) * speed;
}
public void updateLocation() {
setCenterX( x);
setCenterY( y);
}
}
/**
* Allow movement of objects via mouse.
*/
public class MouseGestures {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
public void makeDraggable( Node node) {
node.setOnMousePressed(circleOnMousePressedEventHandler);
node.setOnMouseDragged(circleOnMouseDraggedEventHandler);
node.setOnMouseReleased(circleOnMouseReleasedEventHandler);
}
EventHandler<MouseEvent> circleOnMousePressedEventHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
orgSceneX = t.getSceneX();
orgSceneY = t.getSceneY();
Particle p = ((Particle) (t.getSource()));
p.selected = true;
orgTranslateX = p.getCenterX();
orgTranslateY = p.getCenterY();
}
};
EventHandler<MouseEvent> circleOnMouseReleasedEventHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
Particle p = ((Particle) (t.getSource()));
p.selected = false;
};
};
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
Particle p = ((Particle) (t.getSource()));
p.x = newTranslateX;
p.y = newTranslateY;
}
};
}
}
The update() method in the Spring class is the unknown territory:
public void update() {
double dx = p1.getCenterX() - p2.getCenterX();
double dy = p1.getCenterY() - p2.getCenterY();
double dist = Math.hypot(dx, dy);
double theta = Math.atan2(dy, dx);
double force = (length - dist) * strength;
// System.out.println( dist + ", " + Math.toDegrees( theta) + ", " + force);
// what's supposed to happen here?
p1.angle = ... // <===
p1.speed = ... // <===
p2.angle = ... // <===
p2.speed = ... // <===
}
Here's a screenshot of how it looks like currently:
Thank you very much!
Ok, adding a damping and a non compressable spring:
import java.util.ArrayList;
import java.util.List;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;
public class SpringField extends Application {
MouseGestures mg = new MouseGestures();
double damping = 0.995;
double speedo = 0.001;
List<Particle> particles = new ArrayList<>();
List<Spring> springs = new ArrayList<>();
public static void main(String[] args) {
launch(args);
}
Particle addParticle(Group parent, Paint p, double x, double y, double mass) {
Particle particle = new Particle(p, x, y, mass);
mg.makeDraggable(particle);
particles.add(particle);
parent.getChildren().add(particle);
return particle;
}
void addSpring(Group parent, Particle p1, Particle p2, double length, double strength) {
Spring spring = new Spring(parent, p1, p2, length, strength);
springs.add(spring);
}
#Override
public void start(Stage primaryStage) {
Group root = new Group();
// create particles
Particle pRed = addParticle(root, Color.RED, 300, 100, 10);
Particle pBlue = addParticle(root, Color.BLUE, 600, 200, 1);
Particle pGreen = addParticle(root, Color.GREEN, 300, 300, 1);
// add springs
addSpring(root, pRed, pBlue, 100, 0.5);
addSpring(root, pGreen, pBlue, 100, 0.5);
addSpring(root, pGreen, pRed, 100, 0.5);
primaryStage.setScene(new Scene(root, 1024, 768));
primaryStage.show();
// animate
startAnimation();
}
private void startAnimation() {
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
// move particles
for (Particle p : particles) {
if (!p.selected) {
p.move();
}
}
// apply springs
for (Spring s : springs) {
s.update();
}
// move particles to new location
for (Particle p : particles) {
p.updateLocation();
}
}
};
timer.start();
}
/**
* The spring constraint and calculation. Updates particle
*/
public class Spring {
Particle p1;
Particle p2;
double length; // length it tries to obtain
double strength; // how quickly it tries to reach that length
public Spring(Group parent, Particle p1, Particle p2, double length, double strength) {
this.p1 = p1;
this.p2 = p2;
this.length = length;
this.strength = strength;
Line lineRedBlue = new Line(100, 100, 500, 500);
lineRedBlue.setStroke(Color.BLACK);
lineRedBlue.setStrokeWidth(5);
lineRedBlue.startXProperty().bind(p1.centerXProperty());
lineRedBlue.startYProperty().bind(p1.centerYProperty());
lineRedBlue.endXProperty().bind(p2.centerXProperty());
lineRedBlue.endYProperty().bind(p2.centerYProperty());
parent.getChildren().add(lineRedBlue);
}
public void update() {
double stop = 1.0;
double dx = p1.getCenterX() - p2.getCenterX();
double dy = p1.getCenterY() - p2.getCenterY();
double dist = Math.hypot(dx, dy);
double theta = Math.atan2(dy, dx);
double force = (length - dist) * strength;
if (force > 0) { force *= 4; stop = 0.9; }
// System.out.println( dist + ", " + Math.toDegrees( theta) + ", " + force);
Point2D p1v = new Point2D(force*Math.cos(theta)*speedo/p1.mass, force*Math.sin(theta)*speedo/p1.mass);
Point2D p2v = new Point2D(-force*Math.cos(theta)*speedo/p2.mass, -force*Math.sin(theta)*speedo/p2.mass);
p1.vector = p1.vector.add(p1v).multiply(stop);
p2.vector = p2.vector.add(p2v).multiply(stop);
}
}
/**
* The particle itself
*/
public class Particle extends Circle {
double x;
double y;
Point2D vector = new Point2D(0, 0);
double mass = 1;
boolean selected = false;
public Particle(Paint color, double x, double y, double mass) {
super(x, y, 50);
this.x = x;
this.y = y;
this.mass = mass;
setFill(color);
setStroke(color);
setStrokeWidth(2);
setStrokeType(StrokeType.OUTSIDE);
}
public void move() {
x += vector.getX();
y += vector.getY();
vector = vector.multiply(damping);
}
public void updateLocation() {
setCenterX( x);
setCenterY( y);
}
}
/**
* Allow movement of objects via mouse.
*/
public class MouseGestures {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
public void makeDraggable( Node node) {
node.setOnMousePressed(circleOnMousePressedEventHandler);
node.setOnMouseDragged(circleOnMouseDraggedEventHandler);
node.setOnMouseReleased(circleOnMouseReleasedEventHandler);
}
EventHandler<MouseEvent> circleOnMousePressedEventHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
orgSceneX = t.getSceneX();
orgSceneY = t.getSceneY();
Particle p = ((Particle) (t.getSource()));
p.selected = true;
orgTranslateX = p.getCenterX();
orgTranslateY = p.getCenterY();
}
};
EventHandler<MouseEvent> circleOnMouseReleasedEventHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
Particle p = ((Particle) (t.getSource()));
p.selected = false;
};
};
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
Particle p = ((Particle) (t.getSource()));
p.x = newTranslateX;
p.y = newTranslateY;
}
};
}
}
First I would propose to not use angle and speed in your Particle, but Point2D as a movement vector:
public class Particle extends Circle {
double x;
double y;
Point2D vector = new Point2D(0, 0);
that simplifies the later update calulation...
Then you might programm update as follows:
public void update() {
double dx = p1.getCenterX() - p2.getCenterX();
double dy = p1.getCenterY() - p2.getCenterY();
double dist = Math.hypot(dx, dy);
double theta = Math.atan2(dy, dx);
double force = (length - dist) * strength;
// System.out.println( dist + ", " + Math.toDegrees( theta) + ", " + force);
Point2D p1v = new Point2D(force*Math.cos(theta)/p1.mass/10000, force*Math.sin(theta)/p1.mass/10000);
Point2D p2v = new Point2D(-force*Math.cos(theta)/p2.mass/10000, -force*Math.sin(theta)/p2.mass/10000);
p1.vector = p1.vector.add(p1v);
p2.vector = p2.vector.add(p2v);
}
That ignores any collisions, but will provide a fair physics model.
Well here was my approach in a 3D cloth simulation using Verlet Integration:
You can View it here: https://www.youtube.com/watch?v=uRsCcpbsdsg
/**
*
* #author Jason Pollastrini aka jdub1581
*/
#FunctionalInterface
public interface Constraint {
public void solve();
public default void solve(int iter){
IntStream.range(0, iter).parallel().forEach(i->{solve();});
}
}
Then in implementing class (I called it PointLink):
public void solve() {
// calculate the distance between the two PointMasss
Point3D diff = new Point3D(
p1.getPosition().getX() - p2.getPosition().getX(),
p1.getPosition().getY() - p2.getPosition().getY(),
p1.getPosition().getZ() - p2.getPosition().getZ()
);
double d = diff.magnitude();
double difference = (distance - d) / d;
double im1 = 1 / p1.getMass();
double im2 = 1 / p2.getMass();
double scalarP1 = (im1 / (im1 + im2)) * stiffness;
double scalarP2 = stiffness - scalarP1;
p1.position.x = (float) (p1.getPosition().x + diff.x * scalarP1 * difference);
p1.position.y = (float) (p1.getPosition().y + diff.y * scalarP1 * difference);
p1.position.z = (float) (p1.getPosition().z + diff.z * scalarP1 * difference);
p2.position.x = (float) (p2.getPosition().x - diff.x * scalarP2 * difference);
p2.position.y = (float) (p2.getPosition().y - diff.y * scalarP2 * difference);
p2.position.z = (float) (p2.getPosition().z - diff.z * scalarP2 * difference);
}
Then in the Main class something like this:
public void solveConstraints() {
constraints.values().parallelStream().forEach((Constraint c) -> {
c.solve();
});
}
public void updatePhysics(double dt, double t) {
if (isAnchored()) {
setPosition(getAnchorPosition());
return;
}
Point3D vel = new Point3D(
(position.x - oldPosition.x),
(position.y - oldPosition.y),
(position.z - oldPosition.z)
);
float dtSq = (float) (dt * dt);
// calculate the next position using Verlet Integration
Point3D next = new Point3D(
position.x + vel.x + (((force.x / (float) (mass)) * 0.5f) * dtSq),
position.y + vel.y + (((force.y / (float) (mass)) * 0.5f) * dtSq),
position.z + vel.z + (((force.z / (float) (mass)) * 0.5f) * dtSq)
);
// reset variables
setOldPosition(position);
setPosition(next);
setForce(new Point3D(0, 0, 0));
}
Note that I used a simple class with double x,y,z as public members for ease of use for position variable...