Update GridPane with Slider in JavaFx - javafx

I'm new at this, I´m creating a field that solves N x N Matrixes, and I want the spinner to add more TextFields every time I change its value, so the user can select the size of the matrix and then solve it...However I don't know how to do this.
I've tried to attach a listener JavaFX spinner but it didn't work, and it has created some errors in the code.
The code in question...
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
public void start(Stage primaryStage) throws Exception{
Double[][] A = new Double[6][6];
Label tamanoML = new Label("Tamaño Matriz:");
Spinner tamanoMS =new Spinner(1,5,0,1);
int tamano=(int) tamanoMS.getValue();
int r;
Label[] campoL = new Label[6];
TextField campo [][] = new TextField[6][6];
Button resolverB = new Button("Resolver matrix");
resolverB.setOnAction((ActionEvent t) -> {
int n=(int) tamanoMS.getValue();
int cont=0;
for(int i=0; i<n; i++) {
for(int j=0; j<n+1; j++) {
A[i][j]=Double.parseDouble(campo[i][j].getText());
}
}
for (int a = 0; a < n; a++) {
Double temp = 0.0;
temp = A[cont][cont];
for (int y = 0; y < (n + 1); y++) {
A[cont][y] = A[cont][y] / temp;
}
for (int x = 0; x < n; x++) {
if (x!=cont) {
Double c = A[x][cont];
for (int z = 0; z < (n + 1); z++) {
A[x][z] = ((-1 * c) * A[cont][z]) + A[x][z];
}
}
}
cont++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n+1; j++) {
campo[i][j].setText(""+A[i][j]);
}
}
});
Button limpiarb = new Button("Limpiar");
limpiarb.setOnAction((ActionEvent t) -> {
for(int i=0; i<tamano+1; i++) {
for(int j=0; j<tamano+1; j++) {
campo[i][j].setText("0");
}
}
});
GridPane mainPane = new GridPane();
mainPane.setMinSize(650,350);
mainPane.setPadding(new Insets(10,10,10,10));
mainPane.setVgap(5);
mainPane.setHgap(5);
mainPane.setAlignment(Pos.CENTER);
mainPane.add(tamanoML, 0, 0);
mainPane.add(tamanoMS, 1, 0);
mainPane.add(resolverB,0,1);
mainPane.add(limpiarb,1,1);
for(int i=0; i<tamano+1; i++) {
r=i+1;
if(r<tamano+1) {
r=i+1;
campoL[i]=new Label("X"+r);
mainPane.add(campoL[i],i,2);
}else {
campoL[i]=new Label("R");
mainPane.add(campoL[i],i,2);
}
}
for(int i=0; i<tamano; i++) {
for(int j=0; j<tamano+1; j++) {
campo[i][j] = new TextField("0");
mainPane.add(campo[i][j],j,i+3);
}
}
tamanoMS.valueProperty().addListener((newValue) -> {
int s;
for(int i=0; i<tamano+1; i++) {
s=i+1;
if(s<tamano+1) {
s=i+1;
campoL[i]=new Label("X"+s);
mainPane.add(campoL[i],i,2);
}else {
campoL[i]=new Label("R");
mainPane.add(campoL[i],i,2);
}
}
for(int i=0; i<tamano; i++) {
for(int j=0; j<tamano+1; j++) {
campo[i][j] = new TextField("0");
mainPane.add(campo[i][j],j,i+3);
}
}
s=0;
});
Scene scene = new Scene(mainPane);
primaryStage.setScene(scene);
primaryStage.setTitle("Ventas del Dia");
primaryStage.show();
}

Ok so the first thing you want to do is rewrite your listener like this
tamanoMS.valueProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("heard");
int s;
for(int i=0; i<newValue+1; i++) {
s=i+1;
if(s<newValue+1) {
s=i+1;
campoL[i]=new Label("X"+s);
mainPane.add(campoL[i],i,2);
}else {
campoL[i]=new Label("R");
mainPane.add(campoL[i],i,2);
}
}
for(int i=0; i<newValue; i++) {
for(int j=0; j<newValue+1; j++) {
campo[i][j] = new TextField("0");
mainPane.add(campo[i][j],j,i+3);
}
}
s=0;
});
I'm gonna be honest I don't quite understand why you need oldValue and obs but you do. However, the reason your matrix wasn't expanding was because you weren't updating tamano to the most recent number.
You also need to change your spinner to this so that is know the value that is coming out is an integer.
Spinner<Integer> tamanoMS =new Spinner<Integer>(1,5,0,1);

Related

