/* So this is my program, I cannot make all the functions in it work. Can anyone
ID: 3811008 • Letter: #
Question
/*
So this is my program, I cannot make all the functions in it work. Can anyone copy paste it and see what i need to do to make them work?
*/
package AlienTiles;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
/**
*
* @author rijanprajapati
*/
public class AlienTiles extends Application {
private int rows = 7;
private int columns = 7;
private Cell[][] cells = new Cell[rows][columns];
private Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.PURPLE};
private String[] strMenu = {"file", "Game", "Editing"};
private String[][] strMenuItems = {{"Save to file", "Load from File"}, {"New game: Same color", "New game: Random color"}, {"undo"}};
private MenuBar menuBar;
private Menu[] menus;
private MenuItem[][] menuItems;
private final int CELL_LENGTH = 80;
@Override
public void start(Stage primaryStage) {
menuBar = new MenuBar();
menus = new Menu[strMenu.length];
menuItems = new MenuItem[strMenuItems.length][];
for (int i = 0; i < strMenu.length; i++) {
menus[i] = new Menu(strMenu[i]);
menuItems[i] = new MenuItem[strMenuItems[i].length];
for (int j = 0; j < strMenuItems[i].length; j++) {
menuItems[i][j] = new MenuItem(strMenuItems[i][j]);
}
menus[i].getItems().addAll(menuItems[i]);
}
menuBar.getMenus().addAll(menus);
BorderPane borderPane = new BorderPane();
GridPane pane = new GridPane();
borderPane.setBottom(menuBar);
borderPane.setCenter(pane);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
pane.add(cells[i][j] = new Cell(i, j, 0, CELL_LENGTH, CELL_LENGTH), j, i);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cells[i][j].setOnMouseClicked(e -> {
Object src = e.getSource();
if (src instanceof MenuItem) {
return;
}
Cell c = (Cell) src;
for (int k = 0; k < columns; k++) {
cells[c.row][k].forward();
}
for (int k = 0; k < rows; k++) {
if (k != c.row) {
cells[k][c.column].forward();
}
}
});
}
}
Scene scene = new Scene(borderPane, CELL_LENGTH * rows + 5, CELL_LENGTH * columns + 40);
primaryStage.setTitle("Aliengame");
primaryStage.setScene(scene);
primaryStage.show();
}
public class Cell extends Rectangle {
public int row;
public int column;
public int colorIndex = 0;
public Cell(int rowIndex, int columnIndex, int colorIndex, int width, int height) {
super(width, height);
row = rowIndex;
column = columnIndex;
this.colorIndex = colorIndex % colors.length;
setFill(colors[this.colorIndex]);
super.setStroke(Color.BLACK);
}
public Cell(int rowIndex, int columnIndex, int colorIndex) {
this(rowIndex, columnIndex, colorIndex, 40, 40);
}
public Cell() {
this(0, 0, 0);
}
public void forward() {
colorIndex = (colorIndex + 1) % colors.length;
setFill(colors[this.colorIndex]);
super.setStroke(Color.BLACK);
}
public void backward() {
colorIndex = (colorIndex + colors.length - 1) % colors.length;
setFill(colors[this.colorIndex]);
super.setStroke(Color.BLACK);
}
}
public static void main(String[] args) {
launch(args);
}
}
Explanation / Answer
î copy pasted the code . and the code worked fluently .to work correctly.open net beans
1.Click on File New project
2.Under catogories select javafx and on the right side you can see project names .then select JavaFX Application
3.click on next button in the dialod
4.name your application
5.then click on finish
6.Now copy paste your code
7.and rename the file JavaFXApplication3.java to AlienTiles.java.
Please let me know if you need some more info.and specify what needs to be checked.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.