Set up a JavaFX GUIbased program that shows a 10 times 10 gr
Solution
Code:
//multiTable.java
// import required packages
import java.util.List;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
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.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
// class for revrse multiplication table
public class multiTable extends Application
{
// over ride nethod for setup the grid
@Override
public void start(Stage primaryStage)
{
BorderPane pane1 = new BorderPane();
pane1.setTop(getHbox1());
HBox h_box = new HBox(15);
h_box.setPadding(new Insets(15, 15, 15, 15));
h_box.setAlignment(Pos.TOP_CENTER);
h_box.getStyleClass().add(\"hbox2\");
Label labl = new Label(\"Enter Answer: \");
h_box.getChildren().add(labl);
TextField textfield1 = new TextField();
h_box.getChildren().add(textfield1);
GridPane grid_pane1 = setUpGrid();
gridmaker griddp = new gridmaker(grid_pane1);
Button Answer = new Button(\"Find problems\");
Answer.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>()
{
@Override
public void handle(Event arg0)
{
List<int[]> fact = factors(textfield1);
for (int[] RCx1 : fact) {
Node node = griddp.get_chldren()[RCx1[0]][RCx1[1]];
node.setStyle(\"-fx-background-color: green\");
}
}
});
h_box.getChildren().add(Answer);
pane1.setCenter(h_box);
pane1.setBottom(grid_pane1);
Scene scene = new Scene(pane1, 550, 650);
scene.getStylesheets().add(getClass().getResource(\"application.css\").toExternalForm());
primaryStage.setTitle(\"lab 6\");
primaryStage.setScene(scene);
primaryStage.show();
}
// method for grid layout
private HBox getHbox1()
{
HBox hbox2 = new HBox(15);
hbox2.setPadding(new Insets(15, 15, 15, 15));
hbox2.setAlignment(Pos.TOP_CENTER);
hbox2.getStyleClass().add(\"hbox1\");
Label labl = new Label(\"Multiplication Table\");
hbox2.getChildren().add(labl);
return hbox2;
}
// method to setup a grid
public GridPane setUpGrid() {
GridPane pane1 = new GridPane();
Label[][] labels2 = new Label[11][11];
for (int nrow = 0; nrow < 11; nrow++) {
for (int ncol = 0; ncol < 11; ncol++) {
Label labl1 = new Label();
setUpLabel(labl1, ncol, nrow);
labels2[nrow][ncol] = labl1;
pane1.add(labl1, ncol, nrow);
}
}
return pane1;
}
// method to setup label
public void setUpLabel(final Label labl1, final int ncol, final int nrow)
{
labl1.setPrefHeight(50);
labl1.setPrefWidth(50);
labl1.setAlignment(Pos.CENTER);
labl1.setStyle(\"-fx-stroke-border: black; -fx-border-width: 1;\");
String na = String.valueOf(nrow);
String nb = String.valueOf(ncol);
if (nrow == 0 || ncol == 0) {
labl1.getStyleClass().add(\"gridBorders\");
if (nrow == 0) {
labl1.setText(nb);
} else if (ncol == 0) {
labl1.setText(na);
}
} else {
labl1.setText(na + \" * \" + nb);
labl1.getStyleClass().add(\"gridInside\");
}
}
// method to setup display factor
public List<int[]> factors(TextField pblm)
{
FactorCalc calcul = new FactorCalc();
int numb = Integer.parseInt(pblm.getText());
System.out.println(numb);
List<int[]> factors = calcul.FactFind(numb, 10);
System.out.println(factors);
return factors;
}
// main method
public static void main(String[] args) {
launch(args);
}
}
//gridmaker.java
// import required packages
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
// class gridmaker
public class gridmaker
{
// pane object
GridPane grid_pane1;
//constructor
public gridmaker(GridPane grid_pane1)
{
this.grid_pane1 = grid_pane1;
}
// grid size
private int getSize()
{
return grid_pane1.getChildren().size();
}
// method to get column size
public int getColSize()
{
int No_rows = grid_pane1.getRowConstraints().size();
for (int coli = 0; coli < grid_pane1.getChildren().size(); coli++)
{
Node chld = grid_pane1.getChildren().get(coli);
if (chld.isManaged())
{
int Col_index = GridPane.getColumnIndex(chld);
int columnEnd = GridPane.getColumnIndex(chld);
No_rows = Math.max(No_rows, (columnEnd != GridPane.REMAINING ? columnEnd : Col_index) + 1);
}
}
return No_rows;
}
// method to get row size
public int getSizeRow()
{
int No_rows = grid_pane1.getRowConstraints().size();
for (int coli = 0; coli < grid_pane1.getChildren().size(); coli++) {
Node chld = grid_pane1.getChildren().get(coli);
if (chld.isManaged()) {
int Row_index = GridPane.getRowIndex(chld);
int Row_End = GridPane.getRowIndex(chld);
No_rows = Math.max(No_rows, (Row_End != GridPane.REMAINING ? Row_End : Row_index) + 1);
}
}
return No_rows;
}
// mehtod to ge column childs
public Node[] getChildColoumn(int Col_no) {
if (Col_no < getSizeRow())
{
return get_chldren()[Col_no];
}
return null;
}
// mehtod to get row childs
public Node[] get_child_row(int RCrowNo)
{
Node sz[] = new Node[getSizeRow()];
if (RCrowNo <= getSizeRow())
{
for (int coli = 0; coli < getSizeRow(); coli++)
{
sz[coli] = getChildColoumn(coli)[RCrowNo];
}
return sz;
}
return null;
}
// mehtod to get child row
public Node[] get_child_row_vise()
{
Node sz[] = new Node[getSize()];
int ncol = getColSize();
int Incr = 0;
for (int coli = 0; coli < ncol; coli++)
{
for (Node sz1 : get_child_row(coli))
{
if (sz1 != null)
{
sz[Incr] = sz1;
Incr++;
}
}
}
return sz;
}
// method to get the childs
public Node[][] get_chldren() {
Node[][] RCnodes = new Node[getSizeRow()][getColSize()];
for (Node RCnode : grid_pane1.getChildren()) {
int nrow = grid_pane1.getRowIndex(RCnode);
int RCcolumn = grid_pane1.getColumnIndex(RCnode);
RCnodes[nrow][RCcolumn] = RCnode;
}
return RCnodes;
}
// method to get the position
public Integer RCpostion(Node RCnode, Pos RCpos) {
if (RCnode != null) {
switch (RCpos) {
case RCRow:
return grid_pane1.getRowIndex(RCnode);
case RCColumn:
return grid_pane1.getColumnIndex(RCnode);
}
}
return null;
}
//enumerator
enum Pos
{
RCRow,
RCColumn;
}
}
//FactorCalc.java
// import required packages
import java.util.ArrayList;
import java.util.List;
// class to compute factor
class FactorCalc
{
public List<Integer> valLst = new ArrayList<Integer>();
private int valproblem = 0;
// mehtod for finding the facts
public List<int[]> FactFind(int valproblem, int limit)
{
int incval = 1;
this.valproblem = valproblem;
while (incval <= limit)
{
if (valproblem % incval == 0)
{
valLst.add(incval);
}
incval++;
}
return funcCombi();
}
// method for functional computation
public List<int[]> funcCombi()
{
List<int[]> ValArys = new ArrayList<>();
for (int lp = 0; lp < valLst.size(); lp++)
{
for (int j = 0; j < valLst.size(); j++) {
if (valLst.get(lp) * valLst.get(j) == valproblem)
{
int[] inx = new int[2];
inx[0] = valLst.get(lp);
inx[1] = valLst.get(j);
ValArys.add(inx);
}
}
}
return ValArys;
}
}
//application.css
.hbox1 {
-fx-background-color: gray;
}
.hbox2 {
-fx-background-color: white;
}
.gridBorders {
-fx-background-color: red;
-fx-text-fill:#A3FF47;
-fx-border-style: solid;
-fx-border-width: 1;
-fx-stroke-border: black;
}
.gridInside {
-fx-background-color: gray;
-fx-text-fill: white;
-fx-border-style: solid;
-fx-border-width: 1;
-fx-stroke-border: black;
}
.gridAnswer {
-fx-background-color: white;
-fx-text-fill: black;
}





