Jump to content

Crossword game with 2D array of JButton

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
erietha88

erietha88

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,
Here is my problem :
1. I'm trying to make crossword game with 2d array of JButon. Put number in the top left of JButton. I got it worked but the result number order is upside down. (It should have be the same order as the topLeftNum).
2. How to get the input in the JtextField to show up in the JButton and to make it check the real answer with the input text??:crying:
Help me solve this two problem...
Thanks...:)



import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;



import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;



public class Lat1 extends JFrame implements KeyListener, ActionListener {


Field f;

char answer;

String d;


final int ROWS = 12;


final int COLS = 12;


final static int topLeftNum[][]= {

	{-1, 1, 0, 2, 0, 0, 3, -1, 4, 0, 5, 0},

    {6, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, -1},

    {-1, 0, -1, 0, -1, 7, 0, 0, 8, -1, 0, -1},

    {9, 0, 0, 0, 10, -1, -1, -1, 11, 0, 0, -1},

    {0, -1, -1, 12, 0, 0, 13, -1, 0, -1, -1, -1},

    {0, -1, 14, -1, 0, -1, 0, -1, 15, 0, 0, 16},

    {17, 0, 0, 18, 0, -1, 19, 20, 0, -1, -1, 0},

    {0, -1, 0, 0, -1, 21, 0, 0, 0, -1, -1, 0},

    {22, 23, 0, 0, -1, 0, -1, 0, -1,24, 0, 0},

    {-1, 0, -1, 25, 0, 0, -1, 0, -1, 0, -1, -1},

    {26, 0, 0, -1, -1, 0, -1, 27, 0, 0, 0, -1},

    {-1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1}

    };

 


public static void main(String[] args){


SwingUtilities.invokeLater(new Runnable() {


public void run(){


new Lat1();


}


});


}


 

public Lat1() {

d = f.getText();

answer = d.charAt(0);


this.getContentPane().setLayout(new GridLayout(ROWS, COLS));


for (int j=0; j<COLS; j++) {

	for (int i=0; i<ROWS; i++)	{

this.getContentPane().add(new Box(i, //the boxes index


(topLeftNum[j][i] < 0) ? Color.BLACK : Color.WHITE, //pick the color


topLeftNum[j][i], //the topleft number


answer, //the char inside


this), //the action listener for the button


i); //the index to place it on the container


}

}




this.pack();


this.setVisible(true);


}


 


public void actionPerformed(ActionEvent e) {


Box b = (Box)e.getSource();


this.remove(b);


this.add(new Field(b, this), b.index);


this.validate();


}


public void keyPressed(KeyEvent e) { }


public void keyReleased(KeyEvent e) {


if (e.getKeyCode() == KeyEvent.VK_ENTER) {


Field f = (Field)e.getSource();


Box b = f.box;


b.setText(f.getText());


this.remove(f);


this.add(f.box, b.index);


}


this.validate();


this.repaint();


}


public void keyTyped(KeyEvent e) {}



}



class Box extends JButton {


public int index;


private String topLeftNum;



public Box(int index, Color color, int topLeftNum, char c, ActionListener al) {



this.setBackground(color);


if (color != Color.BLACK) {


this.setFont(new Font("SansSerif", Font.ITALIC, 20));


this.setText(""+ c);


this.addActionListener(al);


this.index = index;


if (topLeftNum != 0)


this.topLeftNum = topLeftNum+"";


}


else {


this.setText("");


this.setEnabled(false);


return;


}


}


public void paintComponent(Graphics g) {


super.paintComponent(g); // paints background


g.setFont(new Font("SansSerif", Font.PLAIN, 8));


if (topLeftNum != null) g.drawString(topLeftNum, 5, 10);


}


}



class Field extends JTextField {


Box box;


 

public Field(Box box, KeyListener kl) {


super(box.getText());


this.addKeyListener(kl);


this.box = box;


}


}



#2
erietha88

erietha88

    Newbie

  • Members
  • Pip
  • 2 posts
My problem has been solved.
I just have to remove the i.:)