I have these code below for the button :

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Coba extends Applet
{
	Button button1;
	
	public void init()
	{
		Font font = new Font("TimesRoman",Font.BOLD,16);
		setFont(font);
		
		button1 = new Button("Level Easy");
		resize(250,250);
		LevelEasy lvleasy = new LevelEasy();
		button1.addActionListener(lvleasy);
		//button1.addActionListener(this);
		add("Left",button1);	
	}
	private class LevelEasy implements ActionListener
	{	
		public void actionPerformed(ActionEvent e)		
		{		
      	Cross4 cross = new Cross4();
		}		
	}
	//----------------------------------------------
}

And one file named Cross4.class that i want to call with a button..
Here is the code for Cross4 :

import java.applet.*;
import java.awt.*;

public class Cross4 extends Applet {
    
    final	static	int	LebarKotakTTS	= 40;
    final	static	int	TinggiKotakTTS	= 40;
    final	static	int	JmlhKotakLebar	= 12;
    final	static	int	JmlhKotakTinggi	= 12;
    
    final static int QuestAreaHeight = 100;
    /*---------------------------------------------------------------*/
    final static int 	layout[][] = {
    {-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 void init() {
	int viewWidth;
	int left, top;
 	//resize(200,200);   
	NewGame();  
    }
    /*---------------------------------------------------------------*/

    public void NewGame() {
	for (int j = 0 ; j < JmlhKotakTinggi ; j++) {
	    for (int i = 0 ; i < JmlhKotakLebar ; i++) {
		gGuesses[i][j] = "";	
	    }
	}
 }

    /*----------------------------------------------*/

    public void paint(Graphics g) {
	int left = 0;
	int right = 0;
	int top = 0;
	int bottom = 0;
	int tempLeft = 0;
	int tempRight = 0;
	int tempTop = 0;
	int	viewWidth;
	int	viewHeight;
	
    
	Font f = new java.awt.Font("Helvetica", Font.BOLD, 12);
	g.setFont(f);
    
	Font numFont = new java.awt.Font("Helvetica", Font.BOLD, 10);
    
	Font answerFont = new java.awt.Font("Helvetica", 0, 18);
	Font questionFont = new java.awt.Font("Courier", Font.BOLD, 12);

	FontMetrics answerFontMetrics = g.getFontMetrics(answerFont);
	FontMetrics questionFontMetrics = g.getFontMetrics(questionFont);
	
	viewWidth = JmlhKotakLebar * LebarKotakTTS;
	viewHeight = JmlhKotakTinggi * TinggiKotakTTS;
	
    
	top = viewHeight + ((size().height / 2) - (viewHeight / 2));
	left = (size().width / 2) - (viewWidth / 2);
    
	g.setColor(Color.black);
	g.draw3DRect(left, top, viewWidth, QuestAreaHeight, true);

	
	top = (size().height / 2) - (viewHeight / 2); 
	left = (size().width / 2) - (viewWidth / 2);
	
	for (int j = 0 ; j < JmlhKotakTinggi ; j++) 
	{
	    for (int i = 0 ; i < JmlhKotakLebar ; i++) 
	    {
		tempLeft = left + (i * LebarKotakTTS);
		tempTop = top + (j * TinggiKotakTTS);
		
		g.setColor(Color.black);
		g.drawRect(tempLeft, tempTop, LebarKotakTTS , TinggiKotakTTS );
	
		if (layout[j][i] == -1){
		    g.setColor(Color.black);
		    g.fillRect(tempLeft, tempTop, LebarKotakTTS, TinggiKotakTTS);
		}
		else if (layout[j][i] != 0) 
		{
		    String numStr = String.valueOf(layout[j][i]);
		    g.setFont(numFont);
		    g.drawString(numStr, tempLeft + 4 , tempTop + 10);
		    
	    
			}
	  	}
	}
	
  }
/*------------------------------------------------------------*/
}

The problem is why when i clicked on the button, it doesn't do anything.. Is there something wrong in my code??
Can anyone please help me or? :'(

ohh well, set the object visible just u have created. you have create the object but did not make it visible. hope it wil help u.

ohh well, set the object visible just u have created. you have create the object but did not make it visible. hope it wil help u.

Thx for the suggestion. I tried it, still it doesn't seem to be working. I put the cross.setVisible(true) in the void actionPerformed. Anything else that might could be wrong?

You want to open an applet using another applet? It would make sense for an Applet to open a JFrame or a JFrame to open another JFrame, but not for an Applet which opens another Applet.... Applets are for web applications that you can open in your browser. They shouldn't open eachother.

I think what you are trying to do is have the applet open another window (you should use a JFrame), which contains your game. Or if its an applet you want, have it so clicking "Level Easy" would replace the content of the current applet with that of the game itself.

You want to open an applet using another applet? It would make sense for an Applet to open a JFrame or a JFrame to open another JFrame, but not for an Applet which opens another Applet.... Applets are for web applications that you can open in your browser. They shouldn't open eachother.

I think what you are trying to do is have the applet open another window (you should use a JFrame), which contains your game. Or if its an applet you want, have it so clicking "Level Easy" would replace the content of the current applet with that of the game itself.

Thanx a lot. :) By the way, if i want to make a make a crossword grid that i can fill in, which better i use canvas or JTextArea?

I think what you are trying to do is have the applet open another window (you should use a JFrame), which contains your game. Or if its an applet you want, have it so clicking "Level Easy" would replace the content of the current applet with that of the game itself.

Thanx a lot. :) I got it, but still have problem about mouse down, key down that i use in Applet). How to make it so that i can clicked on & fill in the crossword in JFrame? Do i have to use ActionListener or MouseListener?? How to apply that to JFrame??

