Now this is an assignment for me, but ive spent the last two weeks writing this code day and night, I just cant get it out of my head, I have to beat it lol. So anyway, two days ago I won and completed my program exactly the way I wanted it and I decided to try and use JFrameBuilder to build me a GUI to use my code. Well three sleepless night later its totally beaten me :( I would be so grateful if someone could take a look for me and tell me where im going wrong. I decided once the gui loaded up to use a start button to start running my code. Now I dont want it to do anything fancy, I just was trying to get it to display my code in the JTextArea but its absolutley defeated me. I think I have two problems, one when the code is run, I dont think its pausing to wait for the user input, like at the "Would you like to start a new Towers of Hanoi game? [Y/N]"
prompt. And maybe im trying to execute my code the wrong way???
First i'll post my main GUI code then i'll post my working game code main im trying to integrate.
I'd be eternally grateful if anyone has any ideas.
package visualassignment2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import boardage.*;
import EasyIn.*;
import java.io.*;
class Error extends Exception {
public Error() {
super("*******ERROR*******\n");
}
}
class badInput extends Exception {
public badInput() {
super("Wrong Input type... Please try again!\n");
}
}
public class Main extends JFrame
{
// Declared variables
private JLabel jLabel1;
private JLabel jLabel3;
private JTextField input;
private JTextArea displayScreen;
private JScrollPane jScrollPane1;
private JProgressBar jProgressBar1;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JPanel contentPane;
private board bd;
// End of variables declaration
//Constructor
public Main()
{
super();
initializeComponent();
this.setVisible(true);
bd = new board();
}
//This method is called from within the constructor to initialize the form.
private void initializeComponent()
{
jLabel1 = new JLabel();
jLabel3 = new JLabel();
input = new JTextField();
displayScreen = new JTextArea();
jScrollPane1 = new JScrollPane();
jProgressBar1 = new JProgressBar();
b1 = new JButton();
b2 = new JButton();
b3 = new JButton();
b4 = new JButton();
b5 = new JButton();
b6 = new JButton();
b7 = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// jLabel1
// setting image location
jLabel1.setIcon(new ImageIcon("K:\\Portable Start Menu\\Documents\\BInfoTech\\2nd Year\\Java\\visualassignment2\\build\\hanoititle.jpg"));
//
// jLabel3
// Labels the input on the display
jLabel3.setText("Input");
//
// jTextField1
// This is where the user input goes
input.setText("Type here");
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}
});
input.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e)
{
jTextField1_keyPressed(e);
}
});
//
// jTextArea1
//sets wether the main display for instructions and current gamestate is editable or not
displayScreen.setEditable(false);
// Add display area with scroll bars
//
// jScrollPane1
// scrolls textarea1 when needed
jScrollPane1.setViewportView(displayScreen);
// jProgressBar1
// sets the color of the progress bar
jProgressBar1.setForeground(new Color(246, 5, 21));
//
//
// jButton1
// Move from peg1 to peg2
b1.setBackground(new Color(255, 255, 255));
b1.setForeground(new Color(0, 0, 0));
b1.setText("Peg 1 to 2");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
//
// jButton2
// Move from peg1 to peg3
b2.setBackground(new Color(255, 255, 255));
b2.setForeground(new Color(0, 0, 0));
b2.setText("Peg 1 to 3");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton2_actionPerformed(e);
}
});
//
// jButton3
// Move from peg2 to peg1
b3.setBackground(new Color(255, 255, 255));
b3.setForeground(new Color(0, 0, 0));
b3.setText("Peg 2 to 1");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton3_actionPerformed(e);
}
});
//
// jButton4
// Move from peg2 to peg3
b4.setBackground(new Color(255, 255, 255));
b4.setForeground(new Color(0, 0, 0));
b4.setText("Peg 2 to 3");
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton4_actionPerformed(e);
}
});
//
// jButton5
// Move from peg3 to peg1
b5.setBackground(new Color(255, 255, 255));
b5.setForeground(new Color(0, 0, 0));
b5.setText("Peg 3 to 1");
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
displayScreen.setText("dude, seriously");
jButton5_actionPerformed(e);
}
});
//
// jButton6
// Move from peg3 to peg2
b6.setBackground(new Color(255, 255, 255));
b6.setForeground(new Color(0, 0, 0));
b6.setText("Peg 3 to 2");
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton6_actionPerformed(e);
}
});
//
// jButton5
// Start game button
b7.setText("START GAME");
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton7_actionPerformed(e);
}
});
//
// contentPane
//
contentPane.setLayout(null);
contentPane.setBackground(new Color(155, 155, 209));
addComponent(contentPane, jLabel1, -1,0,526,282);
addComponent(contentPane, jLabel3, 11,475,65,18);
addComponent(contentPane, input, 9,452,228,22);
addComponent(contentPane, jScrollPane1, 9,289,505,155);
addComponent(contentPane, jProgressBar1, 11,280,503,9);
addComponent(contentPane, b1, 247,450,83,28);
addComponent(contentPane, b2, 246,485,83,28);
addComponent(contentPane, b3, 338,450,83,28);
addComponent(contentPane, b4, 338,485,83,28);
addComponent(contentPane, b5, 430,450,83,28);
addComponent(contentPane, b6, 430,485,83,28);
addComponent(contentPane, b7, 66,486,132,25);
//
// Main
//
this.setTitle("The Towers of Hanoi");
this.setLocation(new Point(112, 32));
this.setSize(new Dimension(538, 555));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jTextField1_keyPressed(KeyEvent e)
{
System.out.println("\njTextField1_keyPressed(KeyEvent e) called.");
// TODO: Add any handling code here
}
private void jButton1_actionPerformed(ActionEvent e)
{
}
private void jButton2_actionPerformed(ActionEvent e)
{
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton3_actionPerformed(ActionEvent e)
{
System.out.println("\njButton3_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton4_actionPerformed(ActionEvent e)
{
System.out.println("\njButton4_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton5_actionPerformed(ActionEvent e)
{
System.out.println("\njButton5_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton6_actionPerformed(ActionEvent e)
{
System.out.println("\njButton6_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton7_actionPerformed(ActionEvent e)
{
final boolean finished = false;
do {
displayScreen.setText("Would you like to start a new Towers of Hanoi game? [Y/N]");
char yn = input.getText().charAt(0);
if (yn == 'Y' || yn == 'y'){
//new instance of the board every game
board bd = new board();
displayScreen.setText("M)anual or A)uto mode?");
char ma = input.getText().charAt(0);
if(ma == 'm' || ma == 'M'){
int count = 0;
// String d = JOptionPane.showInputDialog("How many discs would you like to use?");
displayScreen.setText("How many discs would you like to use?");
// Parse input into integer
String d = input.getText();
int discNum = Integer.parseInt(d);
bd.board(discNum);
int optimum = (int)Math.pow(2,discNum)-1 ;
System.out.println("\n\n\n\n\n\nThe optimum number of moves to win in is "+optimum+"\n\n");
bd.pushPeg();
while(!bd.gameFINISH()){
//printing the current game state
bd.printBoard();
// System.out.println("So far you have made "+count+" moves.");
// System.out.println("Make your move!\nPlease choose peg to move from and then which peg to move to.");
// System.out.println("E.g 23 -- would move a disk from peg 2 to peg 3\n Your move.....\n\n");
String f = JOptionPane.showInputDialog("So far you have made "+count+" moves.\nPlease choose peg to move from and then which peg to move to.\nE.g 23 -- would move a disk from peg 2 to peg 3");
// Parse input into integer
int move = Integer.parseInt(f);
count++;
if (move==12){
try{
bd.movePeg12();
}catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
} else if (move==13) {
try{
bd.movePeg13();
}catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
}else if (move==21) {
try{
bd.movePeg21();
}catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
}else if (move==23) {
try{
bd.movePeg23();
}catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
}else if (move==31) {
try{
bd.movePeg31();
}catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
}else if (move==32) {
try{
bd.movePeg32();
}catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
} if (bd.gameFINISH()){
System.out.println("\n\n\n\n\n\n\n\n");
bd.printBoard();
System.out.println("Congratulations!! You have completed the game!!\n\n");
System.out.println("The optimum number of moves was "+optimum);
System.out.println("It took you "+count+" moves to finish.\n\n\n");
}
}
} else if (ma == 'a' || ma == 'A') {
displayScreen.setText("How many discs would you like to use?");
// Parse input into integer
String q = input.getText();
int discNum = Integer.parseInt(q);
bd.board(discNum);
bd.pushPeg();
try{
bd.initSolve(discNum);
} catch (Exception ee){
displayScreen.setText("\n\n*** Bad Input, please try again! \n");
}
}
}
else if (yn == 'n' || yn == 'N'){
System.out.println(" Thankyou for Playing");
System.out.println("^**____*.Have a Nice Day.*____**^");
}
} while (!finished);
}
public static void main(String[] args){ new Main(); }
}
}
}
Heres the code im unsucessfully trying to put in there :(
Any help would be greatly appreciated
package assignment2good;
import boardage.*;
import EasyIn.*;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
// Operators exceeded exception class
class Error extends Exception {
public Error() {
super("*******ERROR*******\n");
}
}
class badInput extends Exception {
public badInput() {
super("Wrong Input type... Please try again!\n");
}
}
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean finished = false;
char yn = JOptionPane.showInputDialog("Would you like to start a new Towers of Hanoi game? [Y/N]").charAt(0);
if (yn == 'Y' || yn == 'y'){
//new instance of the board every game
board bd = new board();
char ma = JOptionPane.showInputDialog("M)anual or A)uto mode?").charAt(0);
if(ma == 'm' || ma == 'M'){
int count = 0;
String d = JOptionPane.showInputDialog("How many discs would you like to use?");
// Parse input into integer
int discNum = Integer.parseInt(d);
bd.board(discNum);
int optimum = (int)Math.pow(2,discNum)-1 ;
System.out.println("\n\n\n\n\n\nThe optimum number of moves to win in is "+optimum+"\n\n");
bd.pushPeg();
while(!bd.gameFINISH()){
//printing the current game state
bd.printBoard();
String f = JOptionPane.showInputDialog("So far you have made "+count+" moves.\nPlease choose peg to move from and then which peg to move to.\nE.g 23 -- would move a disk from peg 2 to peg 3");
// Parse input into integer
int move = Integer.parseInt(f);
count++;
if (move==12){
try{
bd.movePeg12();
}catch (Exception e){
System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
}
} else if (move==13) {
try{
bd.movePeg13();
}catch (Exception e){
System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
}
}else if (move==21) {
try{
bd.movePeg21();
}catch (Exception e){
System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
}
}else if (move==23) {
try{
bd.movePeg23();
}catch (Exception e){
System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
}
}else if (move==31) {
try{
bd.movePeg31();
}catch (Exception e){
System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
}
}else if (move==32) {
try{
bd.movePeg32();
}catch (Exception e){
System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
}
} if (bd.gameFINISH()){
System.out.println("\n\n\n\n\n\n\n\n");
bd.printBoard();
System.out.println("Congratulations!! You have completed the game!!\n\n");
System.out.println("The optimum number of moves was "+optimum);
System.out.println("It took you "+count+" moves to finish.\n\n\n");
}
}
} else if (ma == 'a' || ma == 'A') {
String q = JOptionPane.showInputDialog("How many discs would you like to use?");
// Parse input into integer
int discNum = Integer.parseInt(q);
bd.board(discNum);
bd.pushPeg();
try{
bd.initSolve(discNum);
} catch (Exception e){
System.out.println(e.getMessage());
}
}
}
else if (yn == 'n' || yn == 'N'){
System.out.println(" Thankyou for Playing");
System.out.println("^**____*.Have a Nice Day.*____**^");
finished = true;
}
} while (!finished);
}
}