FloodFill method not working properly. (Recursion)

I am trying to make minesweeper game. Everything is working fine except the floodFill method. Once the floodFill method reveals one number, the recursion stops. I do not want the recursion to stop but go on so that it checks other neighbors as well but it does not. I am not sure what I am doing wrong.
package zProject_MineSweeper;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
static final int screenW = 600;
static final int screenH = 600;
static final int unitSize = 40;
static final int rowsNum = screenW/unitSize;
static final int colsNum = screenH/unitSize;
// In this code i have defined bomb as -5
static final int bombNum = -5;
static final int bombGap = 10;
static final double bombProb = 0.05;
static final int fontSize = 20;
int[][] values;
boolean[][] path;
Random random;
MyPanel(){
initializeAll();
initializeValues();
}
private void initializeAll() {
this.addMouseListener(new MyMouseListeners());
this.setPreferredSize(new Dimension(screenW,screenH));
this.setFocusable(true);
this.setBackground(new Color(200,200,200));
random = new Random();
values = new int[rowsNum][colsNum];
path = new boolean[rowsNum][colsNum];
}
private void initializeValues() {
double randomBombProb;
for(int i=0; i<rowsNum; i++) {
for(int j=0; j<colsNum; j++) {
path[i][j] = false;
}
}
for(int i=0; i<rowsNum; i++) {
for(int j=0; j<colsNum; j++) {
randomBombProb = random.nextDouble();
if (randomBombProb< bombProb) {
values[i][j] = bombNum;
}
}
}
for(int i=0; i<rowsNum; i++) {
for(int j=0; j<colsNum; j++) {
if (values[i][j] != bombNum) {
setNumbers(i,j);
}
}
}
}
private void setNumbers(int i, int j) {
//Check number of bombs near each node
int sum = 0;
for(int x=-1; x<2; x++) {
for(int y=-1; y<2; y++) {
int rowx = (i + x + rowsNum) / rowsNum;
int coly = (j + y + colsNum) / colsNum;
if (rowx==1 && coly==1) {
if(values[i+x][j+y] == bombNum) {
sum++;
}
}
}
}
values[i][j] = sum;
}
public void floodFill(int i, int j) {
if(values[i][j]==0) {
for(int x=-1; x<2; x++) {
for(int y=-1; y<2; y++) {
int rowx = (i + x + rowsNum)/rowsNum;
int coly = (j + y + colsNum)/colsNum;
if (rowx == 1 && coly == 1 && !path[i][j]) {
path[i][j] = true;
floodFill(i+x,j+y);
}
}
}
}
else {
path[i][j] = true;
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
private void draw(Graphics g) {
g.setFont(new Font("Times Roman", Font.BOLD, fontSize));
FontMetrics metrics = getFontMetrics(g.getFont());
for(int i=0; i<rowsNum; i++) {
for(int j=0; j<colsNum; j++) {
if (path[i][j]==true) {
drawValue(g,metrics,i,j);
}
else {
g.setColor(new Color(120,120,120));
g.fillRect(i*unitSize, j*unitSize, unitSize, unitSize);
}
}
}
drawGrid(g);
}
private void drawValue(Graphics g, FontMetrics metrics,int i, int j) {
// Centering the numbers in each cell
if (values[i][j] != bombNum) {
g.setColor(new Color(10,10,10));
g.drawString("" + values[i][j],
i*unitSize + unitSize/2 - metrics.stringWidth("" + values[i][j])/2,
j*unitSize + unitSize/2 + fontSize/2);
}
else {
g.setColor(Color.RED);
g.fillOval(i*unitSize+bombGap, j*unitSize+bombGap, unitSize-(2*bombGap), unitSize-(2*bombGap));
}
}
private void drawGrid(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2));
g2.setColor(new Color(10,10,10));
for(int i=0; i<rowsNum; i++) {
g2.drawLine(unitSize*i, 0, unitSize*i, screenH);
}
for(int i=0; i<colsNum; i++) {
g2.drawLine(0, unitSize*i, screenW, unitSize*i);
}
}
public class MyMouseListeners implements MouseListener {
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 1) {
int i = e.getX()/unitSize;
int j = e.getY()/unitSize;
floodFill(i, j);
repaint();
}
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
}

Beginner coder checking if input is part of a cycle?

