15 Replies - 5114 Views - Last Post: 31 October 2010 - 09:45 PM
#1
Write a 2d array to JTextArea?
Posted 31 October 2010 - 05:37 PM
Replies To: Write a 2d array to JTextArea?
#3
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 06:03 PM
I have the grid in a separate class from where I add all buttons so im guessing I would
private Sudoku grid[][]; at the top to reference it?
EDIT- in the class I also call a print() method which prints it to the terminal, so I'm guessing a viable option would be to just call that method and output it to the JTextArea?
This post has been edited by Wolfy200222: 31 October 2010 - 06:22 PM
#4
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 06:33 PM
Quote
Use the append() method write lines of text to the text area.
RE: edit: yes,assuming the text area is in scope there.
#5
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 06:43 PM
Im not sure if this is right but I reference the grid in the other class using; private Sudoku grid[][];
then under the actionperformed method for open include; text.append(grid[][])
This post has been edited by Wolfy200222: 31 October 2010 - 06:43 PM
#6
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 07:40 PM
EDIT - is append the right method seeming its a 2d array of ints?
This post has been edited by Wolfy200222: 31 October 2010 - 07:40 PM
#7
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 07:49 PM
post your code.
#8
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 07:54 PM
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
//import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Sudoku extends JFrame{
private int[][] grid;
private int[] options;
public Sudoku () {
grid = new int[9][9];
}
public void load(String fileName) throws IOException
{ try {
File myFile = new File(fileName);
FileReader fileReader = new FileReader(myFile);
BufferedReader reader = new BufferedReader(fileReader);
String line = " ";
int j = 0;
while ((line = reader.readLine()) != null) {
for (int i = 0; i<9 && j< 9; ++i) {
if (line.charAt(i) == ' ') {
grid[j][i] = 0;
} else {
grid[j][i] = line.charAt(i)-'0';
}System.out.print("|");
System.out.print(grid[j][i]);
}
j++;
System.out.println();
}
reader.close();
} catch (IOException e) {
System.out.println("Failed");
}
}
public void solve() {
int complete = 0;
do {
complete = 0;
for(int i = 0; i<3; ++i){
for(int j = 0; j<3; ++j){
options = new int[9];
int x = 0;
int valuesLength;
for(int l = i*3; l<(i+1)*3; ++l){
for(int m = j*3; m<(j+1)*3; ++m){
if (grid[l][m] != 0)
options[x++] = grid[l][m];
}
}
valuesLength = x;
x = 0;
int[] candidates = new int[9];
int candidatesLength = 0;
for(int o=1; o<10; ++o){
boolean flag = false;
for(int op=0; op<valuesLength; op++){
if (o == options[op]) {
flag = true;
break;
}
}
if (flag == false)
candidates[x++] = o;
}
candidatesLength = x;
for (x = 0; x < candidatesLength; x++) {
int curr_candidate = candidates[x];
int count = 0;
int lx = 0, ly = 0;
for(int l = i*3; l<(i+1)*3; ++l){
for(int m = j*3; m<(j+1)*3; ++m){
if (grid[l][m] == 0) {
boolean flag = true;
//check in row
for (int k = 0; k < 9 && flag; k++) {
if (grid[l][k] == curr_candidate) {
flag = false;
}
}
// not in row,
//check in col
for (int k = 0; k < 9 && flag; k++) {
if (grid[k][m] == curr_candidate) {
flag = false;
}
}
if (flag) {
++count;
lx = l;
ly = m;
}
}
}
}
if (count == 1) {
grid[lx][ly] = curr_candidate;
++complete;
}
}
}
}
} while(complete != 0);
}
/* To implement later
public void fileChooser(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
fileChooser.setDialogTitle("Choose a file");
this.getContentPane().add(fileChooser);
fileChooser.setVisible(true);
}
*/
public void printSudoku() {
int i, j;
System.out.println("\n");
for (i = 0; i<9; ++i) {System.out.print("|");
for (j = 0; j<9; ++j) {
System.out.print(grid[i][j] + "|");
}
System.out.println();
}
}
}
Function
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class Functions extends JFrame implements ActionListener{
private JButton open, solve;
private Sudoku sudoku;
private JTextArea text;
public Functions() {
super("Sudoku Solver by dng9");
sudoku = new Sudoku();
FlowLayout flowLay = new FlowLayout();
this.setLayout(flowLay);
JTextArea text = new JTextArea();
text.setPreferredSize(new Dimension(100, 150));
text.setEditable(false);
add(text);
open = new JButton("Open");
add(open);
open.addActionListener(this);
solve = new JButton("Solve");
add(solve);
solve.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
if (actionCommand.equals("Open")) {
try {
sudoku.load("web.sud");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (actionCommand.equals("Solve")){
sudoku.solve();
sudoku.printSudoku();
}
}
}
Main
import java.io.IOException;
import javax.swing.JFrame;
class MainModel extends JFrame{
public MainModel() {
Functions func = new Functions();
func.setSize(275,200);
func.setVisible(true);
}
public static void main(String args[]) throws IOException{
MainModel app=new MainModel();
}
}
I would like to get the output of the load()method into the JTextArea, then either print alongide by making the JTextArea bigger or over-ride the text there with the output of the solve() method
This post has been edited by Wolfy200222: 31 October 2010 - 07:57 PM
#9
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 08:04 PM
text.clear();
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[i].length; j++) {
text.append(" " + grid[i][j];
}
text.append("\n");
}
or have a look at my multiple Sudoku tutorial starting by this one
http://www.dreaminco...-i-basic-tools/
#10
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 08:06 PM
(I can't say with 100% confidence, as I've never played sudoku so I'm not entirely sure what it is your output is supposed to look like)
This post has been edited by Deviara: 31 October 2010 - 08:10 PM
#11
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 08:11 PM
Deviara, on 31 October 2010 - 09:06 PM, said:
text.append("" + intValue);
#12
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 08:18 PM
To clear confusion am I meant to implement your code section in my functions class where my textarea is or make a textarea reference in my Sudoku class and call the method under an actionperformed button?
EDIT - I'm also asked to add a cast to the text.clear() bit that takes type Object.
This post has been edited by Wolfy200222: 31 October 2010 - 08:21 PM
#13
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 09:06 PM
public String printSudoku() {
StringBuilder sb = new StringBuilder();
int i, j;
sb.append("\n");
for (i = 0; i < 9; ++i) {
sb.append("|");
for (j = 0; j < 9; ++j) {
sb.append(grid[i][j]);
sb.append("|");
}
sb.append("\n");
}
System.out.println(sb.toString());
return sb.toString();
}
The caller is an ActionListener:
...
solve = new JButton("Solve");
add(solve);
solve.addActionListener(new solveSudoku(text));
...
And solveSudoku() is defined as:
class solveSudoku implements ActionListener {
JTextArea myTextArea;
public solveSudoku(JTextArea ta) {
myTextArea = ta;
}
public void actionPerformed(ActionEvent e) {
String text;
sudoku.solve();
text = sudoku.printSudoku();
myTextArea.append(text);
}
}
Output to console:
Quote
|0|6|0|1|0|4|0|5|0
|0|0|8|3|0|5|6|0|0
|2|0|0|0|0|0|0|0|1
|8|0|0|4|0|7|0|0|6
|0|0|6|0|0|0|3|0|0
|7|0|0|9|0|1|0|0|4
|5|0|0|0|0|0|0|0|2
|0|0|7|2|0|6|9|0|0
|0|4|0|5|0|8|0|7|0
|9|6|3|1|7|4|2|5|8|
|1|7|8|3|2|5|6|4|9|
|2|5|4|6|8|9|7|3|1|
|8|2|1|4|3|7|5|9|6|
|4|9|6|8|5|2|3|1|7|
|7|3|5|9|6|1|8|2|4|
|5|8|9|7|1|3|4|6|2|
|3|1|7|2|4|6|9|8|5|
|6|4|2|5|9|8|1|7|3|
BUILD STOPPED (total time: 8 seconds)
#14
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 09:24 PM
sudoku.solve();
in the solveSudoku() class you provided =/
Edit - Nevermind I've got it to compile and run and solve, however it still gives the error after solve and doesnt print to JTextArea - Null pointer around myTextArea.append(text);
This post has been edited by Wolfy200222: 31 October 2010 - 09:31 PM
#15
Re: Write a 2d array to JTextArea?
Posted 31 October 2010 - 09:41 PM
Here's my functions class:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Functions extends JFrame implements ActionListener {
private JButton open, solve;
private Sudoku sudoku;
private JTextArea text;
public Functions() {
super("Sudoku Solver by dng9");
sudoku = new Sudoku();
FlowLayout flowLay = new FlowLayout();
this.setLayout(flowLay);
JTextArea text = new JTextArea();
text.setPreferredSize(new Dimension(100, 150));
text.setEditable(false);
add(text);
open = new JButton("Open");
add(open);
open.addActionListener(this);
solve = new JButton("Solve");
add(solve);
solve.addActionListener(new solveSudoku(text));
}
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
if (actionCommand.equals("Open")) {
try {
sudoku.load("web.sud");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class solveSudoku implements ActionListener {
JTextArea myTextArea;
public solveSudoku(JTextArea ta) {
myTextArea = ta;
}
public void actionPerformed(ActionEvent e) {
String text;
sudoku.solve();
text = sudoku.printSudoku();
myTextArea.append(text);
}
}
}
This post has been edited by n8wxs: 31 October 2010 - 09:44 PM
|
|

New Topic/Question
Reply




MultiQuote




|