I would make a class called Box which extends any kind of Container (a simple one at most, maybe even Container itself). Something like a JTextArea wouldn't really work out since you can only add text to it and can't really control the text position and fonts separately in each JTextArea.

The class Box would contain the characteristics of each Box such as: int upperLeftNum, char userLetter, char realLetter, Color background, setUserLetter(), etc which would all be passed into a constructor to create a custom box.

I would then use a 2D Box Array to hold all the Boxes. And add them directly to the JFrame or the container of the Applet (whatever ur using).

There's no 1 way to do it, but do whatever makes it feel better for you.

That was for the previous post.

Maybe a combination of a JButton and a JTextField. The JButton would use an ActionListener, when clicked, a JTextField would be placed in its place which is Editable. You can have the JTextField use a KeyListener which when you hit enter, append the text to the JButton.

You can override the paintComponent method of the JButton to add the top left number and setBackground() to change the background of each JButton if you want.

The JButton would use an ActionListener, when clicked, a JTextField would be placed in its place which is Editable.

I still don't get it about this part. So, the Jbutton will works as the cell (white & black cell) and when the JButton clicked, it will show up a JTextField or .....??(It will need a lot of button and JTextField for each button or one JTextField for all button??)

I see what your doing. You are drawing everything using the Applets paint() function. Opps on my part -- I didn't bother to look :)

Java is object oriented. I would have made each box an object and modified each object independently. Not that your way wouldn't work too :).

Btw, there's nothing wrong with having lots of buttons -- I made a MineSweeper game a long time ago that uses upwards of 500 buttons. http://web.njit.edu/~ca3/minesweeper/V2.0/MineSweeper.jar

There's nothing wrong with doing something like that. The fact that java is an OO language makes it even easier to modify your code and add more things -- you are making an Object, just like how you would make a living person with all their characteristics. Your programmed Object should not be limited by any other constraints that you think you need for your program to work.

In your case, drawing everything right on the applet is constraining you in so many ways.

I see what your doing. You are drawing everything using the Applets paint() function. Opps on my part -- I didn't bother to look :)

Java is object oriented. I would have made each box an object and modified each object independently. Not that your way wouldn't work too :).

Btw, there's nothing wrong with having lots of buttons -- I made a MineSweeper game a long time ago that uses upwards of 500 buttons. http://web.njit.edu/~ca3/minesweeper/V2.0/MineSweeper.jar

There's nothing wrong with doing something like that. The fact that java is an OO language makes it even easier to modify your code and add more things -- you are making an Object, just like how you would make a living person with all their characteristics. Your programmed Object should not be limited by any other constraints that you think you need for your program to work.

In your case, drawing everything right on the applet is constraining you in so many ways.

Anyway, i did a search about PaintComponent of button and all i found is how to change the shape of button, doesn't mention about draw string on button. Can you give me an example on how to do it??