public Graph (int nb){
this.nbNodes = nb;
this.adjacency = new boolean [nb][nb];
for (int i = 0; i < nb; i++){
for (int j = 0; j < nb; j++){
this.adjacency[i][j] = false;
}
}
}
public void addEdge (int i, int j){
if(!(i<0 || i>=this.nbNodes|| j<0 || j>=this.nbNodes))
{
this.adjacency[i][j] = true;
this.adjacency[j][i] = true;
}
}
public void removeEdge (int i, int j){
if(!(i<0 || i>=this.nbNodes|| j<0 || j>=this.nbNodes))
{
this.adjacency[i][j] = false;
this.adjacency[j][i] = false;
}
}
public int nbEdges(){
int c =0;
for(int i=0; i<this.nbNodes; i++)
{
for(int j= 0; j<this.nbNodes; j++)
{
if(this.adjacency[i][j]==true)
{
c++;
}
}
}
return c/2;
}
public boolean cycle(int start){
return false;
}
A Graph has a number of nodes nbNodes and is characterized by its adjacency matrix adjacency. adjacency[i][j] states whether or not there is an edge from the i-th node to the j-th node.
the graphs are undirected.
I am a beginner coder and am having trouble writing cycle(int start)
It takes as input an integer, g.cycle(i) returns true if the i-th node is part of a cycle (and false otherwise).
does anyone have any idea on how I should approach this?

How to writte matrice(2d array) into listbox

I have tried following code but it writes in vertical way.How can i add 2d array into my listbox
for (int i = 0; i < test.GetLength(0); i++)
{
for (int j = 0; j < test.GetLength(1); j++)
{
ListBox3.Items.Add("-" + test[i, j].ToString());
}
ListBox3.Items.Add("\n");
}
}
This may help you from what I understand from your query;
string listboxstr = "";
for (int i = 0; i < test.GetLength(0); i++)
{
listboxstr = "" + test[i, 0].ToString();
for (int j = 1; j < test.GetLength(1); j++)
{
listboxstr +="-" + test[i, j].ToString();
}
ListBox3.Items.Add(listboxstr);
ListBox3.Items.Add("\n");
}
}

Runtime error SIGSEGV on SPOJ

I'm trying to solve problem ARDA1 - The hunt for Gollum on SPOJ
Here's the link: http://www.spoj.com/problems/ARDA1/
And here's my code:
#include <algorithm>
#include <string>
using namespace std;
string map[2010];
string marshes[310];
char str[310];
int main()
{
int N1, N2, M1, M2 = 0;
freopen("INPUT.txt","rt",stdin);
scanf("%d%d", &N1, &N2);
for (int i = 0; i < N1; i++) {
scanf("%s", &str);
marshes[i] = str;
}
scanf("%d%d", &M1, &M2);
for (int i = 0; i < M1; i++) {
scanf("%s", &str);
map[i] = str;
}
bool isFound = false;
for (int i = 0; i <= M1 - N1; i++)
for (int j = 0; j <= M2 - N2; j++) {
if (map[i][j] == marshes[0][0]) {
bool isSame = true;
for (int t = 0; t < N1; t++) {
if (marshes[t] != map[i+t].substr(j, N2)) {
isSame = false;
break;
}
}
if (isSame) {
printf("(%d,%d)\n", i+1, j+1);
isFound = true;
}
}
}
if (!isFound)
printf("NO MATCH FOUND...");
return 0;
}
But I got "Runtime error (SIGSEGV)" when I submit my solution. I know that we get SIGSEGV when trying to access elements out of bound or when there's not enough memory.
I checked my code but couldn't find anything wrong and it worked on my computer. Anyone can tell me what could be wrong here?

Multiplying 2d arrays

Hey I am trying to multiply two dimensional arrays and I am confused. I have this code, but It Doesn't seem to work..
class MatrixMultiply {
public static void main(String[] args) {
int array[][] = { {1,2,-2,0}, {-3,4,7,2}, {6,0,3,1} };
int array1[][] = { {-1,3}, {0,9}, {1,-11}, {4,-5} };
int array2[][] = new int[2][3];
int x= array.length;
int y= array1.length;
for(int i = 0; i < x; i++) {
for(int j = 0; j < y-1; j++) {
for(int k = 0; k < y; k++){
array2[i][j] += array[i][k]*array1[k][j];
}
}
}
System.out.println("After we multiply the two arrays: ");
for(int i = 0; i < x; i++) {
for(int j = 0; j < y-1; j++) {
System.out.print(" "+array2[i][j]);
}
System.out.println();
}
}
}
modify your loop that multiplies the array.
for (int i = 0; i < x-1; i++) {
for (int j = 0; j < array1[0].length; j++) {
for (int k = 0; k < array[0].length; k++) {
array2[i][j] += array[i][k] * array1[k][j];
}
}
}

Resources