KeyListener problem
I'm writing a program to solve sudokus for fun (yes, I kno there's already a hundred out there)
I've implemented all the KeyListener methods inside my main class, which is just "Sudoku" and set it to "implements KeyListener" along with other interfaces.
Then, in its constructor, I call "addKeyListener(this);"
I've imported java.awt.*, java.awt.event.*, and java.awt.image.*
I was having problems getting my program to respond to key input, so for each if the KeyListener methods, I commented out all my code and just put "JOptionPane.showMessageBox(null,"x");"
Behold the problem - these methods aren't getting called!
Here's the beginning of my class definition:
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
publicclass Sudokuextends JPanelimplements KeyListener,MouseListener,MouseMotionListener,WindowListener,ActionListener,ComponentListener,MouseWheelListener
{
publicint width=800;
publicint height=600;
public BufferedImage buffer;
public Graphics draw;
publicint selectx;
publicint selecty;
publicint[][] boxes;
public String[][] subscript;
My constructor:
public Sudoku()
{
boxes =newint[9][9];
subscript =new String[9][9];
for(int x=0;x<9;x++)
{
for(int y=0;y<9;y++)
{
boxes[x][y]=0;
}//end for y
}//end for x
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
addComponentListener(this);
addMouseWheelListener(this);
buffer =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
draw = buffer.getGraphics();
new Timer(0,this).start();
selectx=0;
selecty=0;
}//end constructor
My KeyListener methods:
publicvoid keyPressed(KeyEvent e)
{
JOptionPane.showInputDialog(null,"x");
}//end keyPressed
publicvoid keyReleased(KeyEvent e)
{
JOptionPane.showInputDialog(null,"x");
}//end keyReleased
publicvoid keyTyped(KeyEvent e)
{
JOptionPane.showInputDialog(null,"x");
}//end keyTyped
My program is currently using a Timer to run the main loop, which is what the "new Timer" code in my constructor does. It calls a method called render(), which draws to BufferedImage buffer using Graphics draw, then render() calls repaint(), and paintComponent() just draws buffer to the screen.
EDIT:
Oh yeah, my main{} method:
publicstaticvoid main(String[] args)
{
JFrame main =new JFrame();
int width=800;
int height=600;
main.setSize(width,height);
main.setBackground(Color.black);
main.setTitle("Sudoku Solver");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Sudoku content =new Sudoku();
main.setContentPane(content);
main.addWindowListener(content);
main.setVisible(true);
main.setFocusable(true);
/*
while(true)
{
content.render();
try{
Thread.sleep(50);
}catch(Exception e){}
}//end loop
*/
}//end main
As you can see, I tried changing it to use a while loop instead of a Timer, but that didn't fix anything, so I changed it back.
Message was edited by:
blevinstein

