my problem is that when i try to use the GuiOutput directly, it does what i want. it shows the total dimension, then displays 1, delays, then 2, delays, then 3, and etc. it's like i want the user to see the slow individual output of the program.
BUT.. when i use the GuiMsV2 class to input and call the GuiOutput class. it hangs / lags then displays everything as one. its like it wont display until every process is finished and i cant see the displaying of the nos. with delays.
here are the codes of the 2 classes:
GuiMsV2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiMsV2 extends JFrame implements ActionListener
{
int dimensionInt;
int row;
int col;
JPanel inputPanel=new JPanel();
JLabel lbl_1=new JLabel(" Enter an odd dimension for the square: ");
JLabel lbl_2=new JLabel(" Input the starting coordinates: ");
JLabel lbl_space=new JLabel("");
JLabel lbl_x=new JLabel(" X = ");
JLabel lbl_y=new JLabel(" Y = ");
JTextField txt_dimension=new JTextField(3);
JTextField txt_x=new JTextField(3);
JTextField txt_y=new JTextField(3);
JTextField txt_space=new JTextField("");
JPanel buttonsPanel=new JPanel();
JButton btn_solve=new JButton("SOLVE");
JButton btn_clear=new JButton("CLEAR");
public GuiMsV2() {
setTitle(" Magic Square");
setSize(500,130);
setLocation((1024-200)/2,(768-700)/2);
inputPanel.setLayout(new GridLayout(4,2));
buttonsPanel.setLayout(new GridLayout(1,2));
inputPanel.add(lbl_1); inputPanel.add(txt_dimension);
inputPanel.add(lbl_2); inputPanel.add(lbl_space);
inputPanel.add(lbl_x); inputPanel.add(txt_x);
inputPanel.add(lbl_y); inputPanel.add(txt_y);
buttonsPanel.add(btn_solve);
buttonsPanel.add(btn_clear);
btn_solve.addActionListener(this);
btn_clear.addActionListener(this);
add(inputPanel,BorderLayout.NORTH);
add(buttonsPanel,BorderLayout.CENTER);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn_solve)
{
if(checking())
{
String dim=txt_dimension.getText();
String x=txt_x.getText();
String y=txt_y.getText();
dimensionInt = Integer.parseInt(dim);
row = Integer.parseInt(x);
col = Integer.parseInt(y);
GuiOutput gm = new GuiOutput(dimensionInt, row, col);
}
}
if(e.getSource()==btn_clear)
{
txt_dimension.setText("");
txt_x.setText("");
txt_y.setText("");
}
}
private boolean checking()
{
boolean valid = true;
try
{
String dim=txt_dimension.getText();
String x=txt_x.getText();
String y=txt_y.getText();
int dimensionInt = Integer.parseInt(dim);
int row = Integer.parseInt(x);
int col = Integer.parseInt(y);
if(dimensionInt%2==0)
{
JOptionPane.showMessageDialog(null,"Your dimension is not an odd number.\nPlease enter an odd number.","Error!",JOptionPane.ERROR_MESSAGE);
valid = false;
}
else if(row >= dimensionInt || col >= dimensionInt)
{
String d=(dimensionInt-1) + "";
JOptionPane.showMessageDialog(null,"Your coordinates cannot be accessed right now.\nPlease input properly from 0 to "+d,"Error!",JOptionPane.ERROR_MESSAGE);
valid = false;
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Please input number only.","Error!",JOptionPane.ERROR_MESSAGE);txt_dimension.setText(null);
valid = false;
}
return valid;
}
public static void main(String args[]) {
GuiMsV2 s=new GuiMsV2();
}
}
GuiOutput:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
public class GuiOutput extends JFrame
{
JPanel magicSquarePanel;
JTextField txt_boxArray [] [];
public GuiOutput (int dim, int row, int col)
{
setTitle(" Magic Square");
setSize(500,600);
setLocation((1024-200)/2,(768-700)/2);
txt_boxArray =new JTextField[dim] [dim];
magicSquarePanel=new JPanel();
magicSquarePanel.setLayout(new GridLayout(dim,dim));
for(int h=0; h<dim;h++)
{
for (int i=0; i<dim; i++)
{
magicSquarePanel.add(txt_boxArray[h][i]=new JTextField());
txt_boxArray[h][i].setEditable(false);
txt_boxArray[h][i].setHorizontalAlignment(JTextField.CENTER);
}
}
add(magicSquarePanel,BorderLayout.CENTER);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
solve(dim, row, col);
}
private void solve(int dimensionInt, int row, int col)
{
String d=(dimensionInt-1) + "";
int array [][]=new int[dimensionInt][dimensionInt];
array[row][col]=1;
int prow = row;
int pcol = col;
int maxDimension = dimensionInt * dimensionInt;
Delay();
txt_boxArray[row][col].setText("1");
for(int i=2;i<=maxDimension;i++)
{
Delay();
row--;
col--;
if(row < 0)
row=dimensionInt-1;
if(col < 0)
col=dimensionInt-1;
if(array[row][col]!=0)
{
row=prow;
col=pcol;
if(row+1 > dimensionInt-1)
row=0;
else
row++;
}
array[row][col] = i;
txt_boxArray[row][col].setBackground(Color.BLUE);
Delay();
txt_boxArray[row][col].setBackground(null);
Delay();
txt_boxArray[row][col].setText("" + i);
prow = row;
pcol = col;
}
}
private void Delay()
{
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
// ignore
}
}
}
i think there's nothing wrong with the logic already but just the output.. thanks

New Topic/Question
Reply




MultiQuote



|