I'll do you 1 better. I spit this out in a few minutes from some older source code I had.

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 java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class plat extends JFrame implements KeyListener, ActionListener {

	final int ROWS = 5;
	final int COLS = 5;

	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run(){
				new plat();
			}
		});
	}

	public plat() {
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new GridLayout(ROWS, COLS));

		//write an algorithm to place the words (this is all junk)
		Random rand = new Random();
		for (int i=0; i<ROWS*COLS; i++) {
			this.add(new Box(i, //the boxes index
					(rand.nextInt(5)<2) ? Color.BLACK : Color.WHITE, //pick the color
					rand.nextInt(5), //the topleft number
					(char)(65+i),  //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(Font.SANS_SERIF, 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(Font.SANS_SERIF, 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;
	}	
}
commented: very easy to understand +1

Thanx for the example..I'm new with JButton and its method..

I have this code for the number :

private class Number
		{
			public int x;
			public int y;
			public String number;
 
			Number(int x, int y, String number) 
			{
				this.x= x;
				this.y= y;
				this.number= number;
			}
		}

And trying to get the x,y position with Point, how to do it??
I mean like point.x and point.y ??
And what does the Math.max() function is for??

The whole painting on 1 canvas isn't really java oo, but i guess its still doable if your hard pressed on doing it that way.

Point p = new Point(Number.x, Number.y);
Math.max() will return the higher of 2 numbers.

I tried to change the Cross4 extends Applets to extends JFrame. (You already look at the code,right??)
(add a main method, move init()method to constructor).

import javax.swing.JFrame;
...
public class Cross4 extends JFrame {
...

public Cross4() {
  ...//init method here
}
public static void main(String[] args) {
  Cross4 applet = new Cross4();
  JFrame frame = new JFrame("Frame title");
  frame.getContentpANE.add(applet);
  frame.setSize(400,400); //initial size
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
}

But i got an error like this :

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
        at java.awt.Container.addImpl(Container.java:616)
        at java.awt.Container.add(Container.java:307)
        at Cross4.main(Cross4.java:165)
Press any key to continue...

Do you know why??

I tried to change the Cross4 extends Applets to extends JFrame. (You already look at the code,right??)
(add a main method, move init()method to constructor).

import javax.swing.JFrame;
...
public class Cross4 extends JFrame {
...

public Cross4() {
  ...//init method here
}
public static void main(String[] args) {
  Cross4 applet = new Cross4();
  JFrame frame = new JFrame("Frame title");
  frame.getContentpANE.add(applet);
  frame.setSize(400,400); //initial size
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
}

But i got an error like this :

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
        at java.awt.Container.addImpl(Container.java:616)
        at java.awt.Container.add(Container.java:307)
        at Cross4.main(Cross4.java:165)
Press any key to continue...

Do you know why??

I managed to solve this problem (I use extends JPanel) but still have some problems. It was an applet before (i use boolean mouse down, mouse enter, key down) and now when i change it to extends JPanel those thing isn't working. Any idea??

I made it like this :

PointCanvas.addMouseListener(new MouseAdapter() {  

            //saat mouse di atas button  
            // @Override  
            public void mouseEntered(MouseEvent e) {  
                 //setOver(true); 
			
            }  
   
             //saat mouse di luar button  
             //@Override  
             public void mouseExited(MouseEvent e) {  
                //setOver(false);  
            }  
  
            //@Override  
            public void mousePressed(MouseEvent e,int i,int j) {  
            i = e.getX();
            j = e.getY();
            if(i>=0 && i < JmlhKotakLbr && j>=0 && j < JmlhKotakTgg){
            	if(layout[i][j] != -1){
            	repaint();
            	}
            }
            
             }  
  
            //@Override  
             public void mouseReleased(MouseEvent e) {  
                 //setOver(false);  
            }  
         } ); 
  
    }
    
    public void paint(Graphics g) {
    	//int tempLeft=0;
    	//int tempTop =0;
    super.paint(g);
    
    g.setColor(Color.BLACK);
    g.drawString("M",(LebarKotakTTS / 2), TinggiKotakTTS / 2));
  	}

But the paint didn't working. Help....

From the code I gave you up top, take out the main method, then make a Class which extends JApplet that calls the new Class() that I gave you in its constructor.

Thats it... It only takes 2 classes, one that extends Applet and one that extends JFrame. The applet calls the new JFrame.

commented: Some good posts in this thread. +24

I'm getting problem with 2D array of JButtons. I can't get the button to show..What could be missing?

int ROWS = 12;
int COLS = 12;
for (int i = 0; i < COLS; i++) {
    for (int j = 0; j < ROWS; j++) {
        buttonArray[ROWS][COLS] = new JButton(" ");
    }
}
import java.applet.*;
import java.awt.*;

public class Coba5 extends Applet {
    
	
    final	static	int	CellWide	= 30;
    final	static	int	CellHigh	= 30;
    final	static	int	QCellWidth	= 15;
    final	static	int	QCellHeight	= 15;
    
    final static int QuestAreaHeight = 100;
	
    final	static	int	kUp = 0;
    final	static	int	kDown = 1;

    final static 	int kAcross = 0;

    int gDirection = kAcross;
    int CurrX	= 0;
    int	CurrY	= 0;

    int		gBlockMinY	= 0;
    int		gBlockMaxY 	= 0;
    int		gBlockMinX  = 0;
    int		gBlockMaxX	= 0;

    
    int		gOldBlockMinY	= 0;
    int		gOldBlockMaxY 	= 0;
    int		gOldBlockMinX   = 0;
    int		gOldBlockMaxX	= 0;
    
	
    String	letters[] = {	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
				"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /*---------------------------------------------------------------*/

    final static int 	layout[][] = {
    {-1, -1, -1, 1, 0, 0, -1, 2, 0, 0, 0, 3, -1, -1, -1},
    {4, 0, 0, 0, -1, -1, -1, 0, -1, -1, -1, 5, 0, 0, 6},
    {0, -1, -1, 0, -1, 7, 0, 0, 0, 0, -1, 0, -1, -1, 0},
    {0, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, 0, -1, -1, 0},
    {8, 9, 0, 0, 0, 0, -1, -1, -1, 10, 0, 0, 11, 0, -1},
    {-1, 0, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, 0, -1, -1},
    {12, 0, 0, -1, 13, 0, 0, 0, 14, 0, -1, 15, 0, 0, 0},
    {-1, 0, -1, -1, -1, 0, -1, -1, 0, -1, -1, -1, 0, -1, -1},
    {16, 0, 0, 0, 17, -1, -1, 18, 0, 0, 19, 0, 0, 0, -1},
    {0, 0, -1, -1, 0, -1, -1, -1, 0, -1, 0, -1, -1, -1, -1},
    {0, -1, 20, 0, 0, 0, 0, 21, 0, -1, 22, 0, 23, 0, 24},
    {0, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, -1, 0},
    {25, 0, 0, -1, 0, -1, -1, 26, 0, 0, 0, -1, 0, -1, 0},
    {-1, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, -1, 0},
    {-1, -1, -1, 27, 0, 0, 0, 0, 0, -1, 0, -1, -1, -1, -1}
    };


    final static String answers[][] = {
    {" ", " ", " ", "W", "E", "T", " ", "P", "R", "O", "U", "D", " ", " ", " "},
    {"W", "I", "S", "H", " ", " ", " ", "R", " ", " ", " ", "O", "B", "E", "Y"},
    {"A", " ", " ", "E", " ", "B", "L", "A", "C", "K", " ", "U", " ", " ", "E"},
    {"R", " ", " ", "R", " ", "E", " ", "Y", " ", " ", " ", "B", " ", " ", "S"},
    {"M", "Y", "S", "E", "L", "F", " ", " ", " ", "C", "A", "T", "C", "H", " "},
    {" ", "E", " ", " ", " ", "O", " ", " ", " ", " ", " ", " ", "H", " ", " "},
    {"I", "L", "L", " ", "P", "R", "E", "T", "T", "Y", " ", "B", "A", "R", "E"},
    {" ", "L", " ", " ", " ", "E", " ", " ", "H", " ", " ", " ", "R", " ", " "},
    {"F", "O", "U", "N", "D", " ", " ", "W", "E", "L", "C", "O", "M", "E", " "},
    {"I", "W", " ", " ", "E", " ", " ", " ", "R", " ", "L", " ", " ", " ", " "},
    {"G", " ", "B", "E", "C", "A", "U", "S", "E", " ", "O", "T", "H", "E", "R"},
    {"H", " ", "E", " ", "L", " ", " ", "E", " ", " ", "T", " ", "I", " ", "E"},
    {"T", "I", "E", " ", "A", " ", " ", "W", "I", "S", "H", " ", "R", " ", "N"},
    {" ", " ", "S", " ", "R", " ", " ", "E", " ", " ", "E", " ", "E", " ", "T"},
    {" ", " ", " ", "B", "E", "W", "A", "R", "E", " ", "S", " ", " ", " ", " "}
        };



    final static String gQuestionsAcross[] = {
	"",			//  0
	"The road is still ___ beacuse of last night rain.",		// 1
	"His father is very ___ of him because his grade are always good.",		// 2
	"I ___ that he will pass the test. He didn't even study.",		// 3
	"I ___ I could do that.",		// 4
	"You have to ___ the rules or else you will get expelled.",			   // 5
	"",		// 6
	"That is an old ___ and white TV.",		//7
	"I wanted to go with him, but I had to go by ___.",		   // 8
	"Traffic lights are usually green, ___ and red.",		// 9
	"Try to ___ the balls.",		//10
	"",		   // 11
	"He have ___ tempered. He always shouting at people",		//12
	"This is a ___ flower.",		//13
	"",		//14
	"He catch the eel with his ___ hands.",		//15
	"I ___ the money that I had lost.",		//16
	"",		//17
	"___ to my house.",		//18
	"He is wearing a glasses because he has minus ___.",		//19
	"I can't read this ___ I have broken my glasses.",		//20
	"",		//21
	"I don't see ___ than you.",		//22
	"",		//23
	"",		//24
	"The boys have to wear a ___  and the girls have to wear a ribbon.",		//25
	"I ___ i could go to the party, but i don't have the invitation card.",		//26
	"___ of the dog."		//27
	};


    final static String gQuestionsDown[] = {
	"",			//  0
	"___ do you live? I live in New York.",		// 1
	"I ___ for her happines",	// 2
	"The ___ rise at the east.",	// 3
	"The opposite of cool is ___.",	// 4
	"",		//5
	"I would like to say ___, but i can't.",		//6
	"The opposite of after is ___.",		// 7
	"",		// 8
	"Traffic lights are usually green, ___ and red.", // 9
	"",		//10
	"He has ___ that makes the girls fall for him.",			      // 11
	"",		//12
	"",		//13
	"Here, ___ and everywhere.",			//14
	"",		//15
	"He had a ___ with his friends and got punch.",		//16
	"That country ___ a war.",		      // 17
	"",		//18
	"I put all the ___ in the wash machine",		//19
	"He got sting by two ___",		//20
	"I need to bring my skirt to the ___.",		//21
	"",		//22
	"He got ___ to be an assistant of a professor.",		//23
	"He can't buy a house, but he manage to ___ a room."		//24 
	
	};


    /*---------------------------------------------------------------*/

    String gGuesses[][] = new String[QCellWidth][QCellHeight];

    boolean	gUpdateActiveAreaFlag = false;
    boolean	gChangedActiveAreaFlag = false;
	
	/*---------------------------------------------------------------*/
	
    public void init() {
	int viewWidth;
	int left, top;
	
	NewGame();  

    }

    /*---------------------------------------------------------------*/

    public void NewGame() {
	for (int j = 0 ; j < QCellHeight ; j++) {
	    for (int i = 0 ; i < QCellWidth ; i++) {
		gGuesses[i][j] = "";	
	    }
	}
	
	gOldBlockMinY	= 0;
	gOldBlockMaxY 	= 0;
	gOldBlockMinX = 0;
	gOldBlockMaxX	= 0;
	
	gDirection = kAcross;
	CurrX = 0;
	CurrY = 0;
	SetActiveBlock(CurrX, CurrY, gDirection);
	

    }

    /*----------------------------------------------*/

    public void paint(Graphics g) {
	int left = 0;
	int right = 0;
	int top = 0;
	int bottom = 0;
	int tempLeft = 0;
	int tempRight = 0;
	int tempTop = 0;
	int	viewWidth;
	int	viewHeight;
	
    
	Font f = new java.awt.Font("Helvetica", Font.BOLD, 12);
	g.setFont(f);
    
	Font numFont = new java.awt.Font("Helvetica", Font.BOLD, 10);
    
	Font answerFont = new java.awt.Font("Helvetica", 0, 18);
	Font questionFont = new java.awt.Font("Courier", Font.BOLD, 12);

	FontMetrics answerFM = g.getFontMetrics(answerFont);
	FontMetrics questionFM = g.getFontMetrics(questionFont);
	
	viewWidth = QCellWidth * CellWide;
	viewHeight = QCellHeight * CellHigh;
	
    
	top = viewHeight + ((size().height / 2) - (viewHeight / 2));
	left = (size().width / 2) - (viewWidth / 2);
    
	
	g.setColor(Color.black);
	g.draw3DRect(left, top, viewWidth, QuestAreaHeight, true);

	
	g.setFont(f);
	
	String s = new String(String.valueOf(layout[gBlockMinY][gBlockMinX]));
	
	s = s.concat(" - ");
	if (gDirection == kAcross)
	    s = s.concat("across");
	else
	    s = s.concat("down");
	g.drawString(s, left + 5, top + 12);	
	g.setFont(questionFont);
	if (gDirection == kAcross) {
	
	    if (questionFM.stringWidth(gQuestionsAcross[layout[gBlockMinY][gBlockMinX]]) > viewWidth - 4)
	    {
		g.setFont(questionFont);
	    }
	
	    g.drawString(gQuestionsAcross[layout[gBlockMinY][gBlockMinX]], 
			 (size().width / 2) - (g.getFontMetrics().stringWidth(gQuestionsAcross[layout[gBlockMinY][gBlockMinX]]) / 2), 
			 (top + QuestAreaHeight) - 8);
	
	}	
	else {
	   
	    if (questionFM.stringWidth(gQuestionsDown[layout[gBlockMinY][gBlockMinX]]) > viewWidth - 4)
	    {
		g.setFont(questionFont);
	    }
	
	    g.drawString(gQuestionsDown[layout[gBlockMinY][gBlockMinX]], 
			 (size().width / 2) - (g.getFontMetrics().stringWidth(gQuestionsDown[layout[gBlockMinY][gBlockMinX]]) / 2), 		 
			 (top + QuestAreaHeight) - 8);
	}

	
	top = (size().height / 2) - (viewHeight / 2); 
	left = (size().width / 2) - (viewWidth / 2);
	
	for (int j = 0 ; j < QCellHeight ; j++) {
	    for (int i = 0 ; i < QCellWidth ; i++) {
		tempLeft = left + (i * CellWide);
		tempTop = top + (j * CellHigh);
	
		if (InActiveBlock(i, j)) {
		    if (i == CurrX && j == CurrY)
			g.setColor(Color.cyan);
		    else
			g.setColor(Color.yellow);
		    g.fillRect(tempLeft, tempTop, CellWide, CellHigh);
		}
		else {
		    g.setColor(Color.white);
		    g.fillRect(tempLeft, tempTop, CellWide, CellHigh);
		}
	
		g.setColor(Color.black);
		g.drawRect(tempLeft, tempTop, CellWide , CellHigh );
	
		if (layout[j][i] == -1){
		    g.setColor(Color.black);
		    g.fillRect(tempLeft, tempTop, CellWide, CellHigh);
		}
		else if (layout[j][i] != 0) {
		    String numStr = String.valueOf(layout[j][i]);
		    g.setFont(numFont);
		    g.drawString(numStr, tempLeft + 4 , tempTop + 10);
		    
	    
		}
		
		if (layout[j][i] != -1) {
		    if (gGuesses[i][j].length() != 0) {
			int sWidth = 0;
			
			if (gGuesses[i][j].equalsIgnoreCase(answers[j][i]) == false)
			    g.setColor(Color.red);
			else
			    g.setColor(Color.black);
			
			sWidth = answerFM.stringWidth(gGuesses[i][j]);
			g.setFont(answerFont);
			g.drawString( gGuesses[i][j], tempLeft + ((CellWide / 2) - (sWidth / 2)), (tempTop + CellHigh) - 6);
		    
		    }
		    
		}	
		
	    }
	}
	
    }

    /*----------------------------------------------*/
	
    void PaintWord(Graphics g, int minX, int maxX, int minY, int maxY) {
	int viewWidth = QCellWidth * CellWide;
	int viewHeight = QCellHeight * CellHigh;
	int left =(size().width / 2) - (viewWidth / 2);
	int top = (size().height / 2) - (viewHeight / 2); 
	int tempLeft = 0;
	int tempRight = 0;
	int tempTop = 0;
    
	left += (minX * CellWide);
	top += (minY * CellHigh);
	
	       
	Font f = new java.awt.Font("Helvetica", 0, 12);
	g.setFont(f);
	       
	Font numFont = new java.awt.Font("Helvetica", Font.BOLD, 10);
	       
	Font answerFont = new java.awt.Font("Helvetica", 0, 18);
	FontMetrics answerFM = g.getFontMetrics(answerFont);
	    
	
	viewWidth = QCellWidth * CellWide;
	viewHeight = QCellHeight * CellHigh;
	       
	//left = (size().width / 2) - (viewWidth / 2);
	//top = (size().height / 2) - (viewHeight / 2);
	     
	for (int j = minY ; j <= maxY ; j++) {
	    for (int i = minX ; i <= maxX ; i++) {
		tempLeft = left + (i * CellWide);
		tempTop = top + (j * CellHigh);
		       
		if (InActiveBlock(i, j)) {
		    if (i == CurrX && j == CurrY)
			g.setColor(Color.cyan);
		    else
			g.setColor(Color.yellow);
		    g.fillRect(tempLeft, tempTop, CellWide, CellHigh);
		}
		else {
		    g.setColor(Color.white);
		    g.fillRect(tempLeft, tempTop, CellWide, CellHigh);
		}
		       
		g.setColor(Color.black);
		g.drawRect(tempLeft, tempTop, CellWide , CellHigh );
		       
		if (layout[j][i] == -1) {
		    g.setColor(Color.black);
		    g.fillRect(tempLeft, tempTop, CellWide, CellHigh);
		}
		else if (layout[j][i] != 0) {
		    String numStr = String.valueOf(layout[j][i]);
			   
		    g.setFont(numFont);
		    g.drawString(numStr, tempLeft + 4 , tempTop + 10);
			   
		}
		
		if (layout[j][i] != -1) {
		    if (gGuesses[i][j].length() != 0) {
			int sWidth = 0;
			
			if (gGuesses[i][j].equalsIgnoreCase(answers[j][i]) == false)
			    g.setColor(Color.red);
			else
			    g.setColor(Color.black);
			
			sWidth = answerFM.stringWidth(gGuesses[i][j]);
			g.setFont(answerFont);
			g.drawString( gGuesses[i][j], tempLeft + ((CellWide / 2) - (sWidth / 2)), (tempTop + CellHigh) - 6);
		    }
		    
		}	
		
	    }
	}
		
    }
	
    /*----------------------------------------------*/
	
    void PaintQuestionArea(Graphics g) {
	Font f = new java.awt.Font("Helvetica", Font.BOLD, 12);
	Font questionFont = new java.awt.Font("Courier", Font.BOLD, 12);
	FontMetrics questionFM = g.getFontMetrics(questionFont);
	
   
	int viewWidth = QCellWidth * CellWide;
	int viewHeight = QCellHeight * CellHigh;
    
	int top = viewHeight + ((size().height / 2) - (viewHeight / 2));
	int left = (size().width / 2) - (viewWidth / 2);
			 	
    
	g.setFont(f);
	String s = new String(	String.valueOf(layout[gBlockMinY][gBlockMinX]));
	
	s = s.concat(" - ");
	
	if (gDirection == kAcross)
	    s = s.concat("across");
	else
	    s = s.concat("down");
	g.drawString(s, left + 5, top + 12);	
	g.setFont(questionFont);
	int fontSize = 12;
	if (gDirection == kAcross) {
		
	    if (questionFM.stringWidth(gQuestionsAcross[layout[gBlockMinY][gBlockMinX]]) > viewWidth - 4) {
	
		g.setFont(questionFont);
	    }
	
	    g.drawString(gQuestionsAcross[layout[gBlockMinY][gBlockMinX]], 
			 (size().width / 2) - (g.getFontMetrics().stringWidth(gQuestionsAcross[layout[gBlockMinY][gBlockMinX]]) / 2), 
			 (top + QuestAreaHeight) - 8);
	}	
	else {
	
	    if (questionFM.stringWidth(gQuestionsDown[layout[gBlockMinY][gBlockMinX]]) > viewWidth - 4) {
		
		g.setFont(questionFont);
	    }
	
	    g.drawString(gQuestionsDown[layout[gBlockMinY][gBlockMinX]], 
			 (size().width / 2) - (g.getFontMetrics().stringWidth(gQuestionsDown[layout[gBlockMinY][gBlockMinX]]) / 2), 
			 (top + QuestAreaHeight) - 8);
	}
	
	

    }
    
    /*----------------------------------------------*/

    private boolean InActiveBlock(int x, int y) {
	if (x < gBlockMinX)
	    return(false);
	if (x > gBlockMaxX)
	    return(false);
	if (y < gBlockMinY)
	    return(false);
	if (y > gBlockMaxY)
	    return(false);
    
	return(true);
    }

    /*----------------------------------------------*/

    private void SetActiveBlock(int x, int y, int direction) {
	int tempx;
	int tempy;
	
	gOldBlockMinY	= gBlockMinY;
	gOldBlockMaxY 	= gBlockMaxY;
	gOldBlockMinX   = gBlockMinX;
	gOldBlockMaxX	= gBlockMaxX;
	
	if (direction == kAcross) {
	    
	    gBlockMinY = y;
	    gBlockMaxY = y;
	    tempx = x;
	    
	    while (tempx > 0 && layout[y][tempx] != -1) {
		tempx--;
	    }
	    if (tempx > 0)
		gBlockMinX = tempx + 1;
	    else {
		if (layout[y][0] == -1)
		    gBlockMinX = 1;
		else
		    gBlockMinX = 0;
	    }

	    tempx = x;
	    while (tempx < QCellWidth && layout[y][tempx] != -1)
	    {
		tempx++;
	    }
	    gBlockMaxX = tempx -1;
	}
	else {
	    gBlockMinX = x;
	    gBlockMaxX = x;
	    tempy = y;
	    while (tempy > 0 && layout[tempy][x] != -1) {
		tempy--;
	    }
	    if (tempy > 0)
		gBlockMinY = tempy + 1;
	    else {
		if (layout[0][x] == -1)
		    gBlockMinY = 1;
		else
		    gBlockMinY = 0;
	    }

	    tempy = y;
	    while (tempy < QCellHeight && layout[tempy][x] != -1) {
		tempy++;
	    }
	    gBlockMaxY = tempy -1;
	}	
    }

    /*----------------------------------------------*/

    public void Update(Graphics g)  {

	if (gChangedActiveAreaFlag == false && gUpdateActiveAreaFlag == false) {		
	    paint(g);
	    return;
	}

	if (gChangedActiveAreaFlag == true) {
	   	PaintQuestionArea(g);
	    PaintWord(g, gOldBlockMinX, gOldBlockMaxX, gOldBlockMinY, gOldBlockMaxY);
	    PaintWord(g, gBlockMinX, gBlockMaxX, gBlockMinY, gBlockMaxY);
	    gChangedActiveAreaFlag = false;
	    return;
	}

	//-----------------------------------------------
	
	if (gUpdateActiveAreaFlag == true) {
	    gUpdateActiveAreaFlag = false;
	    PaintWord(g, gBlockMinX, gBlockMaxX, gBlockMinY, gBlockMaxY);
	    return;
	}
	
    }

    /*----------------------------------------------*/

    public boolean mouseDown(java.awt.Event evt, int x, int y) {
	
	
	int viewWidth = QCellWidth * CellWide;
	int viewHeight = QCellHeight * CellHigh;
	int left = (size().width / 2) - (viewWidth / 2);
	int top  =viewHeight + ((size().height / 2) - (viewHeight / 2));
	
	requestFocus();

	if (x < left)
	    return false;
	if (y < top)
	    return false;
    // assignment i = i / CellHigh
	int j = y - top;
	j /= CellHigh;
    // assignment i = i / CellWide
	int i = x - left;
	i /= CellWide;
    
    
	if (i >= 0 && i < QCellWidth && j >= 0 && j < QCellHeight) {
	    if (layout[j][i] != -1) {
		CurrX = i;
		CurrY = j;
		if (InActiveBlock(i, j)) {
		    gUpdateActiveAreaFlag = true;
		    repaint();
		}
		else {
		    SetActiveBlock(i, j, gDirection);
		    gChangedActiveAreaFlag = true;
		    repaint();
		}
		return true;
	    }
	}
	return true;
	
	
    }
    /*----------------------------------------------*/

    public boolean mouseUp(java.awt.Event evt, int x, int y) {
	requestFocus();
	return true;
    }
    /*----------------------------------------------*/

    public boolean mouseDrag(java.awt.Event evt, int x, int y) {
	requestFocus();
	return true;
    }
    /*----------------------------------------------*/

    public boolean mouseExit(java.awt.Event evt) {
	return true;
    }
    /*----------------------------------------------*/

    public boolean mouseEnter(java.awt.Event evt) {
	requestFocus();
	return true;
    }
    /*----------------------------------------------*/

    public boolean mouseMove(java.awt.Event evt, int x, int y) {
	requestFocus();
	return true;
    }
    /*----------------------------------------------*/
    public boolean keyDown(java.awt.Event evt, int key) {

	if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z')) {
	    char charArray[] = new char[1];
    
	    charArray[0] = (char)key;
    
	    gGuesses[CurrX][CurrY] = new String(charArray); 
	    gGuesses[CurrX][CurrY] = gGuesses[CurrX][CurrY].toUpperCase();
    
	    gUpdateActiveAreaFlag = true;
	    repaint();
	    return true;
	}

	switch ((char)key) {
	  case ' ':
	    ChangeDirection();
	    gChangedActiveAreaFlag = true;
	    repaint();
	    break;
	  
	}
	return true;
    }
    /*----------------------------------------------*/
    boolean PositionInRect(int x, int y, int left, int top, int right, int bottom) {
	if (x < left)
	    return(false);
    
	if (x > right)
	    return(false);
    
	if (y < top)
	    return(false);
    
	if (y > bottom)
	    return(false);
    
	return(true);
    }
    /*----------------------------------------------*/
    void ChangeDirection() {
	if (gDirection == kDown) {
	    gDirection = kUp;	
	}
	else
	    gDirection = kDown;
	SetActiveBlock(CurrX, CurrY, gDirection);	
    }


}

Here is the code that my tutor gave me. The task is to make it into 2 classes like what you said.

It only takes 2 classes, one that extends Applet and one that extends JFrame. The applet calls the new JFrame.

Any idea on how to do it??

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jcross2 extends JFrame
{	
final static int ROWS = 12;
final static 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 Jcross2()
	{	
		//JFrame f1=new JFrame();
		this.getContentPane().setLayout(new GridLayout(ROWS,COLS));
		
		for(int k = 0; k<ROWS*COLS; k++)
		{	
			for(int i = 0;i < ROWS; i++)
			{	
				for(int j = 0; j < COLS; j++)
				{	
				String numstr = String.valueOf(topLeftNum[i][j]);
					
						this.getContentPane().add(new Box(topLeftNum[i][j],
						Color.WHITE,
						numstr,
						(char)(65+1),
						this),
						topLeftNum[i][j]);
					}
				}
		//if(topLeftNum[i][j] == -1){
		//btn.setBackground(Color.BLACK);}
		}
		this.pack();
	
			
			
	}

	public static void main(String args[])
	{
		Jcross2 j = new Jcross2();
		j.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {

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

	//this.remove(b);
	}

}

class Box extends JButton
{
	public int index;

	public Box(int index, Color color, String numstr, char c, ActionListener al)
	{
		//this.setBackground(color);

				this.setText(c+"");
				this.addActionListener(al);
				this.setBackground(Color.BLACK);
	}
}

2D Array of JButton . I got an error.. Help...Thanks..

Sorry, if i'm asking too much. I doing the JButton with 2D array as the number. But i got the number result upside down. The top row become the last row and the last row become the top row number. How to solve this problem ??

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}
    };
 for (int j=0; j<COLS; j++) {
	for (int i=0; i<ROWS; i++)	{
//String numstr = String.valueOf(topLeftNum[i][j]);
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

(char)(65+i), //the char inside

this), //the action listener for the button

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

}
}

Thanks for theWhite. You have been so much help. My problem has been solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.